Skip to content

Commit 79c4140

Browse files
committed
refactor(tools): fix tests
1 parent cc07869 commit 79c4140

11 files changed

Lines changed: 129 additions & 30 deletions

File tree

packages/core/test/defaultPlanarDataProvider.jest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ActorRenderMode } from '../src/types';
2020
import { createAndCacheVolume } from '../src/loaders/volumeLoader';
2121
import { getGenericViewportPlanarDisplaySet } from '../src/RenderingEngine/GenericViewport/genericViewportDisplaySetAccess';
2222
import { DefaultPlanarDataProvider } from '../src/RenderingEngine/GenericViewport/Planar/DefaultPlanarDataProvider';
23+
import { coreLog } from '../src/utilities/logger';
2324

2425
const DATA_ID = 'display-set-1';
2526

@@ -86,7 +87,12 @@ describe('DefaultPlanarDataProvider', () => {
8687
const dataSetImageIds = makeImageIds(10);
8788
registerDataSet({ imageIds: dataSetImageIds, initialImageIdIndex: 3 });
8889
mockVolume(makeImageIds(10).map((id) => `${id}-other`));
89-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
90+
const warnSpy = jest
91+
.spyOn(
92+
coreLog.getLogger('RenderingEngine', 'DefaultPlanarDataProvider'),
93+
'warn'
94+
)
95+
.mockImplementation(() => {});
9096

9197
try {
9298
const provider = new DefaultPlanarDataProvider();

packages/core/test/utilities/splitImageIdsBy4DTags.jest.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
generateFrameImageId,
44
} from '../../src/utilities/splitImageIdsBy4DTags';
55
import { metaData } from '@cornerstonejs/metadata';
6+
import { logging } from '@cornerstonejs/utils';
67
import {
78
describe,
89
it,
@@ -115,26 +116,36 @@ describe('splitImageIdsBy4DTags - Multiframe 4D Functions', () => {
115116
});
116117

117118
it('should return null when TimeSlotVector length does not match NumberOfFrames', () => {
118-
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
119+
const loggerWarnSpy = jest
120+
.spyOn(
121+
logging.metadataLog.getLogger('utilities.splitImageIdsBy4DTags'),
122+
'warn'
123+
)
124+
.mockImplementation();
119125
mockMetaDataGet.mockReturnValue({
120126
NumberOfFrames: 4,
121127
TimeSlotVector: [1, 1, 2],
122128
});
123129
const result = handleMultiframe4D([baseImageId]);
124130

125131
expect(result).toBeNull();
126-
expect(consoleWarnSpy).toHaveBeenCalledWith(
132+
expect(loggerWarnSpy).toHaveBeenCalledWith(
127133
'TimeSlotVector length does not match NumberOfFrames:',
128134
3,
129135
'vs',
130136
4
131137
);
132138

133-
consoleWarnSpy.mockRestore();
139+
loggerWarnSpy.mockRestore();
134140
});
135141

136142
it('should return null when SliceVector length does not match NumberOfFrames', () => {
137-
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
143+
const loggerWarnSpy = jest
144+
.spyOn(
145+
logging.metadataLog.getLogger('utilities.splitImageIdsBy4DTags'),
146+
'warn'
147+
)
148+
.mockImplementation();
138149
mockMetaDataGet.mockReturnValue({
139150
NumberOfFrames: 4,
140151
TimeSlotVector: [1, 1, 2, 2],
@@ -143,18 +154,23 @@ describe('splitImageIdsBy4DTags - Multiframe 4D Functions', () => {
143154
const result = handleMultiframe4D([baseImageId]);
144155

145156
expect(result).toBeNull();
146-
expect(consoleWarnSpy).toHaveBeenCalledWith(
157+
expect(loggerWarnSpy).toHaveBeenCalledWith(
147158
'SliceVector exists but has invalid length or undefined entries. Expected length:',
148159
4,
149160
'Actual length:',
150161
3
151162
);
152163

153-
consoleWarnSpy.mockRestore();
164+
loggerWarnSpy.mockRestore();
154165
});
155166

156167
it('should return null when SliceVector has undefined entries', () => {
157-
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
168+
const loggerWarnSpy = jest
169+
.spyOn(
170+
logging.metadataLog.getLogger('utilities.splitImageIdsBy4DTags'),
171+
'warn'
172+
)
173+
.mockImplementation();
158174
mockMetaDataGet.mockReturnValue({
159175
NumberOfFrames: 4,
160176
TimeSlotVector: [1, 1, 2, 2],
@@ -163,18 +179,23 @@ describe('splitImageIdsBy4DTags - Multiframe 4D Functions', () => {
163179
const result = handleMultiframe4D([baseImageId]);
164180

165181
expect(result).toBeNull();
166-
expect(consoleWarnSpy).toHaveBeenCalledWith(
182+
expect(loggerWarnSpy).toHaveBeenCalledWith(
167183
'SliceVector exists but has invalid length or undefined entries. Expected length:',
168184
4,
169185
'Actual length:',
170186
4
171187
);
172188

173-
consoleWarnSpy.mockRestore();
189+
loggerWarnSpy.mockRestore();
174190
});
175191

176192
it('should return null when SliceVector is not an array', () => {
177-
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
193+
const loggerWarnSpy = jest
194+
.spyOn(
195+
logging.metadataLog.getLogger('utilities.splitImageIdsBy4DTags'),
196+
'warn'
197+
)
198+
.mockImplementation();
178199
mockMetaDataGet.mockReturnValue({
179200
NumberOfFrames: 4,
180201
TimeSlotVector: [1, 1, 2, 2],
@@ -183,12 +204,12 @@ describe('splitImageIdsBy4DTags - Multiframe 4D Functions', () => {
183204
const result = handleMultiframe4D([baseImageId]);
184205

185206
expect(result).toBeNull();
186-
expect(consoleWarnSpy).toHaveBeenCalledWith(
207+
expect(loggerWarnSpy).toHaveBeenCalledWith(
187208
'SliceVector exists but is not an array. Expected length:',
188209
4
189210
);
190211

191-
consoleWarnSpy.mockRestore();
212+
loggerWarnSpy.mockRestore();
192213
});
193214
});
194215
});

packages/dicomImageLoader/src/__tests__/scalingAndParsing.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import parseImageId from '../imageLoader/wadouri/parseImageId';
1515
import { getTransferSyntaxForContentType } from '../imageLoader/wadors/loadImage';
1616
import isModalityLUTForDisplay from '../imageLoader/isModalityLutForDisplay';
1717
import isNMReconstructable from '../imageLoader/isNMReconstructable';
18+
import { logging } from '@cornerstonejs/utils';
1819
import getOverlayPlaneModule from '../imageLoader/wadors/metaData/getOverlayPlaneModule';
1920
import { getECGModule } from '../imageLoader/wadors/metaData/ECGHelpers';
2021

@@ -643,8 +644,8 @@ describe('getInstanceModule', () => {
643644
});
644645

645646
it('swallows provider errors for a given type and continues with the rest', () => {
646-
const consoleErrorSpy = jest
647-
.spyOn(console, 'error')
647+
const loggerErrorSpy = jest
648+
.spyOn(logging.loaderLog.getLogger('getInstanceModule'), 'error')
648649
.mockImplementation(() => {});
649650
const provider = jest.fn((type: string) => {
650651
if (type === 'badModule') {
@@ -659,7 +660,7 @@ describe('getInstanceModule', () => {
659660
]);
660661

661662
expect(result).toEqual({ Ok: true });
662-
expect(consoleErrorSpy).toHaveBeenCalled();
663-
consoleErrorSpy.mockRestore();
663+
expect(loggerErrorSpy).toHaveBeenCalled();
664+
loggerErrorSpy.mockRestore();
664665
});
665666
});

packages/dicomImageLoader/src/__tests__/wadorsMetaDataPrimitives.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Enums } from '@cornerstonejs/core';
2+
import { logging } from '@cornerstonejs/utils';
23

34
import getValue from '../imageLoader/wadors/metaData/getValue';
45
import getNumberValue from '../imageLoader/wadors/metaData/getNumberValue';
@@ -124,7 +125,9 @@ describe('wadors metadata primitives', () => {
124125
let warnSpy: jest.SpyInstance;
125126

126127
beforeEach(() => {
127-
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
128+
warnSpy = jest
129+
.spyOn(logging.loaderLog.getLogger('wadors'), 'warn')
130+
.mockImplementation(() => undefined);
128131
});
129132

130133
afterEach(() => {

packages/dicomImageLoader/src/__tests__/wadouriDataSetLayer.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { eventTarget } from '@cornerstonejs/core';
3+
import { logging } from '@cornerstonejs/utils';
34

45
import { metadataForDataset } from '../imageLoader/wadouri/metaData/metaDataProvider';
56
import {
@@ -1338,7 +1339,9 @@ describe('wadouri dataSet-layer', () => {
13381339
});
13391340

13401341
it('forces samplesPerPixel to 2 for YBR_FULL_422 photometric interpretation', () => {
1341-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
1342+
const warnSpy = jest
1343+
.spyOn(logging.loaderLog.getLogger('wadouri'), 'warn')
1344+
.mockImplementation(() => {});
13421345
// rows=1, cols=2 -> with samplesPerPixel forced to 2: pixelsPerFrame=4 bytes/frame (8-bit)
13431346
const dataSet = dataSetWithPixelData(0, [1, 2, 3, 4, 5, 6, 7, 8], {
13441347
x00280100: 8,

packages/metadata/test/metaDataAddGetSplit.jest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
afterEach,
88
} from '@jest/globals';
99
import * as metaData from '../src/metaData';
10+
import { logging } from '@cornerstonejs/utils';
1011
import {
1112
addCacheForType,
1213
addWritableCacheForType,
@@ -54,7 +55,12 @@ describe('metaData add/get split', () => {
5455
});
5556

5657
it('returns cached value and warns on duplicate writable add', async () => {
57-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
58+
const warnSpy = jest
59+
.spyOn(
60+
logging.metadataLog.getLogger('utilities.metadataProvider.cacheData'),
61+
'warn'
62+
)
63+
.mockImplementation(() => {});
5864
const downstream = jest.fn(async () => ({ value: 7 }));
5965

6066
addCacheForType(TEST_TYPE);

packages/tools/src/stateManagement/segmentation/SegmentationRenderingEngine.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ jest.mock('@cornerstonejs/core', () => ({
88
getEnabledElementByViewportId: jest.fn(),
99
getRenderingEngines: jest.fn(() => []),
1010
triggerEvent: jest.fn(),
11+
utilities: {
12+
logger: {
13+
toolsLog: {
14+
getLogger: jest.fn(() => ({
15+
debug: jest.fn(),
16+
info: jest.fn(),
17+
warn: jest.fn(),
18+
error: jest.fn(),
19+
setLevel: jest.fn(),
20+
})),
21+
},
22+
},
23+
},
1124
}));
1225

1326
jest.mock('./getSegmentation', () => ({

packages/tools/src/tools/displayTools/Labelmap/labelmapRenderPlan.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ jest.mock('@cornerstonejs/core', () => ({
1818
getVolume: jest.fn(),
1919
},
2020
utilities: {
21+
logger: {
22+
toolsLog: {
23+
getLogger: jest.fn(() => ({
24+
debug: jest.fn(),
25+
info: jest.fn(),
26+
warn: jest.fn(),
27+
error: jest.fn(),
28+
setLevel: jest.fn(),
29+
})),
30+
},
31+
},
2132
uuidv4: jest.fn(() => 'generated-volume-id'),
2233
genericViewportDisplaySetMetadataProvider: {
2334
add: jest.fn(),

packages/tools/test/annotationStateManagement.jest.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ jest.mock('@cornerstonejs/core', () => ({
3232
getEnabledElement: (...args) => mockGetEnabledElement(...args),
3333
getEnabledElementByIds: jest.fn(),
3434
utilities: {
35+
logger: {
36+
toolsLog: {
37+
getLogger: jest.fn(() => ({
38+
debug: jest.fn(),
39+
info: jest.fn(),
40+
warn: jest.fn(),
41+
error: jest.fn(),
42+
setLevel: jest.fn(),
43+
})),
44+
},
45+
},
3546
uuidv4: jest.fn(() => `uuid-${mockUuidCounter++}`),
3647
deepClone: (value) =>
3748
value === undefined ? value : JSON.parse(JSON.stringify(value)),

packages/tools/test/storeToolFilters.jest.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,27 @@
1010
// instances and annotation POJOs, so we mock the stateManagement annotation
1111
// module and the ToolGroupManager rather than pulling in real ones.
1212

13-
jest.mock('@cornerstonejs/core', () => ({
14-
utilities: {
15-
deepClone: (value) => JSON.parse(JSON.stringify(value)),
16-
},
17-
getEnabledElement: jest.fn(),
18-
}));
13+
jest.mock('@cornerstonejs/core', () => {
14+
const toolsLogger = {
15+
debug: jest.fn(),
16+
info: jest.fn(),
17+
warn: jest.fn(),
18+
error: jest.fn(),
19+
setLevel: jest.fn(),
20+
};
21+
22+
return {
23+
utilities: {
24+
logger: {
25+
toolsLog: {
26+
getLogger: jest.fn(() => toolsLogger),
27+
},
28+
},
29+
deepClone: (value) => JSON.parse(JSON.stringify(value)),
30+
},
31+
getEnabledElement: jest.fn(),
32+
};
33+
});
1934

2035
jest.mock('../src/stateManagement/annotation/annotationState', () => ({
2136
getAnnotations: jest.fn(),
@@ -25,7 +40,7 @@ jest.mock('../src/store/ToolGroupManager', () => ({
2540
getToolGroupForViewport: jest.fn(),
2641
}));
2742

28-
import { getEnabledElement } from '@cornerstonejs/core';
43+
import { getEnabledElement, utilities } from '@cornerstonejs/core';
2944
import { getAnnotations } from '../src/stateManagement/annotation/annotationState';
3045
import { getToolGroupForViewport } from '../src/store/ToolGroupManager';
3146

@@ -116,16 +131,14 @@ describe('store/filterToolsWithAnnotationsForElement', () => {
116131
const toolA = createFakeTool('ToolA');
117132
const annotationsA = [createFakeAnnotation()];
118133
getAnnotations.mockReturnValue(annotationsA);
119-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
120134

121135
const result = filterToolsWithAnnotationsForElement(element, [
122136
undefined,
123137
toolA,
124138
]);
125139

126140
expect(result).toEqual([{ tool: toolA, annotations: annotationsA }]);
127-
expect(warnSpy).toHaveBeenCalled();
128-
warnSpy.mockRestore();
141+
expect(utilities.logger.toolsLog.getLogger().warn).toHaveBeenCalled();
129142
});
130143

131144
it('applies filterInteractableAnnotationsForElement when a tool defines it', () => {

0 commit comments

Comments
 (0)