fix(UnitTest): run was failing due to JS tests importing TS modules (#5145)

This commit is contained in:
Vinícius Alves de Faria Resende authored and GitHub committed 2025-06-18 14:15:50 -04:00
1 parent f6337e0970
commit 268d62e991
2 files changed
+96 -49

No files matched your search

@@ -2,19 +2,39 @@ import getDirectURL from './getDirectURL';
import getBulkdataValue from './getBulkdataValue';
import createRenderedRetrieve from './createRenderedRetrieve';
const mockedGetBulkdataValue = getBulkdataValue as jest.Mock;
const mockedCreateRenderedRetrieve = createRenderedRetrieve as jest.Mock;
jest.mock('@ohif/core');
jest.mock('./getBulkdataValue');
jest.mock('./createRenderedRetrieve');
global.URL.createObjectURL = jest.fn(() => 'blob:');
global.URL.createObjectURL = jest.fn(() => 'blob:') as jest.Mock;
describe('getDirectURL', () => {
const config = {
interface GetDirectURLConfig {
singlepart: boolean | string[];
defaultType: string;
}
interface GetDirectURLParams {
tag: string;
defaultPath: string;
instance: {
StudyInstanceUID: string;
SeriesInstanceUID: string;
SOPInstanceUID: string;
[key: string]: unknown;
};
url?: string;
}
const config: GetDirectURLConfig = {
singlepart: true,
defaultType: 'video/mp4',
};
const params = {
const params: GetDirectURLParams = {
tag: 'PixelData',
defaultPath: '/path/to/pixeldata',
instance: {
@@ -24,6 +44,12 @@ describe('getDirectURL', () => {
},
};
beforeEach(() => {
mockedGetBulkdataValue.mockClear();
mockedCreateRenderedRetrieve.mockClear();
(global.URL.createObjectURL as jest.Mock).mockClear();
});
it('should return the provided URL if it exists', () => {
const url = 'https://example.com/direct-retrieve';
@@ -46,7 +72,7 @@ describe('getDirectURL', () => {
instance: {
...params.instance,
PixelData: value,
},
} as GetDirectURLParams['instance'],
});
expect(result).toBe(value.DirectRetrieveURL);
@@ -63,7 +89,7 @@ describe('getDirectURL', () => {
instance: {
...params.instance,
PixelData: value,
},
} as GetDirectURLParams['instance'],
});
expect(result).toContain('blob:');
@@ -85,7 +111,7 @@ describe('getDirectURL', () => {
instance: {
...params.instance,
PixelData: value,
},
} as GetDirectURLParams['instance'],
}
);
@@ -93,7 +119,12 @@ describe('getDirectURL', () => {
});
it('should return the BulkDataURI with defaultType if singlepart is false with retrieveBulkData', async () => {
const value = {
interface PixelDataValueWithRetrieveBulkData {
BulkDataURI: string;
retrieveBulkData: jest.Mock<Promise<Uint8Array>, []>;
}
const value: PixelDataValueWithRetrieveBulkData = {
BulkDataURI: 'https://example.com/bulkdata',
retrieveBulkData: jest.fn().mockResolvedValueOnce(new Uint8Array([0, 1, 2])),
};
@@ -109,7 +140,7 @@ describe('getDirectURL', () => {
instance: {
...params.instance,
PixelData: value,
},
} as GetDirectURLParams['instance'],
}
);
@@ -119,7 +150,12 @@ describe('getDirectURL', () => {
it('should return the BulkDataURI with defaultType if singlepart does not include fetchPart', async () => {
const arr = new Uint8Array([0, 1, 2]);
const value = {
interface PixelDataValueWithRetrieveBulkData {
BulkDataURI: string;
retrieveBulkData: jest.Mock<Promise<Uint8Array>, []>;
}
const value: PixelDataValueWithRetrieveBulkData = {
BulkDataURI: 'https://example.com/bulkdata',
retrieveBulkData: jest.fn().mockResolvedValueOnce(arr),
};
@@ -135,35 +171,37 @@ describe('getDirectURL', () => {
instance: {
...params.instance,
PixelData: value,
},
} as GetDirectURLParams['instance'],
}
);
expect(result).toContain('blob:');
expect(URL.createObjectURL).toHaveBeenCalledWith(new Blob([arr], { type: 'accept=video/mp4' }));
expect(global.URL.createObjectURL).toHaveBeenCalledWith(
new Blob([arr], { type: 'accept=video/mp4' })
);
});
it('should return the URL from getBulkdataValue if it exists', () => {
const bulkDataURL = 'https://example.com/bulkdata';
getBulkdataValue.mockReturnValueOnce(bulkDataURL);
mockedGetBulkdataValue.mockReturnValueOnce(bulkDataURL);
const result = getDirectURL(config, params);
expect(getBulkdataValue).toHaveBeenCalledWith(config, params);
expect(mockedGetBulkdataValue).toHaveBeenCalledWith(config, params);
expect(result).toBe(bulkDataURL);
});
it('should return the URL from createRenderedRetrieve if getBulkdataValue returns falsy', () => {
const renderedRetrieveURL = 'https://example.com/rendered-retrieve';
getBulkdataValue.mockReturnValueOnce(null);
createRenderedRetrieve.mockReturnValueOnce(renderedRetrieveURL);
mockedGetBulkdataValue.mockReturnValueOnce(null);
mockedCreateRenderedRetrieve.mockReturnValueOnce(renderedRetrieveURL);
const result = getDirectURL(config, params);
expect(getBulkdataValue).toHaveBeenCalledWith(config, params);
expect(createRenderedRetrieve).toHaveBeenCalledWith(config, params);
expect(mockedGetBulkdataValue).toHaveBeenCalledWith(config, params);
expect(mockedCreateRenderedRetrieve).toHaveBeenCalledWith(config, params);
expect(result).toBe(renderedRetrieveURL);
});
});