用 jest 测试 vue

时间:2021-03-04 07:29:45

标签: vue.js unit-testing jestjs vuex vue-test-utils

我们在 vuex 中有一个这样的商店:

import { createStore } from "vuex";
import dialog from "./modules/dialog";

export default createStore({
  state: {},
  getters: {},
  mutations: {},
  actions: {},
  modules: {
   dialog
  }
});

也在对话模块中我们有:

export default {
 state: {
  show: false,
  size: Width.md
 },

mutations: {
showDialog: (state, options) => {
  const { size, body, header, footer, title } = options;

  state.show = true;
  state.size = size;
  state.body = body;
  state.header = header;
  state.footer = footer;
  state.title = title;
},

hideDialog: (state: Dialog) => {
  state.show = false;
}
},
};

我们要编写单元测试来测试隐藏和显示功能 这是我的测试:

 it("Should close dialog by clicking on close icon", async () => {
  const wrapper = factory("shallow");

  expect(store.state.dialog.show).toBe(true);
  await wrapper.find(".dialog__icon--close").trigger("click");
  expect(store.state.dialog.show).toBe(false);
  
  const spy = jest.spyOn(modules.default.mutations, "hideDialog");

  expect(spy).toBeCalled();
  expect(store.state.dialog.show).toBe(false);
});

但是这一行“expect(spy).toBeCalled();”被拒绝,导致“hideDialog”是 从来没有打过电话。

我的结果:

 Expected number of calls: >= 1
 Received number of calls:    0

有人可以帮助我们吗?提供示例或定义适当的链接

0 个答案:

没有答案