单元测试:如何在调用 vuex 函数的输入上正确触发触发事件?
Unit Test: How can i correctly trigger a trigger event on an input which calls a function in vuex?
我有这个 bootstrap vue 组件:
<b-form-input
v-model="currentUser.name"
placeholder="Name *"
name="name"
@input="checkSubmitStatus()"
></b-form-input>
方法中的 checkSubmitStatus 会调用 updateSubmitDisabled,我在另一个文件中的突变中使用了它:
methods: {
...mapMutations({
updateSubmitDisabled: "updateSubmitDisabled"
}),
checkSubmitStatus() {
const isDisabled = this.currentUser.name.length === 0;
this.updateSubmitDisabled(isDisabled);
}
}
这是 .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();
});
我有这个 bootstrap vue 组件:
<b-form-input
v-model="currentUser.name"
placeholder="Name *"
name="name"
@input="checkSubmitStatus()"
></b-form-input>
方法中的 checkSubmitStatus 会调用 updateSubmitDisabled,我在另一个文件中的突变中使用了它:
methods: {
...mapMutations({
updateSubmitDisabled: "updateSubmitDisabled"
}),
checkSubmitStatus() {
const isDisabled = this.currentUser.name.length === 0;
this.updateSubmitDisabled(isDisabled);
}
}
这是 .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();
});