【问题标题】:'TypeError: store.dispatch is not a function' redux action testing with jest'TypeError: store.dispatch is not a function' redux action testing with jest
【发布时间】:2020-07-11 22:29:48
【问题描述】:

我正在尝试为此 redux 操作编写测试:

export const clearError = () => (dispatch: Dispatch<IStoreState>) => dispatch({ type: actionTypes.CLEAR_ACTIVATION_ERRORS });

不明白为什么会这样,因为我从示例中一步一步完成了所有操作。这是我的测试:

import configureMockStore from 'redux-mock-store';
import * as actionTypes from '@const/actions';

import './__mocks__/reactNativeConfig';

import * as actions from '../index';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

describe('index.ts -> clearError', async () => {
  it('clearError', () => {
    const expectedAction = {
      type: actionTypes.CLEAR_ACTIVATION_ERRORS,
    };

    const store = mockStore;

    return store.dispatch(actions.clearError)
      .then(() => {
        expect(store.getActions()).toEqual(expectedAction);
      }
    );
  });
});

当我运行测试时出现错误:


      18 |     const store = mockStore;
      19 | 
    > 20 |     return store.dispatch(actions.clearError)
         |                  ^
      21 |       .then(() => {
      22 |         expect(store.getActions()).toEqual(expectedAction);
      23 |       }

请你帮我弄清楚我做错了什么。谢谢!

【问题讨论】:

  • 测试应该是单元测试。您不需要发送实际操作。只需测试您的减速器是否正在执行所需的状态更改。

标签: javascript reactjs redux jestjs redux-thunk


【解决方案1】:

尝试提供模拟商店数据,如下例所示

import configureMockStore from 'redux-mock-store';
import * as actionTypes from '@const/actions';

import './__mocks__/reactNativeConfig';

import * as actions from '../index';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

describe('index.ts -> clearError', async () => {
  it('clearError', () => {
    const expectedAction = {
      type: actionTypes.CLEAR_ACTIVATION_ERRORS,
    };

    const store = mockStore("mock store data should be given here({loading: false})");

    return store.dispatch(actions.clearError)
      .then(() => {
        expect(store.getActions()).toEqual(expectedAction);
      }
    );
  });
});

【讨论】:

    猜你喜欢
    • 2020-11-24
    • 2022-10-13
    • 2020-01-12
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    相关资源
    最近更新 更多