vue-test-utils 在按钮上点击触发器不触发
vue-test-util click trigger on button not firing
我有一个带按钮的 Vue 组件。单击按钮时,将调用一个方法。我正在使用 Jest 进行单元测试。我希望 vue-test-utils
中的 .trigger
方法在按钮上创建一个合成事件,但它什么也没做。
我尝试通过调用 wrapper.vm.addService()
然后使用 console.log(wrapper.emitted())
直接在包装器上调用方法,我确实可以看到事件已被触发。所以我的问题是为什么 addServiceBtn.trigger('click')
什么都不做。
console.log(wrapper.emitted())
是一个空对象。测试结果失败,错误消息:Expected spy to have been called, but it was not called.
ServiceItem.vue
<template>
<v-flex xs2>
<v-card>
<v-card-text id="itemTitle">{{ item.title }}</v-card-text>
<v-card-actions>
<v-btn flat color="green" id="addServiceBtn" @click="this.addService">Add</v-btn>
</v-card-actions>
</v-card>
</v-flex>
</template>
<script>
export default {
data: () => ({
title: ''
}),
props: {
item: Object
},
methods: {
addService: function (event) {
console.log('service item')
this.$emit('add-service')
}
}
}
</script>
tests.spec.js
import { shallowMount, mount } from '@vue/test-utils'
import ServiceItem from '@/components/ServiceItem.vue'
import Vue from 'vue';
import Vuetify from 'vuetify';
Vue.use(Vuetify);
describe('ServiceItem.vue', () => {
it('emits add-service when Add button is clicked', () => {
const item = {
title: 'Service'
}
const wrapper = mount(ServiceItem, {
propsData: { item }
})
expect(wrapper.find('#addServiceBtn').exists()).toBe(true)
const addServiceBtn = wrapper.find('#addServiceBtn')
const spy = spyOn(wrapper.vm, 'addService')
console.log(wrapper.emitted())
addServiceBtn.trigger('click')
expect(wrapper.vm.addService).toBeCalled()
})
})
您的 HTML 代码有一点错误。您将 @click
事件绑定到您的方法,而无需任何 this
。做到:
<v-btn flat color="green" id="addServiceBtn" @click="addService($event)">Add</v-btn>
它不起作用的原因是因为 this.addService
在 <template>
中建议您删除 this
并只说 @click="addService($event)"
或 @click="addService"
也可以正常工作,但没有传入任何事件
对我来说它没有用,但是在使用 vue-test-utils 测试时未能触发事件,直到我添加 .native
<v-btn @click.native="addToCart($event)">
Add
</v-btn>
其实原代码中的测试失败还有一个原因:函数调用中的括号。
我发现语法 @click="addService"
会导致测试失败,而非常相似(并且不知何故不鼓励)的语法 @click="addService()"
会成功。
示例:
test('Click calls the right function', () => {
// wrapper is declared before this test and initialized inside the beforeEach
wrapper.vm.testFunction = jest.fn();
const $btnDiscard = wrapper.find('.btn-discard');
$btnDiscard.trigger('click');
expect(wrapper.vm.testFunction).toHaveBeenCalled();
});
此测试因以下语法而失败:
<button class="btn blue-empty-btn btn-discard" @click="testFunction">
{{ sysDizVal('remove') }}
</button>
但使用此语法:
<button class="btn blue-empty-btn btn-discard" @click="testFunction()">
{{ sysDizVal('remove') }}
</button>
我有一个带按钮的 Vue 组件。单击按钮时,将调用一个方法。我正在使用 Jest 进行单元测试。我希望 vue-test-utils
中的 .trigger
方法在按钮上创建一个合成事件,但它什么也没做。
我尝试通过调用 wrapper.vm.addService()
然后使用 console.log(wrapper.emitted())
直接在包装器上调用方法,我确实可以看到事件已被触发。所以我的问题是为什么 addServiceBtn.trigger('click')
什么都不做。
console.log(wrapper.emitted())
是一个空对象。测试结果失败,错误消息:Expected spy to have been called, but it was not called.
ServiceItem.vue
<template>
<v-flex xs2>
<v-card>
<v-card-text id="itemTitle">{{ item.title }}</v-card-text>
<v-card-actions>
<v-btn flat color="green" id="addServiceBtn" @click="this.addService">Add</v-btn>
</v-card-actions>
</v-card>
</v-flex>
</template>
<script>
export default {
data: () => ({
title: ''
}),
props: {
item: Object
},
methods: {
addService: function (event) {
console.log('service item')
this.$emit('add-service')
}
}
}
</script>
tests.spec.js
import { shallowMount, mount } from '@vue/test-utils'
import ServiceItem from '@/components/ServiceItem.vue'
import Vue from 'vue';
import Vuetify from 'vuetify';
Vue.use(Vuetify);
describe('ServiceItem.vue', () => {
it('emits add-service when Add button is clicked', () => {
const item = {
title: 'Service'
}
const wrapper = mount(ServiceItem, {
propsData: { item }
})
expect(wrapper.find('#addServiceBtn').exists()).toBe(true)
const addServiceBtn = wrapper.find('#addServiceBtn')
const spy = spyOn(wrapper.vm, 'addService')
console.log(wrapper.emitted())
addServiceBtn.trigger('click')
expect(wrapper.vm.addService).toBeCalled()
})
})
您的 HTML 代码有一点错误。您将 @click
事件绑定到您的方法,而无需任何 this
。做到:
<v-btn flat color="green" id="addServiceBtn" @click="addService($event)">Add</v-btn>
它不起作用的原因是因为 this.addService
在 <template>
中建议您删除 this
并只说 @click="addService($event)"
或 @click="addService"
也可以正常工作,但没有传入任何事件
对我来说它没有用,但是在使用 vue-test-utils 测试时未能触发事件,直到我添加 .native
<v-btn @click.native="addToCart($event)">
Add
</v-btn>
其实原代码中的测试失败还有一个原因:函数调用中的括号。
我发现语法 @click="addService"
会导致测试失败,而非常相似(并且不知何故不鼓励)的语法 @click="addService()"
会成功。
示例:
test('Click calls the right function', () => {
// wrapper is declared before this test and initialized inside the beforeEach
wrapper.vm.testFunction = jest.fn();
const $btnDiscard = wrapper.find('.btn-discard');
$btnDiscard.trigger('click');
expect(wrapper.vm.testFunction).toHaveBeenCalled();
});
此测试因以下语法而失败:
<button class="btn blue-empty-btn btn-discard" @click="testFunction">
{{ sysDizVal('remove') }}
</button>
但使用此语法:
<button class="btn blue-empty-btn btn-discard" @click="testFunction()">
{{ sysDizVal('remove') }}
</button>