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

This commit is contained in:
Vinícius Alves de Faria Resende 2025-06-18 15:15:50 -03:00 committed by GitHub
parent f6337e0970
commit 268d62e991
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 96 additions and 49 deletions

View File

@ -1,49 +1,54 @@
import { DicomMetadataStore, IWebApiDataSource } from '@ohif/core'; import { DicomMetadataStore } from '@ohif/core';
import { import {
mergeMap, mergeMap,
callForAllDataSourcesAsync, callForAllDataSourcesAsync,
callForAllDataSources, callForAllDataSources,
callForDefaultDataSource, callForDefaultDataSource,
callByRetrieveAETitle, callByRetrieveAETitle,
createMergeDataSourceApi,
} from './index'; } from './index';
jest.mock('@ohif/core'); jest.mock('@ohif/core');
/** Those types aren't exported by their respective modules, thus defined here */
type DataSourceDefinition = {
sourceName: string;
configuration: object;
};
type SeriesData = Record<string, unknown>;
type DataSourceAndSeriesMap = Record<string, SeriesData>;
type DisplaySet = {
StudyInstanceUID: string;
SeriesInstanceUID: string;
};
type DataSourceAndDSMap = Record<string, { displaySet: DisplaySet }>;
type QuerySeriesSearchMock = jest.Mock<Promise<SeriesData[]>, []>;
type GetImageIdsForInstanceFn = () => string[];
type DataSourceInstance = {
[key: string]: QuerySeriesSearchMock | GetImageIdsForInstanceFn;
};
describe('MergeDataSource', () => { describe('MergeDataSource', () => {
let path, let path: string, pathSync: string;
sourceName,
mergeConfig, let extensionManager: {
extensionManager, dataSourceDefs: Record<string, DataSourceDefinition>;
series1, getDataSources: jest.Mock<DataSourceInstance[], [string]>;
series2, },
series3, series1: SeriesData,
series4, series2: SeriesData,
mergeKey, series3: SeriesData,
tagFunc, mergeKey: string,
dataSourceAndSeriesMap, dataSourceAndSeriesMap: DataSourceAndSeriesMap,
dataSourceAndUIDsMap, dataSourceAndUIDsMap: Record<string, string[]>,
dataSourceAndDSMap, dataSourceAndDSMap: DataSourceAndDSMap;
pathSync;
beforeAll(() => { beforeAll(() => {
path = 'query.series.search'; path = 'query.series.search';
pathSync = 'getImageIdsForInstance'; pathSync = 'getImageIdsForInstance';
tagFunc = jest.fn((data, sourceName) =>
data.map(item => ({ ...item, RetrieveAETitle: sourceName }))
);
sourceName = 'dicomweb1';
mergeKey = 'seriesInstanceUid'; mergeKey = 'seriesInstanceUid';
series1 = { [mergeKey]: '123' }; series1 = { [mergeKey]: '123' };
series2 = { [mergeKey]: '234' }; series2 = { [mergeKey]: '234' };
series3 = { [mergeKey]: '345' }; series3 = { [mergeKey]: '345' };
series4 = { [mergeKey]: '456' };
mergeConfig = {
seriesMerge: {
dataSourceNames: ['dicomweb1', 'dicomweb2'],
defaultDataSourceName: 'dicomweb1',
},
};
dataSourceAndSeriesMap = { dataSourceAndSeriesMap = {
dataSource1: series1, dataSource1: series1,
dataSource2: series2, dataSource2: series2,
@ -115,8 +120,9 @@ describe('MergeDataSource', () => {
mergeMap, mergeMap,
path, path,
args: [], args: [],
extensionManager, extensionManager: extensionManager as any,
dataSourceNames: ['dataSource1', 'dataSource2'], dataSourceNames: ['dataSource1', 'dataSource2'],
defaultDataSourceName: 'dataSource1',
}); });
/** Assert */ /** Assert */
@ -140,8 +146,9 @@ describe('MergeDataSource', () => {
const data = callForAllDataSources({ const data = callForAllDataSources({
path: pathSync, path: pathSync,
args: [], args: [],
extensionManager, extensionManager: extensionManager as any,
dataSourceNames: ['dataSource2', 'dataSource3'], dataSourceNames: ['dataSource2', 'dataSource3'],
defaultDataSourceName: 'dataSource1',
}); });
/** Assert */ /** Assert */
@ -165,7 +172,7 @@ describe('MergeDataSource', () => {
const data = callForDefaultDataSource({ const data = callForDefaultDataSource({
path: pathSync, path: pathSync,
args: [], args: [],
extensionManager, extensionManager: extensionManager as any,
defaultDataSourceName: 'dataSource2', defaultDataSourceName: 'dataSource2',
}); });
@ -178,8 +185,10 @@ describe('MergeDataSource', () => {
describe('callByRetrieveAETitle', () => { describe('callByRetrieveAETitle', () => {
it('should call the correct function and return the data', () => { it('should call the correct function and return the data', () => {
const mockedGetSeries = DicomMetadataStore.getSeries as jest.Mock;
/** Arrange */ /** Arrange */
DicomMetadataStore.getSeries.mockImplementationOnce(() => [series2]); mockedGetSeries.mockImplementationOnce(() => [series2]);
extensionManager.getDataSources = jest.fn(dataSourceName => [ extensionManager.getDataSources = jest.fn(dataSourceName => [
{ {
[pathSync]: () => dataSourceAndUIDsMap[dataSourceName], [pathSync]: () => dataSourceAndUIDsMap[dataSourceName],
@ -190,7 +199,7 @@ describe('MergeDataSource', () => {
const data = callByRetrieveAETitle({ const data = callByRetrieveAETitle({
path: pathSync, path: pathSync,
args: [dataSourceAndDSMap['dataSource2']], args: [dataSourceAndDSMap['dataSource2']],
extensionManager, extensionManager: extensionManager as any,
defaultDataSourceName: 'dataSource2', defaultDataSourceName: 'dataSource2',
}); });

View File

@ -2,19 +2,39 @@ import getDirectURL from './getDirectURL';
import getBulkdataValue from './getBulkdataValue'; import getBulkdataValue from './getBulkdataValue';
import createRenderedRetrieve from './createRenderedRetrieve'; import createRenderedRetrieve from './createRenderedRetrieve';
const mockedGetBulkdataValue = getBulkdataValue as jest.Mock;
const mockedCreateRenderedRetrieve = createRenderedRetrieve as jest.Mock;
jest.mock('@ohif/core'); jest.mock('@ohif/core');
jest.mock('./getBulkdataValue'); jest.mock('./getBulkdataValue');
jest.mock('./createRenderedRetrieve'); jest.mock('./createRenderedRetrieve');
global.URL.createObjectURL = jest.fn(() => 'blob:'); global.URL.createObjectURL = jest.fn(() => 'blob:') as jest.Mock;
describe('getDirectURL', () => { 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, singlepart: true,
defaultType: 'video/mp4', defaultType: 'video/mp4',
}; };
const params = { const params: GetDirectURLParams = {
tag: 'PixelData', tag: 'PixelData',
defaultPath: '/path/to/pixeldata', defaultPath: '/path/to/pixeldata',
instance: { 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', () => { it('should return the provided URL if it exists', () => {
const url = 'https://example.com/direct-retrieve'; const url = 'https://example.com/direct-retrieve';
@ -46,7 +72,7 @@ describe('getDirectURL', () => {
instance: { instance: {
...params.instance, ...params.instance,
PixelData: value, PixelData: value,
}, } as GetDirectURLParams['instance'],
}); });
expect(result).toBe(value.DirectRetrieveURL); expect(result).toBe(value.DirectRetrieveURL);
@ -63,7 +89,7 @@ describe('getDirectURL', () => {
instance: { instance: {
...params.instance, ...params.instance,
PixelData: value, PixelData: value,
}, } as GetDirectURLParams['instance'],
}); });
expect(result).toContain('blob:'); expect(result).toContain('blob:');
@ -85,7 +111,7 @@ describe('getDirectURL', () => {
instance: { instance: {
...params.instance, ...params.instance,
PixelData: value, 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 () => { 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', BulkDataURI: 'https://example.com/bulkdata',
retrieveBulkData: jest.fn().mockResolvedValueOnce(new Uint8Array([0, 1, 2])), retrieveBulkData: jest.fn().mockResolvedValueOnce(new Uint8Array([0, 1, 2])),
}; };
@ -109,7 +140,7 @@ describe('getDirectURL', () => {
instance: { instance: {
...params.instance, ...params.instance,
PixelData: value, 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 () => { it('should return the BulkDataURI with defaultType if singlepart does not include fetchPart', async () => {
const arr = new Uint8Array([0, 1, 2]); 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', BulkDataURI: 'https://example.com/bulkdata',
retrieveBulkData: jest.fn().mockResolvedValueOnce(arr), retrieveBulkData: jest.fn().mockResolvedValueOnce(arr),
}; };
@ -135,35 +171,37 @@ describe('getDirectURL', () => {
instance: { instance: {
...params.instance, ...params.instance,
PixelData: value, PixelData: value,
}, } as GetDirectURLParams['instance'],
} }
); );
expect(result).toContain('blob:'); 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', () => { it('should return the URL from getBulkdataValue if it exists', () => {
const bulkDataURL = 'https://example.com/bulkdata'; const bulkDataURL = 'https://example.com/bulkdata';
getBulkdataValue.mockReturnValueOnce(bulkDataURL); mockedGetBulkdataValue.mockReturnValueOnce(bulkDataURL);
const result = getDirectURL(config, params); const result = getDirectURL(config, params);
expect(getBulkdataValue).toHaveBeenCalledWith(config, params); expect(mockedGetBulkdataValue).toHaveBeenCalledWith(config, params);
expect(result).toBe(bulkDataURL); expect(result).toBe(bulkDataURL);
}); });
it('should return the URL from createRenderedRetrieve if getBulkdataValue returns falsy', () => { it('should return the URL from createRenderedRetrieve if getBulkdataValue returns falsy', () => {
const renderedRetrieveURL = 'https://example.com/rendered-retrieve'; const renderedRetrieveURL = 'https://example.com/rendered-retrieve';
getBulkdataValue.mockReturnValueOnce(null); mockedGetBulkdataValue.mockReturnValueOnce(null);
createRenderedRetrieve.mockReturnValueOnce(renderedRetrieveURL); mockedCreateRenderedRetrieve.mockReturnValueOnce(renderedRetrieveURL);
const result = getDirectURL(config, params); const result = getDirectURL(config, params);
expect(getBulkdataValue).toHaveBeenCalledWith(config, params); expect(mockedGetBulkdataValue).toHaveBeenCalledWith(config, params);
expect(createRenderedRetrieve).toHaveBeenCalledWith(config, params); expect(mockedCreateRenderedRetrieve).toHaveBeenCalledWith(config, params);
expect(result).toBe(renderedRetrieveURL); expect(result).toBe(renderedRetrieveURL);
}); });
}); });