Jest: How to mock a redux thunk calling another thunk?

Viewed 1735

I have the following redux thunk:

function thunkOne() {
    return dispatch => {
        callToApi().then(() => {
            dispatch(someNonThunkAction());
            dispatch(thunkTwo());
        });
    }
}

function thunkTwo() {
   return dispatch => {
      anotherCallToApi.then(dispatch(someOtherAction()));
   }
}

I would like to test only thunkOne and mock thunkTwo so that it doesn't get executed when I test thunkOne.

I tried to do this but it doesn't work:

import * as someActions from './actions';

it ('thunkOne should dispatch someNonThunkAction and thunkTwo', () => {
    someActions.thunkTwo = jest.fn();
    const expectedActions = [
            { type: SOME_NON_THUNK_ACTION, data: {} }
        ],
        store = mockStore(initialState);

    store.dispatch(someActions.thunkOne()).then(() => {
        expect(store.getActions()).toEqual(expectedActions);
        expect(someActions.thunkTwo).toHaveBeenCalled();
    });

    someActions.thunkTwo.mockRestore();
});

I get the following error when I run this test:

[ERROR] Actions must be plain objects. Use custom middleware for async actions.

How do I mock thunkTwo and test thunkOne only?

0 Answers
Related