Skip to content

Commit faac39a

Browse files
committed
add unit test for fetchVipMetadataInformation method
1 parent 7571778 commit faac39a

File tree

2 files changed

+102
-23
lines changed

2 files changed

+102
-23
lines changed

src/App.vue

+12-13
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export default defineComponent({
210210
instanceCreationDate: '',
211211
instanceCreationTime: '',
212212
},
213+
// url: null,
213214
patientInformation: [],
214215
studyInformation: [],
215216
seriesInformation: [],
@@ -226,13 +227,12 @@ export default defineComponent({
226227
// get resource, ensure resource url is not empty!
227228
if (this.url != null && this.url != undefined && this.url != '') {
228229
this.dicomUrl = await this.addWadouriPrefix(this.url)
229-
}
230-
231-
// get vip metadata
232-
await this.fetchVipMetadataInformation(await this.addWadouriPrefix(this.url))
230+
// get vip metadata
231+
await this.fetchVipMetadataInformation(this.dicomUrl)
233232
234-
// prefetch all other metadata (in separate function for performance reasons)
235-
await this.fetchMetadataInformation(await this.addWadouriPrefix(this.url))
233+
// prefetch all other metadata (in separate function for performance reasons)
234+
await this.fetchMetadataInformation(this.dicomUrl)
235+
}
236236
},
237237
async mounted() {
238238
// check if cornerstone core is initialized
@@ -268,11 +268,10 @@ export default defineComponent({
268268
this.viewport = <Types.IStackViewport>this.renderingEngine.getViewport(viewportId)
269269
270270
// add resource to stack, ensure resource url is not empty!
271-
if (this.url != null && this.url != undefined && this.url != '') {
272-
let dicomResourceUrl = await this.addWadouriPrefix(this.url)
271+
if (this.dicomUrl){
273272
274273
// define a stack containing a single image
275-
const dicomStack = [dicomResourceUrl]
274+
const dicomStack = [this.dicomUrl]
276275
277276
// set stack on the viewport (currently only one image in the stack, therefore no frame # required)
278277
await this.viewport.setStack(dicomStack)
@@ -282,7 +281,7 @@ export default defineComponent({
282281
this.setViewportCameraParallelScaleFactor()
283282
284283
// getting image metadata from viewport
285-
this.getImageMetadataFromViewport(dicomResourceUrl)
284+
this.getImageMetadataFromViewport(this.dicomUrl)
286285
}
287286
},
288287
updated() {
@@ -291,7 +290,7 @@ export default defineComponent({
291290
// this.setViewportCameraParallelScaleFactor()
292291
},
293292
beforeUnmount() {
294-
this.renderingEngine.destroy()
293+
this.renderingEngine.destroy
295294
this.isVipMetadataFetched = false
296295
this.isMetadataFetched = false
297296
this.isImageMetadataExtractedFromViewport = false
@@ -312,11 +311,11 @@ export default defineComponent({
312311
313312
if (!this.isDicomImageDataFetched) {
314313
this.dicomImageData = await fetchDicomImageData(imageId)
315-
if (!this.isDicomImageDataFetched) {
314+
// if (!this.isDicomImageDataFetched) {
316315
if (this.dicomImageData != null){
317316
this.isDicomImageDataFetched = true
318317
}
319-
}
318+
// }
320319
}
321320
322321
this.vipInformation.patientName = this.dicomImageData.string(findDicomTagByValue('patientName'))

tests/unit/App.spec.ts

+90-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { shallowMount } from "@vue/test-utils";
1+
import { shallowMount, flushPromises } from "@vue/test-utils";
22
import App from "../../src/App.vue";
33
import { vi } from "vitest";
44
import { defaultPlugins } from "../../src/helper/defaultPlugins";
5+
import * as ExtractMetadata from "../../src/helper/extractMetadata";
6+
import { url } from "inspector";
57

68
vi.mock("vue3-gettext", () => ({
79
useGettext: vi.fn().mockImplementation((text) => {
@@ -21,6 +23,12 @@ vi.mock("@cornerstonejs/core", () => {
2123
return {
2224
setStack: vi.fn(),
2325
render: vi.fn(),
26+
getCamera: vi.fn().mockImplementation(() => {
27+
return { parallelScale: 137.3853139193763 };
28+
}),
29+
getImageData: vi.fn().mockImplementation(() => {
30+
return { dimensions: [] };
31+
}),
2432
};
2533
}
2634
enableElement() {}
@@ -31,7 +39,17 @@ vi.mock("@cornerstonejs/core", () => {
3139
STACK: "", // "stack",
3240
},
3341
},
34-
metaData: vi.fn(),
42+
metaData: {
43+
get: vi.fn().mockImplementation(() => {
44+
return {
45+
pixelRepresentation: "",
46+
bitsAllocated: "",
47+
bitsStored: "",
48+
highBit: "",
49+
samplesPerPixel: "",
50+
};
51+
}),
52+
},
3553
init: vi.fn(),
3654
getConfiguration: vi.fn().mockImplementation(() => {
3755
return { rendering: "" };
@@ -60,16 +78,76 @@ vi.mock("dicom-parser", () => ({
6078
}));
6179

6280
describe("App component", () => {
81+
const spyFetchVipMetadataInformation = vi
82+
.spyOn(App.methods, "fetchVipMetadataInformation")
83+
.mockImplementation(vi.fn());
84+
const spyFetchMetadataInformation = vi
85+
.spyOn(App.methods, "fetchMetadataInformation")
86+
.mockImplementation(vi.fn());
87+
const spyAddWadouriPrefix = vi
88+
.spyOn(App.methods, "addWadouriPrefix")
89+
.mockImplementation(vi.fn());
90+
91+
it("should not fetch overlay metadata", () => {
92+
getWrapper();
93+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(0);
94+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(0);
95+
});
96+
it("should fetch overlay metadata", async () => {
97+
getWrapper({ props: { url: "https://test" } });
98+
await flushPromises();
99+
100+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(1);
101+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(1);
102+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(1);
103+
});
104+
105+
// describe("method: fetchVipMetadataInformation", () => {
106+
// it("should fetch overlay metadata", async () => {
107+
// const options = {
108+
// data() {
109+
// return {
110+
// vipInformation: {
111+
// patientName: "DemoPatient",
112+
// patientBirthdate: "19951025",
113+
// institutionName: "DemoInstitute",
114+
// instanceCreationDate: "20241120",
115+
// instanceCreationTime: "093801",
116+
// },
117+
// };
118+
// },
119+
// };
120+
121+
// const fetchDicomImageData = vi
122+
// .spyOn(ExtractMetadata, "fetchDicomImageData")
123+
// .mockImplementation(vi.fn());
124+
// const findDicomTagByValue = vi
125+
// .spyOn(ExtractMetadata, "findDicomTagByValue")
126+
// .mockImplementation(vi.fn());
127+
// const wrapper = getWrapper(options);
128+
129+
// // wrapper.vm.fetchVipMetadataInformation(
130+
// // "https://host.docker.internal:9200/6b0a0207-3bb6-4a6c-ab8a-e6b8a4beb6c5"
131+
// // );
132+
// console.log(
133+
// "isDicomImageDataFetched: ",
134+
// wrapper.vm.isDicomImageDataFetched
135+
// );
136+
// console.log("dicomImageData: ", wrapper.vm.dicomImageData);
137+
138+
// expect(wrapper.vm.vipInformation.patientName).toBe("DemoPatient");
139+
// expect(wrapper.vm.isDicomImageDataFetched).toBe(true);
140+
// expect(findDicomTagByValue).toHaveBeenCalledTimes(1);
141+
// expect(fetchDicomImageData).toHaveBeenCalled();
142+
// });
143+
// });
63144
describe("Methods", () => {
64-
vi.spyOn(App.methods, "fetchVipMetadataInformation").mockImplementation(
65-
vi.fn()
66-
);
67-
vi.spyOn(App.methods, "fetchMetadataInformation").mockImplementation(
68-
vi.fn()
69-
);
70145
const wrapper = getWrapper();
71-
72146
describe("method: wadouri", () => {
147+
beforeAll(() => {
148+
spyAddWadouriPrefix.mockRestore();
149+
});
150+
73151
it('should add "wadouri" prefix', async () => {
74152
expect(await wrapper.vm.addWadouriPrefix("https://dummy_url")).toBe(
75153
"wadouri:https://dummy_url"
@@ -124,7 +202,9 @@ describe("App component", () => {
124202
])(
125203
`should return $expected for $description`,
126204
({ date, time, expected }) => {
127-
expect(wrapper.vm.formatOverlayDateAndTime(date, time)).toBe(expected);
205+
expect(wrapper.vm.formatOverlayDateAndTime(date, time)).toBe(
206+
expected
207+
);
128208
}
129209
);
130210
});

0 commit comments

Comments
 (0)