笑话:如何嘲笑一个redux thunk调用另一个thunk?

时间:2018-09-07 18:08:38

标签: javascript reactjs unit-testing redux jestjs

我有以下redux thunk:

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

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

我只想测试thunkOne和模拟thunkTwo,以便在测试thunkOne时不执行它。

我试图这样做,但是没有用:

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

运行此测试时出现以下错误:

  

[错误]操作必须是普通对象。使用自定义中间件执行异步操作。

如何仅模拟thunkTwo并测试thunkOne

0 个答案:

没有答案