测试嵌套vuex调度的动作

时间:2018-10-15 19:58:31

标签: javascript vue.js jestjs vuex

我遇到了一个非常普遍的问题。

我有2个动作-一个用于获取数据(安装组件时发生),另一个用于对获取的数据进行少量更改。

最低限度的代码:

const actions = {
  fetchData({ commit ), ids) {
    Api.getData(ids).then(res => {
      /* do some magic with response */
      const payload = /* serialized data */
      dispatch('initAction', payload);
    }
  },
  initAction({ commit }, payload) {
    /* run few commits */
  }
} 

问题是我想测试在fetchData完成之后是否调用了initAction。

/*This is my mocked jest functions which matches the names in the Vuex store which I inject in my shallow rendered components.*/

actions: {
   initAction: jest.fn().mockName('initAction'),
   fetchData: jest.fn().mockName('fetchQuestions'),
},


/* actual testing code */
const wrapper = mount(Widget, options);
const { organizationId, feedbackId, categoryId } = wrapper.props();
expect(storeMock.modules.widget.actions.fetchData).toHaveBeenCalled();
expect(storeMock.modules.widget.actions.initAction).toHaveBeenCalled();

有人对此有任何提示吗?也许有人对此用例有任何示例?

0 个答案:

没有答案
相关问题