Skip to content

Commit 2d9d1b3

Browse files
committed
test: exercise volume project scopes through HTTP
Signed-off-by: 1fanwang <1fannnw@gmail.com>
1 parent 390c2fe commit 2d9d1b3

1 file changed

Lines changed: 73 additions & 28 deletions

File tree

ui/tests/unit/components/view/VolumesTab.spec.js

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
import { createServer } from 'http'
1819
import { flushPromises } from '@vue/test-utils'
1920

20-
import mockAxios from '../../../mock/mockAxios'
2121
import common from '../../../common'
2222
import VolumesTab from '@/components/view/VolumesTab.vue'
23+
import { axios } from '@/utils/request'
24+
import { CURRENT_PROJECT } from '@/store/mutation-types'
25+
import { vueProps } from '@/vue-app'
2326

24-
jest.mock('axios', () => mockAxios)
2527
jest.mock('@/vue-app', () => ({
2628
vueProps: {
2729
$localStorage: {
@@ -32,10 +34,43 @@ jest.mock('@/vue-app', () => ({
3234

3335
const router = common.createMockRouter()
3436
const i18n = common.createMockI18n('en')
37+
const projectVm = { id: 'vm-1', projectid: 'project-1' }
38+
const accountVm = { id: 'vm-2' }
39+
const systemVm = { id: 'vm-3', systemvmtype: 'secondarystoragevm' }
40+
let requests = []
41+
const server = createServer((request, response) => {
42+
const params = Object.fromEntries(new URL(request.url, 'http://localhost').searchParams)
43+
requests.push(params)
44+
const vm = [projectVm, accountVm, systemVm].find(vm => vm.id === params.virtualmachineid)
45+
const volume = vm && (
46+
(params.projectid === '-1' && params.listall === 'true') ||
47+
params.projectid === vm.projectid
48+
)
49+
? [
50+
{ id: 'data-volume', name: 'Data volume', deviceid: 1 },
51+
{ id: 'root-volume', name: 'Root volume', deviceid: 0 }
52+
]
53+
: []
54+
response.setHeader('Content-Type', 'application/json')
55+
response.end(JSON.stringify({ listvolumesresponse: { count: volume.length, volume } }))
56+
})
3557

3658
describe('Components > View > VolumesTab.vue', () => {
59+
beforeAll(async () => {
60+
await new Promise((resolve, reject) => {
61+
server.once('error', reject)
62+
server.listen(0, '127.0.0.1', resolve)
63+
})
64+
axios.defaults.baseURL = `http://127.0.0.1:${server.address().port}`
65+
axios.defaults.adapter = require('axios/lib/adapters/http')
66+
})
67+
68+
afterAll(() => new Promise(resolve => server.close(resolve)))
69+
3770
beforeEach(() => {
71+
requests = []
3872
jest.clearAllMocks()
73+
vueProps.$localStorage.get.mockReturnValue(null)
3974
jest.spyOn(console, 'warn').mockImplementation(() => {})
4075
})
4176

@@ -63,48 +98,58 @@ describe('Components > View > VolumesTab.vue', () => {
6398

6499
expect(wrapper.text()).toContain('2.00 GiB')
65100
expect(wrapper.text()).not.toContain('2.00 GB')
66-
expect(mockAxios).not.toHaveBeenCalled()
101+
expect(requests).toHaveLength(0)
67102

68103
wrapper.unmount()
69104
})
70105

71106
it.each([
72-
['project instance', { id: 'vm-1', projectid: 'project-1' }],
73-
['account instance', { id: 'vm-2' }],
74-
['system VM', { id: 'vm-3', systemvmtype: 'secondarystoragevm' }]
75-
])('requests volumes across project scopes for %s', async (name, resource) => {
76-
mockAxios.mockResolvedValue({
77-
listvolumesresponse: {
78-
volume: [
79-
{ id: 'data-volume', name: 'Data volume', deviceid: 1 },
80-
{ id: 'root-volume', name: 'Root volume', deviceid: 0 }
81-
]
82-
}
107+
['project instance in Default view', projectVm, null],
108+
['project instance in its project', projectVm, projectVm.projectid],
109+
['project instance in another project', projectVm, 'project-2'],
110+
['account instance in Default view', accountVm, null],
111+
['account instance in another project', accountVm, 'project-2'],
112+
['system VM in Default view', systemVm, null],
113+
['system VM in another project', systemVm, 'project-2']
114+
])('shows volume rows for %s', async (name, resource, selectedProjectId) => {
115+
vueProps.$localStorage.get.mockImplementation(key => {
116+
return key === CURRENT_PROJECT && selectedProjectId ? { id: selectedProjectId } : null
117+
})
118+
let interceptor
119+
const response = new Promise((resolve, reject) => {
120+
interceptor = axios.interceptors.response.use(data => {
121+
resolve(data)
122+
return data
123+
}, error => {
124+
reject(error)
125+
return Promise.reject(error)
126+
})
83127
})
84128
const wrapper = common.createFactory(VolumesTab, {
85129
router,
86130
i18n,
87131
props: { resource }
88132
})
89133

90-
await flushPromises()
134+
try {
135+
await response
136+
await flushPromises()
91137

92-
expect(mockAxios).toHaveBeenCalledWith({
93-
url: '/',
94-
method: 'GET',
95-
params: {
138+
expect(wrapper.findAll('tbody tr[data-row-key]')).toHaveLength(2)
139+
expect(requests).toEqual([{
96140
command: 'listVolumes',
97141
response: 'json',
98-
listall: true,
99-
listsystemvms: true,
142+
listall: 'true',
143+
listsystemvms: 'true',
100144
projectid: '-1',
101145
virtualmachineid: resource.id
102-
}
103-
})
104-
expect(wrapper.vm.volumes.map(volume => volume.id)).toEqual(['root-volume', 'data-volume'])
105-
expect(wrapper.text()).toContain('Root volume')
106-
expect(wrapper.text()).toContain('Data volume')
107-
108-
wrapper.unmount()
146+
}])
147+
expect(wrapper.vm.volumes.map(volume => volume.id)).toEqual(['root-volume', 'data-volume'])
148+
expect(wrapper.text()).toContain('Root volume')
149+
expect(wrapper.text()).toContain('Data volume')
150+
} finally {
151+
wrapper.unmount()
152+
axios.interceptors.response.eject(interceptor)
153+
}
109154
})
110155
})

0 commit comments

Comments
 (0)