Skip to content

Commit ef6880f

Browse files
committed
add unit test for fetchVipMetadataInformation method
1 parent 838957b commit ef6880f

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

src/App.vue

+13-19
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,16 @@ export default defineComponent({
202202
}
203203
},
204204
// vue js lifecylce functions
205-
async created() {
206-
// get resource, ensure resource url is not empty!
207-
if (this.url != null && this.url != undefined && this.url != '') {
208-
this.dicomUrl = await this.addWadouriPrefix(this.url)
209-
}
210-
211-
// get vip metadata
212-
await this.fetchVipMetadataInformation(await this.addWadouriPrefix(this.url))
213205
214-
// prefetch all other metadata (in separate function for performance reasons)
215-
await this.fetchMetadataInformation(await this.addWadouriPrefix(this.url))
216-
},
217206
async mounted() {
207+
if (this.url) {
208+
this.dicomUrl = await this.addWadouriPrefix(this.url)
209+
// get vip metadata
210+
await this.fetchVipMetadataInformation(this.dicomUrl)
211+
212+
// prefetch all other metadata (in separate function for performance reasons)
213+
await this.fetchMetadataInformation(this.dicomUrl)
214+
}
218215
// check if cornerstone core is initialized
219216
if (!cornerstone.isCornerstoneInitialized()) {
220217
await this.initCornerstoneCore()
@@ -248,11 +245,9 @@ export default defineComponent({
248245
this.viewport = <Types.IStackViewport>this.renderingEngine.getViewport(viewportId)
249246
250247
// add resource to stack, ensure resource url is not empty!
251-
if (this.url != null && this.url != undefined && this.url != '') {
252-
let dicomResourceUrl = await this.addWadouriPrefix(this.url)
253-
248+
if (this.dicomUrl) {
254249
// define a stack containing a single image
255-
const dicomStack = [dicomResourceUrl]
250+
const dicomStack = [this.dicomUrl]
256251
257252
// set stack on the viewport (currently only one image in the stack, therefore no frame # required)
258253
await this.viewport.setStack(dicomStack)
@@ -262,7 +257,7 @@ export default defineComponent({
262257
this.setViewportCameraParallelScaleFactor()
263258
264259
// getting image metadata from viewport
265-
this.getImageMetadataFromViewport(dicomResourceUrl)
260+
this.getImageMetadataFromViewport(this.dicomUrl)
266261
}
267262
},
268263
updated() {
@@ -277,6 +272,7 @@ export default defineComponent({
277272
this.isImageMetadataExtractedFromViewport = false
278273
this.isDicomImageDataFetched = false
279274
},
275+
280276
methods: {
281277
async initCornerstoneCore() {
282278
try {
@@ -291,11 +287,9 @@ export default defineComponent({
291287
async fetchVipMetadataInformation(imageId) {
292288
if (!this.isDicomImageDataFetched) {
293289
this.dicomImageData = await fetchDicomImageData(imageId)
294-
if (!this.isDicomImageDataFetched) {
295-
if (this.dicomImageData != null) {
290+
if (this.dicomImageData != null){
296291
this.isDicomImageDataFetched = true
297292
}
298-
}
299293
}
300294
301295
this.vipInformation.patientName = this.dicomImageData.string(

tests/unit/App.spec.ts

+47-5
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'
@@ -20,7 +20,13 @@ vi.mock('@cornerstonejs/core', () => {
2020
getViewport() {
2121
return {
2222
setStack: vi.fn(),
23-
render: vi.fn()
23+
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,12 +66,48 @@ 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.spyOn(App.methods, 'addWadouriPrefix').mockImplementation(vi.fn())
76+
const spyInitCornerstoneCore = vi
77+
.spyOn(App.methods, 'initCornerstoneCore')
78+
.mockImplementation(vi.fn())
79+
80+
beforeEach(() => {
81+
vi.clearAllMocks()
82+
})
83+
84+
it('should not fetch overlay metadata when the url is not provided', async () => {
85+
getWrapper()
86+
await flushPromises()
87+
88+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(0)
89+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(0)
90+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(0)
91+
expect(spyInitCornerstoneCore).toHaveBeenCalledTimes(1)
92+
})
93+
it('should fetch overlay metadata when the url is provided', async () => {
94+
getWrapper({ props: { url: 'https://test' } })
95+
await flushPromises()
96+
97+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(1)
98+
expect(spyAddWadouriPrefix).toHaveBeenCalledWith('https://test')
99+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(1)
100+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(1)
101+
expect(spyInitCornerstoneCore).toHaveBeenCalledTimes(1)
102+
})
103+
63104
describe('Methods', () => {
64-
vi.spyOn(App.methods, 'fetchVipMetadataInformation').mockImplementation(vi.fn())
65-
vi.spyOn(App.methods, 'fetchMetadataInformation').mockImplementation(vi.fn())
66105
const wrapper = getWrapper()
67-
68106
describe('method: wadouri', () => {
107+
beforeAll(() => {
108+
spyAddWadouriPrefix.mockRestore()
109+
})
110+
69111
it('should add "wadouri" prefix', async () => {
70112
expect(await wrapper.vm.addWadouriPrefix('https://dummy_url')).toBe(
71113
'wadouri:https://dummy_url'

0 commit comments

Comments
 (0)