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 {
mergeMap,
callForAllDataSourcesAsync,
callForAllDataSources,
callForDefaultDataSource,
callByRetrieveAETitle,
createMergeDataSourceApi,
} from './index';
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', () => {
let path,
sourceName,
mergeConfig,
extensionManager,
series1,
series2,
series3,
series4,
mergeKey,
tagFunc,
dataSourceAndSeriesMap,
dataSourceAndUIDsMap,
dataSourceAndDSMap,
pathSync;
let path: string, pathSync: string;
let extensionManager: {
dataSourceDefs: Record<string, DataSourceDefinition>;
getDataSources: jest.Mock<DataSourceInstance[], [string]>;
},
series1: SeriesData,
series2: SeriesData,
series3: SeriesData,
mergeKey: string,
dataSourceAndSeriesMap: DataSourceAndSeriesMap,
dataSourceAndUIDsMap: Record<string, string[]>,
dataSourceAndDSMap: DataSourceAndDSMap;
beforeAll(() => {
path = 'query.series.search';
pathSync = 'getImageIdsForInstance';
tagFunc = jest.fn((data, sourceName) =>
data.map(item => ({ ...item, RetrieveAETitle: sourceName }))
);
sourceName = 'dicomweb1';
mergeKey = 'seriesInstanceUid';
series1 = { [mergeKey]: '123' };
series2 = { [mergeKey]: '234' };
series3 = { [mergeKey]: '345' };
series4 = { [mergeKey]: '456' };
mergeConfig = {
seriesMerge: {
dataSourceNames: ['dicomweb1', 'dicomweb2'],
defaultDataSourceName: 'dicomweb1',
},
};
dataSourceAndSeriesMap = {
dataSource1: series1,
dataSource2: series2,
@ -115,8 +120,9 @@ describe('MergeDataSource', () => {
mergeMap,
path,
args: [],
extensionManager,
extensionManager: extensionManager as any,
dataSourceNames: ['dataSource1', 'dataSource2'],
defaultDataSourceName: 'dataSource1',
});
/** Assert */
@ -140,8 +146,9 @@ describe('MergeDataSource', () => {
const data = callForAllDataSources({
path: pathSync,
args: [],
extensionManager,
extensionManager: extensionManager as any,
dataSourceNames: ['dataSource2', 'dataSource3'],
defaultDataSourceName: 'dataSource1',
});
/** Assert */
@ -165,7 +172,7 @@ describe('MergeDataSource', () => {
const data = callForDefaultDataSource({
path: pathSync,
args: [],
extensionManager,
extensionManager: extensionManager as any,
defaultDataSourceName: 'dataSource2',
});
@ -178,8 +185,10 @@ describe('MergeDataSource', () => {
describe('callByRetrieveAETitle', () => {
it('should call the correct function and return the data', () => {
const mockedGetSeries = DicomMetadataStore.getSeries as jest.Mock;
/** Arrange */
DicomMetadataStore.getSeries.mockImplementationOnce(() => [series2]);
mockedGetSeries.mockImplementationOnce(() => [series2]);
extensionManager.getDataSources = jest.fn(dataSourceName => [
{
[pathSync]: () => dataSourceAndUIDsMap[dataSourceName],
@ -190,7 +199,7 @@ describe('MergeDataSource', () => {
const data = callByRetrieveAETitle({
path: pathSync,
args: [dataSourceAndDSMap['dataSource2']],
extensionManager,
extensionManager: extensionManager as any,
defaultDataSourceName: 'dataSource2',
});

View File

@ -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);
});
});