我有以下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
?