简体   繁体   中英

'TypeError: store.dispatch is not a function' redux action testing with jest

I'm trying to write a test for this redux action:

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

Don't understand why it's happened because I did everything from the example step by step. This is my test:

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

and when I run my test I have an error:


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

Please, colud you help me figure out what I'm doing wrong. Thank you!

try giving mock store data like the example below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM