Skip to content

Commit 6de8dd7

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

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

src/App.vue

+12-18
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,16 @@ export default defineComponent({
222222
}
223223
},
224224
// vue js lifecylce functions
225-
async created() {
226-
// get resource, ensure resource url is not empty!
227-
if (this.url != null && this.url != undefined && this.url != '') {
228-
this.dicomUrl = await this.addWadouriPrefix(this.url)
229-
}
230-
231-
// get vip metadata
232-
await this.fetchVipMetadataInformation(await this.addWadouriPrefix(this.url))
233225
234-
// prefetch all other metadata (in separate function for performance reasons)
235-
await this.fetchMetadataInformation(await this.addWadouriPrefix(this.url))
236-
},
237226
async mounted() {
227+
if (this.url) {
228+
this.dicomUrl = await this.addWadouriPrefix(this.url)
229+
// get vip metadata
230+
await this.fetchVipMetadataInformation(this.dicomUrl)
231+
232+
// prefetch all other metadata (in separate function for performance reasons)
233+
await this.fetchMetadataInformation(this.dicomUrl)
234+
}
238235
// check if cornerstone core is initialized
239236
if (!cornerstone.isCornerstoneInitialized()) {
240237
await this.initCornerstoneCore()
@@ -268,11 +265,9 @@ export default defineComponent({
268265
this.viewport = <Types.IStackViewport>this.renderingEngine.getViewport(viewportId)
269266
270267
// 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)
273-
268+
if (this.dicomUrl) {
274269
// define a stack containing a single image
275-
const dicomStack = [dicomResourceUrl]
270+
const dicomStack = [this.dicomUrl]
276271
277272
// set stack on the viewport (currently only one image in the stack, therefore no frame # required)
278273
await this.viewport.setStack(dicomStack)
@@ -282,7 +277,7 @@ export default defineComponent({
282277
this.setViewportCameraParallelScaleFactor()
283278
284279
// getting image metadata from viewport
285-
this.getImageMetadataFromViewport(dicomResourceUrl)
280+
this.getImageMetadataFromViewport(this.dicomUrl)
286281
}
287282
},
288283
updated() {
@@ -297,6 +292,7 @@ export default defineComponent({
297292
this.isImageMetadataExtractedFromViewport = false
298293
this.isDicomImageDataFetched = false
299294
},
295+
300296
methods: {
301297
async initCornerstoneCore() {
302298
try {
@@ -312,11 +308,9 @@ export default defineComponent({
312308
313309
if (!this.isDicomImageDataFetched) {
314310
this.dicomImageData = await fetchDicomImageData(imageId)
315-
if (!this.isDicomImageDataFetched) {
316311
if (this.dicomImageData != null){
317312
this.isDicomImageDataFetched = true
318313
}
319-
}
320314
}
321315
322316
this.vipInformation.patientName = this.dicomImageData.string(findDicomTagByValue('patientName'))

tests/unit/App.spec.ts

+46-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,45 @@ 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+
const spyInitCornerstoneCore = vi
79+
.spyOn(App.methods, "initCornerstoneCore")
80+
.mockImplementation(vi.fn());
81+
82+
it("should not fetch overlay metadata when the url is not provided", async () => {
83+
getWrapper();
84+
await flushPromises();
85+
86+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(0);
87+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(0);
88+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(0);
89+
expect(spyInitCornerstoneCore).toHaveBeenCalled();
90+
});
91+
it("should fetch overlay metadata when the url is provided", async () => {
92+
getWrapper({ props: { url: "https://test" } });
93+
await flushPromises();
94+
95+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(1);
96+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(1);
97+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(1);
98+
expect(spyInitCornerstoneCore).toHaveBeenCalled();
99+
});
100+
63101
describe("Methods", () => {
64-
vi.spyOn(App.methods, "fetchVipMetadataInformation").mockImplementation(
65-
vi.fn()
66-
);
67-
vi.spyOn(App.methods, "fetchMetadataInformation").mockImplementation(
68-
vi.fn()
69-
);
70102
const wrapper = getWrapper();
71-
72103
describe("method: wadouri", () => {
104+
beforeAll(() => {
105+
spyAddWadouriPrefix.mockRestore();
106+
});
107+
73108
it('should add "wadouri" prefix', async () => {
74109
expect(await wrapper.vm.addWadouriPrefix("https://dummy_url")).toBe(
75110
"wadouri:https://dummy_url"
@@ -124,7 +159,9 @@ describe("App component", () => {
124159
])(
125160
`should return $expected for $description`,
126161
({ date, time, expected }) => {
127-
expect(wrapper.vm.formatOverlayDateAndTime(date, time)).toBe(expected);
162+
expect(wrapper.vm.formatOverlayDateAndTime(date, time)).toBe(
163+
expected
164+
);
128165
}
129166
);
130167
});

0 commit comments

Comments
 (0)