fix: 完善单测demo(同一url不同入参的axios请求内包含多个不同url的axios请求)
This commit is contained in:
24
src/Test.vue
24
src/Test.vue
@@ -48,7 +48,11 @@ export default {
|
||||
tableTitles: [
|
||||
{ label: 'Name', prop: 'name' },
|
||||
{ label: 'Age', prop: 'age' }
|
||||
]
|
||||
],
|
||||
mergeRequestData0: null,
|
||||
mergeRequestData1: null,
|
||||
mergeRequestChildData0: null,
|
||||
mergeRequestChildData1: null
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
@@ -76,6 +80,24 @@ export default {
|
||||
this.differentParamData1 = response.data
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 同一url,不同入参的axios请求内包含多个不同url请求的demo
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async mergeRequest () {
|
||||
axios.get('/api/differentParam', { params: { name: 0 } }).then(response => {
|
||||
this.mergeRequestData0 = response.data
|
||||
})
|
||||
axios.get('/api/differentParam', { params: { name: 1 } }).then(response => {
|
||||
this.mergeRequestData1 = response.data
|
||||
axios.get('/api/getChildId').then(response1 => {
|
||||
this.mergeRequestChildData0 = response1.data
|
||||
})
|
||||
axios.get('/api/getChildTitle').then(response2 => {
|
||||
this.mergeRequestChildData1 = response2.data
|
||||
})
|
||||
})
|
||||
},
|
||||
async setIndexedDBValue () {
|
||||
await indexedDBUtils.selectTable('test').put({ id: 1, name: 'test' })
|
||||
},
|
||||
|
||||
@@ -16,6 +16,10 @@ const mockTitle = { data: 'title2' }
|
||||
const mockCount = { data: 999 }
|
||||
const mockDifferentParam0 = { data: 0 }
|
||||
const mockDifferentParam1 = { data: 1 }
|
||||
const mergeRequestParam0 = { data: 0 }
|
||||
const mergeRequestParam1 = { data: 1 }
|
||||
const mergeRequestChildParam0 = { data: 0 }
|
||||
const mergeRequestChildParam1 = { data: 1 }
|
||||
|
||||
describe('单元测试demo', () => {
|
||||
test('Vue组件--点击按钮后count的值+1', async () => {
|
||||
@@ -141,6 +145,36 @@ describe('单元测试demo', () => {
|
||||
expect(wrapper.vm.differentParamData0).toBe(0)
|
||||
expect(wrapper.vm.differentParamData1).toBe(1)
|
||||
})
|
||||
test('Vue组件--axios请求内包含多个不同url的组合请求', async () => {
|
||||
require('vue-router').useRoute.mockReturnValue({ query: {} })
|
||||
require('vue-router').useRouter.mockReturnValue({ currentRoute: { value: { path: '/' } } })
|
||||
// 模拟axios返回数据
|
||||
axios.get.mockResolvedValueOnce(mergeRequestParam0)
|
||||
axios.get.mockResolvedValueOnce(mergeRequestParam1)
|
||||
|
||||
axios.get.mockImplementation(url => {
|
||||
switch (url) {
|
||||
case '/api/getChildId':
|
||||
return Promise.resolve(mergeRequestChildParam0)
|
||||
case '/api/getChildTitle':
|
||||
return Promise.resolve(mergeRequestChildParam1)
|
||||
}
|
||||
})
|
||||
|
||||
// 加载vue组件,获得实例
|
||||
const wrapper = mount(Test, {
|
||||
global: {
|
||||
plugins: [ElementPlus]
|
||||
}
|
||||
})
|
||||
await wrapper.vm.mergeRequest()
|
||||
await wrapper.vm.$nextTick(() => {
|
||||
expect(wrapper.vm.mergeRequestData0).toBe(0)
|
||||
expect(wrapper.vm.mergeRequestData1).toBe(1)
|
||||
expect(wrapper.vm.mergeRequestChildData0).toBe(0)
|
||||
expect(wrapper.vm.mergeRequestChildData1).toBe(1)
|
||||
})
|
||||
})
|
||||
test('Vue组件--模拟indexedDB操作', async () => {
|
||||
require('vue-router').useRoute.mockReturnValue({ query: {} })
|
||||
require('vue-router').useRouter.mockReturnValue({ currentRoute: { value: { path: '/' } } })
|
||||
|
||||
Reference in New Issue
Block a user