Skip to content

Commit 691864c

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

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/App.vue

+5-8
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,12 @@ export default defineComponent({
226226
// get resource, ensure resource url is not empty!
227227
if (this.url != null && this.url != undefined && this.url != '') {
228228
this.dicomUrl = await this.addWadouriPrefix(this.url)
229-
}
230-
231-
// get vip metadata
232-
await this.fetchVipMetadataInformation(await this.addWadouriPrefix(this.url))
229+
// get vip metadata
230+
await this.fetchVipMetadataInformation(this.dicomUrl)
233231
234-
// prefetch all other metadata (in separate function for performance reasons)
235-
await this.fetchMetadataInformation(await this.addWadouriPrefix(this.url))
232+
// prefetch all other metadata (in separate function for performance reasons)
233+
await this.fetchMetadataInformation(this.dicomUrl)
234+
}
236235
},
237236
async mounted() {
238237
// check if cornerstone core is initialized
@@ -312,11 +311,9 @@ export default defineComponent({
312311
313312
if (!this.isDicomImageDataFetched) {
314313
this.dicomImageData = await fetchDicomImageData(imageId)
315-
if (!this.isDicomImageDataFetched) {
316314
if (this.dicomImageData != null){
317315
this.isDicomImageDataFetched = true
318316
}
319-
}
320317
}
321318
322319
this.vipInformation.patientName = this.dicomImageData.string(findDicomTagByValue('patientName'))

tests/unit/App.spec.ts

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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";
@@ -21,6 +21,12 @@ vi.mock("@cornerstonejs/core", () => {
2121
return {
2222
setStack: vi.fn(),
2323
render: vi.fn(),
24+
getCamera: vi.fn().mockImplementation(() => {
25+
return { parallelScale: 137.3853139193763 };
26+
}),
27+
getImageData: vi.fn().mockImplementation(() => {
28+
return { dimensions: [] };
29+
}),
2430
};
2531
}
2632
enableElement() {}
@@ -60,16 +66,40 @@ vi.mock("dicom-parser", () => ({
6066
}));
6167

6268
describe("App component", () => {
69+
const spyFetchVipMetadataInformation = vi
70+
.spyOn(App.methods, "fetchVipMetadataInformation")
71+
.mockImplementation(vi.fn());
72+
const spyFetchMetadataInformation = vi
73+
.spyOn(App.methods, "fetchMetadataInformation")
74+
.mockImplementation(vi.fn());
75+
const spyAddWadouriPrefix = vi
76+
.spyOn(App.methods, "addWadouriPrefix")
77+
.mockImplementation(vi.fn());
78+
79+
it("should not fetch overlay metadata when the url is not provided", async () => {
80+
getWrapper();
81+
await flushPromises();
82+
83+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(0);
84+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(0);
85+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(0);
86+
});
87+
it("should fetch overlay metadata when the url is provided", async () => {
88+
getWrapper({ props: { url: "https://test" } });
89+
await flushPromises();
90+
91+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(2);
92+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(1);
93+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(1);
94+
});
95+
6396
describe("Methods", () => {
64-
vi.spyOn(App.methods, "fetchVipMetadataInformation").mockImplementation(
65-
vi.fn()
66-
);
67-
vi.spyOn(App.methods, "fetchMetadataInformation").mockImplementation(
68-
vi.fn()
69-
);
7097
const wrapper = getWrapper();
71-
7298
describe("method: wadouri", () => {
99+
beforeAll(() => {
100+
spyAddWadouriPrefix.mockRestore();
101+
});
102+
73103
it('should add "wadouri" prefix', async () => {
74104
expect(await wrapper.vm.addWadouriPrefix("https://dummy_url")).toBe(
75105
"wadouri:https://dummy_url"
@@ -124,7 +154,9 @@ describe("App component", () => {
124154
])(
125155
`should return $expected for $description`,
126156
({ date, time, expected }) => {
127-
expect(wrapper.vm.formatOverlayDateAndTime(date, time)).toBe(expected);
157+
expect(wrapper.vm.formatOverlayDateAndTime(date, time)).toBe(
158+
expected
159+
);
128160
}
129161
);
130162
});

0 commit comments

Comments
 (0)