'TypeError:store.dispatch不是函数',使用jest进行redux动作测试

时间:2020-03-31 11:25:19

标签: javascript reactjs redux jestjs redux-thunk

我正在尝试为此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 |       }

请帮忙,帮助您找出我做错了什么。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试像下面的示例一样提供模拟存储数据

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);
      }
    );
  });
});