【发布时间】:2021-03-13 03:23:01
【问题描述】:
我正在为我的组件创建一些单元测试,但测试一直失败,因为我正在测试的按钮 一直没有被点击事件触发。
我使用文档作为测试的基础:https://vuetifyjs.com/sv-SE/getting-started/unit-testing/
我也尝试了这里提到的一些建议:https://forum.vuejs.org/t/how-to-trigger-an-onchange-event/11081/4
但我好像错过了什么,谁能帮帮我?
我的测试:
test('If you can click on the Test button', () => {
const wrapper = shallowMount(myComponent, {
localVue,
vuetify,
});
const event = jest.fn();
const button = wrapper.find({name: 'v-btn'})
expect(button.exists()).toBe(true) //this works
wrapper.vm.$on('v-btn:clicked', event)
expect(event).toHaveBeenCalledTimes(0)
button.trigger('click')
expect(event).toHaveBeenCalledTimes(1)
})
我的组件:
<template>
<v-btn class="primary-text" @click.native="methodForTesting($event)">Test</v-btn>
<template>
<script>
methods: {
methodForTesting(){
console.log('button clicked!')
}
</script>
【问题讨论】:
-
尝试在button.trigger('click')之后调用await wrapper.vm.$nextTick()
-
@Anatoly 我尝试了你的建议,但我仍然收到一条错误消息:“expect(jest.fn()).toHaveBeenCalledTimes(expected) 预期的通话次数:1 收到的通话次数:0” ://
-
尝试 button.vm.$on('v-btn:clicked', event) 而不是 wrapper.vm.$on('v-btn:clicked', event)
-
试试 button.vm.$emit('click') - 这个 github 问题有更多关于这个github.com/vuejs/vue-test-utils/issues/919的信息
标签: jestjs vuetify.js vue-test-utils