Skip to content

Commit 9f88909

Browse files
Merge pull request #36 from owncloud/unit-test-App-component
Add unit test for fetchVipMetadataInformation method
2 parents 838957b + a8995a7 commit 9f88909

File tree

4 files changed

+82
-35
lines changed

4 files changed

+82
-35
lines changed

src/App.vue

+14-20
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-
}
210205
211-
// get vip metadata
212-
await this.fetchVipMetadataInformation(await this.addWadouriPrefix(this.url))
213-
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,10 +287,8 @@ 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) {
296-
this.isDicomImageDataFetched = true
297-
}
290+
if (this.dicomImageData != null) {
291+
this.isDicomImageDataFetched = true
298292
}
299293
}
300294

src/helper/extractMetadata.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ export const addSopTagChecker = (tag: string): boolean => {
3131
return tag.endsWith('_addSOPuids') ? true : false
3232
}
3333

34-
export const extractDicomMetadata = async (
35-
imageData: object,
36-
tags: string[],
37-
language: string = 'en'
38-
) => {
34+
export const extractDicomMetadata = async (imageData: object, tags: string[], language = 'en') => {
3935
const extractedData: { label: string; value: string }[] = []
4036

4137
// extracting data

tests/e2e/hooks.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ After(async function (): Promise<void> {
4141

4242
const sendRequest = function ({ method, path }): Promise<any> {
4343
const headers = {
44-
Authorization: `Basic ${Buffer.from(`${config.adminUser}:${config.adminPassword}`).toString('base64')}`
44+
Authorization: `Basic ${Buffer.from(`${config.adminUser}:${config.adminPassword}`).toString(
45+
'base64'
46+
)}`
4547
}
4648
return axios({
4749
method,

tests/unit/App.spec.ts

+64-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'
@@ -14,24 +14,39 @@ vi.mock('vue3-gettext', () => ({
1414

1515
vi.mock('@cornerstonejs/core', () => {
1616
return {
17-
successCallback: vi.fn(),
18-
errorCallback: vi.fn(),
1917
RenderingEngine: class RenderingEngine {
2018
getViewport() {
2119
return {
2220
setStack: vi.fn(),
23-
render: vi.fn()
21+
render: vi.fn(),
22+
getCamera: vi.fn().mockImplementation(() => {
23+
return { parallelScale: 137.3853139193763 }
24+
}),
25+
getImageData: vi.fn().mockImplementation(() => {
26+
return { dimensions: [] }
27+
})
2428
}
2529
}
2630
enableElement() {}
2731
},
2832
Types: vi.fn(),
2933
Enums: {
3034
ViewportType: {
31-
STACK: '' // "stack",
35+
STACK: ''
3236
}
3337
},
34-
metaData: vi.fn(),
38+
metaData: {
39+
get: vi.fn().mockImplementation(() => {
40+
return {
41+
pixelRepresentation: '',
42+
bitsAllocated: '',
43+
bitsStored: '',
44+
highBit: '',
45+
samplesPerPixel: ''
46+
}
47+
})
48+
},
49+
3550
init: vi.fn(),
3651
getConfiguration: vi.fn().mockImplementation(() => {
3752
return { rendering: '' }
@@ -60,12 +75,52 @@ vi.mock('dicom-parser', () => ({
6075
}))
6176

6277
describe('App component', () => {
78+
const spyFetchVipMetadataInformation = vi
79+
.spyOn(App.methods, 'fetchVipMetadataInformation')
80+
.mockImplementation(vi.fn())
81+
const spyFetchMetadataInformation = vi
82+
.spyOn(App.methods, 'fetchMetadataInformation')
83+
.mockImplementation(vi.fn())
84+
const spyAddWadouriPrefix = vi
85+
.spyOn(App.methods, 'addWadouriPrefix')
86+
.mockReturnValue('wadouri:https://test')
87+
const spyInitCornerstoneCore = vi
88+
.spyOn(App.methods, 'initCornerstoneCore')
89+
.mockImplementation(vi.fn())
90+
91+
beforeEach(() => {
92+
vi.clearAllMocks()
93+
})
94+
95+
it('should not fetch overlay metadata when the url is not provided', async () => {
96+
getWrapper()
97+
await flushPromises()
98+
99+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(0)
100+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(0)
101+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(0)
102+
expect(spyInitCornerstoneCore).toHaveBeenCalledTimes(1)
103+
})
104+
it('should fetch overlay metadata when the url is provided', async () => {
105+
getWrapper({ props: { url: 'https://test' } })
106+
await flushPromises()
107+
108+
expect(spyAddWadouriPrefix).toHaveBeenCalledTimes(1)
109+
expect(spyAddWadouriPrefix).toHaveBeenCalledWith('https://test')
110+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledTimes(1)
111+
expect(spyFetchVipMetadataInformation).toHaveBeenCalledWith('wadouri:https://test')
112+
expect(spyFetchMetadataInformation).toHaveBeenCalledTimes(1)
113+
expect(spyFetchMetadataInformation).toHaveBeenCalledWith('wadouri:https://test')
114+
expect(spyInitCornerstoneCore).toHaveBeenCalledTimes(1)
115+
})
116+
63117
describe('Methods', () => {
64-
vi.spyOn(App.methods, 'fetchVipMetadataInformation').mockImplementation(vi.fn())
65-
vi.spyOn(App.methods, 'fetchMetadataInformation').mockImplementation(vi.fn())
66118
const wrapper = getWrapper()
67-
68119
describe('method: wadouri', () => {
120+
beforeAll(() => {
121+
spyAddWadouriPrefix.mockRestore()
122+
})
123+
69124
it('should add "wadouri" prefix', async () => {
70125
expect(await wrapper.vm.addWadouriPrefix('https://dummy_url')).toBe(
71126
'wadouri:https://dummy_url'

0 commit comments

Comments
 (0)