Skip to content

Commit 2ec4f16

Browse files
committed
fix tests
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 5663c1b commit 2ec4f16

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

ui/src/views/compute/MigrateWizard.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class="top-spaced"
2727
:placeholder="$t('label.search')"
2828
v-model:value="searchQuery"
29-
@search="fetchData"
29+
@search="fetchHostsForMigration"
3030
v-focus="true" />
3131
<a-table
3232
class="top-spaced"
@@ -97,7 +97,7 @@
9797
</a-pagination>
9898

9999
<a-form-item
100-
v-if="isUserVm"
100+
v-if="isUserVm && hasVolumes"
101101
class="top-spaced">
102102
<template #label>
103103
<tooltip-label :title="$t('label.migrate.with.storage')" :tooltip="$t('message.migrate.with.storage')"/>
@@ -221,6 +221,9 @@ export default {
221221
computed: {
222222
isUserVm () {
223223
return this.$route.meta.resourceType === 'UserVm'
224+
},
225+
hasVolumes () {
226+
return this.volumes && this.volumes.length > 0
224227
}
225228
},
226229
watch: {
@@ -235,11 +238,11 @@ export default {
235238
return array !== null && array !== undefined && Array.isArray(array) && array.length > 0
236239
},
237240
fetchData () {
238-
this.loading = true
239241
this.fetchHostsForMigration()
240242
this.fetchVolumes()
241243
},
242244
fetchHostsForMigration () {
245+
this.loading = true
243246
api('findHostsForMigration', {
244247
virtualmachineid: this.resource.id,
245248
keyword: this.searchQuery,
@@ -266,12 +269,12 @@ export default {
266269
handleChangePage (page, pageSize) {
267270
this.page = page
268271
this.pageSize = pageSize
269-
this.fetchData()
272+
this.fetchHostsForMigration()
270273
},
271274
handleChangePageSize (currentPage, pageSize) {
272275
this.page = currentPage
273276
this.pageSize = pageSize
274-
this.fetchData()
277+
this.fetchHostsForMigration()
275278
},
276279
handleSelectedHostChange (host) {
277280
if (host.id === -1) {
@@ -305,7 +308,7 @@ export default {
305308
listAll: true,
306309
virtualmachineid: this.resource.id
307310
}).then(response => {
308-
this.volumes = response.listvolumesresponse.volume
311+
this.volumes = response?.listvolumesresponse?.volume || []
309312
}).finally(() => {
310313
this.loading = false
311314
})
@@ -314,7 +317,7 @@ export default {
314317
if (this.selectedHost.requiresStorageMotion || this.volumeToPoolSelection.length > 0) {
315318
return true
316319
}
317-
if (this.selectedHost.id === -1 && this.volumes && this.volumes.length > 0) {
320+
if (this.selectedHost.id === -1 && this.hasVolumes) {
318321
for (var volume of this.volumes) {
319322
if (volume.storagetype === 'local') {
320323
return true
@@ -342,7 +345,7 @@ export default {
342345
var params = this.selectedHost.id === -1
343346
? { autoselect: true, virtualmachineid: this.resource.id }
344347
: { hostid: this.selectedHost.id, virtualmachineid: this.resource.id }
345-
if (this.migrateWithStorage) {
348+
if (this.migrateWithStorage && this.volumeToPoolSelection && this.volumeToPoolSelection.length > 0) {
346349
for (var i = 0; i < this.volumeToPoolSelection.length; i++) {
347350
const mapping = this.volumeToPoolSelection[i]
348351
params['migrateto[' + i + '].volume'] = mapping.volume

ui/tests/unit/views/compute/MigrateWizard.spec.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ describe('Views > compute > MigrateWizard.vue', () => {
126126
if (Object.keys(originalFunc).length > 0) {
127127
Object.keys(originalFunc).forEach(key => {
128128
switch (key) {
129-
case 'fetchData':
130-
wrapper.vm.fetchData = originalFunc[key]
129+
case 'fetchHostsForMigration':
130+
wrapper.vm.fetchHostsForMigration = originalFunc[key]
131131
break
132132
default:
133133
break
@@ -137,11 +137,11 @@ describe('Views > compute > MigrateWizard.vue', () => {
137137
})
138138

139139
describe('Methods', () => {
140-
describe('fetchData()', () => {
140+
describe('fetchHostsForMigration()', () => {
141141
it('API should be called with resource is empty and searchQuery is empty', async (done) => {
142142
await mockAxios.mockResolvedValue({ findhostsformigrationresponse: { count: 0, host: [] } })
143143
await wrapper.setProps({ resource: {} })
144-
await wrapper.vm.fetchData()
144+
await wrapper.vm.fetchHostsForMigration()
145145
await flushPromises()
146146

147147
expect(mockAxios).toHaveBeenCalled()
@@ -164,7 +164,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
164164
it('API should be called with resource.id is empty and searchQuery is empty', async (done) => {
165165
await mockAxios.mockResolvedValue({ findhostsformigrationresponse: { count: 0, host: [] } })
166166
await wrapper.setProps({ resource: { id: null } })
167-
await wrapper.vm.fetchData()
167+
await wrapper.vm.fetchHostsForMigration()
168168
await flushPromises()
169169

170170
expect(mockAxios).toHaveBeenCalled()
@@ -187,7 +187,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
187187
it('API should be called with resource.id is not empty and searchQuery is empty', async (done) => {
188188
await mockAxios.mockResolvedValue({ findhostsformigrationresponse: { count: 0, host: [] } })
189189
await wrapper.setProps({ resource: { id: 'test-id-value' } })
190-
await wrapper.vm.fetchData()
190+
await wrapper.vm.fetchHostsForMigration()
191191
await flushPromises()
192192

193193
expect(mockAxios).toHaveBeenCalled()
@@ -211,7 +211,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
211211
await mockAxios.mockResolvedValue({ findhostsformigrationresponse: { count: 0, host: [] } })
212212
await wrapper.setProps({ resource: { id: 'test-id-value' } })
213213
await wrapper.setData({ searchQuery: 'test-query-value' })
214-
await wrapper.vm.fetchData()
214+
await wrapper.vm.fetchHostsForMigration()
215215
await flushPromises()
216216

217217
expect(mockAxios).toHaveBeenCalled()
@@ -239,7 +239,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
239239
page: 2,
240240
pageSize: 20
241241
})
242-
await wrapper.vm.fetchData()
242+
await wrapper.vm.fetchHostsForMigration()
243243
await flushPromises()
244244

245245
expect(mockAxios).toHaveBeenCalled()
@@ -262,7 +262,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
262262
it('check hosts, totalCount when api is called with response result is empty', async (done) => {
263263
await mockAxios.mockResolvedValue({ findhostsformigrationresponse: { count: 0, host: [] } })
264264
await wrapper.setProps({ resource: {} })
265-
await wrapper.vm.fetchData()
265+
await wrapper.vm.fetchHostsForMigration()
266266
await flushPromises()
267267

268268
expect(wrapper.vm.hosts).toEqual([])
@@ -285,7 +285,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
285285
}
286286
})
287287
await wrapper.setProps({ resource: {} })
288-
await wrapper.vm.fetchData()
288+
await wrapper.vm.fetchHostsForMigration()
289289
await flushPromises()
290290

291291
expect(wrapper.vm.hosts).toEqual([{
@@ -305,7 +305,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
305305

306306
await mockAxios.mockRejectedValue(mockError)
307307
await wrapper.setProps({ resource: {} })
308-
await wrapper.vm.fetchData()
308+
await wrapper.vm.fetchHostsForMigration()
309309
await flushPromises()
310310
await flushPromises()
311311

@@ -543,14 +543,14 @@ describe('Views > compute > MigrateWizard.vue', () => {
543543
await mockAxios.mockResolvedValue(mockData)
544544
await wrapper.setProps({
545545
resource: {
546-
id: 'test-resource-id',
546+
id: 'test-resource-id-err',
547547
name: 'test-resource-name'
548548
}
549549
})
550550
await wrapper.setData({
551551
selectedHost: {
552552
requiresStorageMotion: true,
553-
id: 'test-host-id',
553+
id: 'test-host-id-err',
554554
name: 'test-host-name'
555555
}
556556
})
@@ -572,14 +572,14 @@ describe('Views > compute > MigrateWizard.vue', () => {
572572
await mockAxios.mockResolvedValue(mockData)
573573
await wrapper.setProps({
574574
resource: {
575-
id: 'test-resource-id',
575+
id: 'test-resource-id-catch',
576576
name: 'test-resource-name'
577577
}
578578
})
579579
await wrapper.setData({
580580
selectedHost: {
581581
requiresStorageMotion: true,
582-
id: 'test-host-id',
582+
id: 'test-host-id-catch',
583583
name: 'test-host-name'
584584
}
585585
})
@@ -599,7 +599,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
599599
await wrapper.setData({
600600
selectedHost: {
601601
requiresStorageMotion: true,
602-
id: 'test-host-id',
602+
id: 'test-host-id-no-res',
603603
name: 'test-host-name'
604604
}
605605
})
@@ -617,11 +617,11 @@ describe('Views > compute > MigrateWizard.vue', () => {
617617
})
618618

619619
describe('handleChangePage()', () => {
620-
it('check page, pageSize and fetchData() when handleChangePage() is called', async (done) => {
621-
originalFunc.fetchData = wrapper.vm.fetchData
622-
wrapper.vm.fetchData = jest.fn()
620+
it('check page, pageSize and fetchHostsForMigration() when handleChangePage() is called', async (done) => {
621+
originalFunc.fetchHostsForMigration = wrapper.vm.fetchHostsForMigration
622+
wrapper.vm.fetchHostsForMigration = jest.fn()
623623

624-
const fetchData = jest.spyOn(wrapper.vm, 'fetchData').mockImplementation(() => {})
624+
const fetchHostsForMigration = jest.spyOn(wrapper.vm, 'fetchHostsForMigration').mockImplementation(() => {})
625625
await wrapper.setProps({ resource: {} })
626626
await wrapper.setData({
627627
page: 1,
@@ -632,17 +632,17 @@ describe('Views > compute > MigrateWizard.vue', () => {
632632

633633
expect(wrapper.vm.page).toEqual(2)
634634
expect(wrapper.vm.pageSize).toEqual(20)
635-
expect(fetchData).toBeCalled()
635+
expect(fetchHostsForMigration).toBeCalled()
636636
done()
637637
})
638638
})
639639

640640
describe('handleChangePageSize()', () => {
641-
it('check page, pageSize and fetchData() when handleChangePageSize() is called', async (done) => {
642-
originalFunc.fetchData = wrapper.vm.fetchData
643-
wrapper.vm.fetchData = jest.fn()
641+
it('check page, pageSize and fetchHostsForMigration() when handleChangePageSize() is called', async (done) => {
642+
originalFunc.fetchHostsForMigration = wrapper.vm.fetchHostsForMigration
643+
wrapper.vm.fetchHostsForMigration = jest.fn()
644644

645-
const fetchData = jest.spyOn(wrapper.vm, 'fetchData').mockImplementation(() => {})
645+
const fetchHostsForMigration = jest.spyOn(wrapper.vm, 'fetchHostsForMigration').mockImplementation(() => {})
646646
await wrapper.setProps({ resource: {} })
647647
await wrapper.setData({
648648
page: 1,
@@ -653,7 +653,7 @@ describe('Views > compute > MigrateWizard.vue', () => {
653653

654654
expect(wrapper.vm.page).toEqual(2)
655655
expect(wrapper.vm.pageSize).toEqual(20)
656-
expect(fetchData).toBeCalled()
656+
expect(fetchHostsForMigration).toBeCalled()
657657
done()
658658
})
659659
})

0 commit comments

Comments
 (0)