小编Emi*_*Emi的帖子

如何让我的 vue-unit-test 触发 Vuetify 中 v-btn 上的点击事件?

我正在为我的组件创建一些单元测试,但测试一直失败,因为我正在测试的按钮始终没有被点击事件触发。

我使用这些文档作为测试的基础: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)

jestjs vuetify.js vue-test-utils

2
推荐指数
1
解决办法
5105
查看次数

标签 统计

jestjs ×1

vue-test-utils ×1

vuetify.js ×1