fix: 完善单测demo(多个同url不同参数的axios请求)

This commit is contained in:
chenjinsong
2023-01-30 17:07:58 +08:00
parent d2aa5c9b7a
commit 6756812c34
2 changed files with 28 additions and 0 deletions

View File

@@ -32,6 +32,8 @@ export default {
return {
count: 0,
obj: { id: 1, title: 'title' },
differentParamData0: null,
differentParamData1: null,
indexedDBValue: null,
tableData: [
{
@@ -66,6 +68,14 @@ export default {
this.count = response.data
})
},
async differentRequestParam () {
axios.get('/api/differentParam', { params: { name: 0 } }).then(response => {
this.differentParamData0 = response.data
})
axios.get('/api/differentParam', { params: { name: 1 } }).then(response => {
this.differentParamData1 = response.data
})
},
async setIndexedDBValue () {
await indexedDBUtils.selectTable('test').put({ id: 1, name: 'test' })
},

View File

@@ -14,6 +14,8 @@ import ElementPlus from 'element-plus'
const mockId = { data: 2 }
const mockTitle = { data: 'title2' }
const mockCount = { data: 999 }
const mockDifferentParam0 = { data: 0 }
const mockDifferentParam1 = { data: 1 }
describe('单元测试demo', () => {
test('Vue组件--点击按钮后count的值+1', async () => {
@@ -123,6 +125,22 @@ describe('单元测试demo', () => {
expect(wrapper.vm.obj).toEqual({ id: 2, title: 'title2' })
})
})
test('Vue组件--多个同url不同参数的axios请求', async () => {
require('vue-router').useRoute.mockReturnValue({ query: {} })
require('vue-router').useRouter.mockReturnValue({ currentRoute: { value: { path: '/' } } })
// 模拟axios返回数据
axios.get.mockResolvedValueOnce(mockDifferentParam0)
axios.get.mockResolvedValueOnce(mockDifferentParam1)
// 加载vue组件获得实例
const wrapper = mount(Test, {
global: {
plugins: [ElementPlus]
}
})
await wrapper.vm.differentRequestParam()
expect(wrapper.vm.differentParamData0).toBe(0)
expect(wrapper.vm.differentParamData1).toBe(1)
})
test('Vue组件--模拟indexedDB操作', async () => {
require('vue-router').useRoute.mockReturnValue({ query: {} })
require('vue-router').useRouter.mockReturnValue({ currentRoute: { value: { path: '/' } } })