我正在尝试以开玩笑的方式在vue.js中测试以下道具:
export default {
name: 'my-component',
props: {
onConfirm: {
type: Function,
default: () => {}
},
}
}
Jest一直告诉我,我需要介绍onConfirm道具的默认值,但是我不知道该怎么做。
我尝试了以下操作:
expect(wrapper.vm.onConfirm).toBe(() => {})
expect(wrapper.vm.onConfirm).toBe(Function)
但两者似乎都不正确。
答案 0 :(得分:1)
尝试一下:
expect(wrapper.props().onConfirm.name).toBe('default')
如果它是一个函数,它将具有名称prop,在这种情况下,名称将是默认名称。
答案 1 :(得分:1)
经过反复试验,终于找到了解决方案。
it('handles onConfirm default correct', async () => {
const fn = jest.fn()
wrapper.vm.confirm()
expect(fn).toHaveBeenCalledTimes(0)
})
我只需要模拟一个函数,并调用prop onConfirm,然后测试是否未调用模拟函数。
现在开玩笑说分支已经被覆盖了。