我正在为我的组件创建一些单元测试,但测试一直失败,因为我正在测试的按钮始终没有被点击事件触发。
我使用这些文档作为测试的基础: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)
})
Run Code Online (Sandbox Code Playgroud)
我的组件:
<template>
<v-btn class="primary-text" @click.native="methodForTesting($event)">Test</v-btn>
<template>
<script>
methods: {
methodForTesting(){
console.log('button clicked!')
}
</script>
Run Code Online (Sandbox Code Playgroud)