I just started to contact jest, there are a lot of online apis and so on, I just write the project and record it by myself. If you have any questions, you can leave a message and learn together.
emit event
page:
handleClose() {
this.$emit('update:isShow', false)
},
jest:
test('handleClose', () => {
wrapper.vm.handleClose()
expect(wrapper.emitted().update).toBeFalsy()
})
eventbus eventBus
page:
read(){
this.$message.success('操作成功')
this.$EventsBus.$emit('headerMsgTips')
this.$EventsBus.$emit('unReadySendMsg')
this.msgPageNamber = 1
this.msgList = []
this.getAllMessageList()
}
jest:
const $message = {
error: jest.fn(),
warning: jest.fn(),
success: jest.fn()
}
const wrapper = mount(allMsgList, {
localVue,
data() {
return {}
},
router,
mocks: {
$message,
$EventsBus: {
$emit: jest.fn()
}
}
})
//======
test('read', async () => {
jest.mock('@/api/workBeanch/index.js')
msgApi.checkMessageAllRead.mockResolvedValue({ code: 1 })
jest.spyOn(wrapper.vm, 'getAllMessageList')
await wrapper.vm.read()
Vue.nextTick()
expect(wrapper.vm.$message.success.mock.calls[0][0]).toBe('操作成功')
expect(wrapper.vm.msgPageNamber).toBe(1)
expect(wrapper.vm.msgList).toEqual([])
expect(wrapper.vm.$EventsBus.$emit).toHaveBeenCalledTimes(2)
expect(wrapper.vm.getAllMessageList).toBeCalledTimes(1)
})
Route jump router + window api
page:
goBmsDetail(id) {
let routeData = this.$router.resolve({
path: '/caseDetail/bmsDetail' + `?id=${id}`
})
window.open(routeData.href, `/caseDetail/bmsDetail?id=${id}`)
},
jest:
test('goBmsDetail', () => {
delete window.location //这里要重写一下 方便后面mock掉
window.location = { reload: jest.fn() }
window.location.reload = jest.fn()
window.open = jest.fn()
const spy = jest.spyOn(wrapper.vm.$router, 'resolve')
wrapper.vm.goBmsDetail(123)
expect(spy.mock.calls[0][0].path).toEqual('/caseDetail/bmsDetail?id=123')
expect(window.open).toBeCalledTimes(1)
})
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。