Unit testing 单元测试:如何在调用vuex中函数的输入上正确触发触发器事件?
我有这个引导vue组件:Unit testing 单元测试:如何在调用vuex中函数的输入上正确触发触发器事件?,unit-testing,vue.js,vuex,bootstrap-vue,ts-jest,Unit Testing,Vue.js,Vuex,Bootstrap Vue,Ts Jest,我有这个引导vue组件: <b-form-input v-model="currentUser.name" placeholder="Name *" name="name" @input="checkSubmitStatus()" ></b-form-input> 这是.spec.js文件: import { createLocalVue, moun
<b-form-input
v-model="currentUser.name"
placeholder="Name *"
name="name"
@input="checkSubmitStatus()"
></b-form-input>
这是.spec.js文件:
import { createLocalVue, mount } from "@vue/test-utils";
import Vue from "vue";
import Vuex from 'vuex';
import UserForm from "@/components/event-created/UserForm.vue";
import { BootstrapVue, BootstrapVueIcons } from "bootstrap-vue";
const localVue = createLocalVue();
localVue.use(BootstrapVue);
localVue.use(BootstrapVueIcons);
localVue.use(Vuex);
describe("UserForm.vue", () => {
let mutations;
let store;
beforeEach(() => {
mutations = {
updateSubmitDisabled: jest.fn()
};
store = new Vuex.Store({
state: {
currentUser: {
name: 'pippo',
}
},
mutations
});
})
it("should call the updateSubmitDisabled mutation", async () => {
const wrapper = mount(UserForm, { localVue, store });
const input = wrapper.get('input[name="name"]');
await Vue.nextTick();
input.element.value = 'Test';
await input.trigger('input');
await Vue.nextTick();
expect(mutations.updateSubmitDisabled).toHaveBeenCalled();
});
});
目前,我只想测试是否在“name”上调用了“updateSubmitDisabled”,但测试结果显示:
预期呼叫数:>=1
收到的呼叫数:0我最终与:
it("should call the updateSubmitDisabled mutation", () => {
const wrapper = mount(UserForm, { localVue, store });
const input = wrapper.get('input[name="name"]');
input.element.dispatchEvent(new Event('input'));
expect(mutations.updateSubmitDisabled).toHaveBeenCalled();
});
您可以在checkSubmitStatus中执行console.log以查看是否调用了它吗?
it("should call the updateSubmitDisabled mutation", () => {
const wrapper = mount(UserForm, { localVue, store });
const input = wrapper.get('input[name="name"]');
input.element.dispatchEvent(new Event('input'));
expect(mutations.updateSubmitDisabled).toHaveBeenCalled();
});