From 268d62e9913f6ade375e663ba5736b8ebd0c1075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Alves=20de=20Faria=20Resende?= Date: Wed, 18 Jun 2025 15:15:50 -0300 Subject: [PATCH] fix(UnitTest): run was failing due to JS tests importing TS modules (#5145) --- .../{index.test.js => index.test.ts} | 73 +++++++++++-------- ...DirectURL.test.js => getDirectURL.test.ts} | 72 +++++++++++++----- 2 files changed, 96 insertions(+), 49 deletions(-) rename extensions/default/src/MergeDataSource/{index.test.js => index.test.ts} (75%) rename extensions/default/src/utils/{getDirectURL.test.js => getDirectURL.test.ts} (65%) diff --git a/extensions/default/src/MergeDataSource/index.test.js b/extensions/default/src/MergeDataSource/index.test.ts similarity index 75% rename from extensions/default/src/MergeDataSource/index.test.js rename to extensions/default/src/MergeDataSource/index.test.ts index 2b915e329..5a7dfbb07 100644 --- a/extensions/default/src/MergeDataSource/index.test.js +++ b/extensions/default/src/MergeDataSource/index.test.ts @@ -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; +type DataSourceAndSeriesMap = Record; +type DisplaySet = { + StudyInstanceUID: string; + SeriesInstanceUID: string; +}; +type DataSourceAndDSMap = Record; +type QuerySeriesSearchMock = jest.Mock, []>; +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; + getDataSources: jest.Mock; + }, + series1: SeriesData, + series2: SeriesData, + series3: SeriesData, + mergeKey: string, + dataSourceAndSeriesMap: DataSourceAndSeriesMap, + dataSourceAndUIDsMap: Record, + 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', }); diff --git a/extensions/default/src/utils/getDirectURL.test.js b/extensions/default/src/utils/getDirectURL.test.ts similarity index 65% rename from extensions/default/src/utils/getDirectURL.test.js rename to extensions/default/src/utils/getDirectURL.test.ts index eb13e5ac9..058c3a787 100644 --- a/extensions/default/src/utils/getDirectURL.test.js +++ b/extensions/default/src/utils/getDirectURL.test.ts @@ -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, []>; + } + + 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, []>; + } + + 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); }); });