如何在测试中使用store.dispatch来分发thunk(没有redux-mock-store)

时间:2019-07-18 12:12:34

标签: typescript redux jestjs

我对Typescript还是陌生的,只是设法创建了一些使用getState()的thunk:

沉思:

const checkStudioName = (): ThunkAction<Promise<void>, ProState, void, AnyAction> => async (dispatch, getState) => {
  const studioName = getStudioName(getState());

  const reqObj: IRequestOptions = {
    method: RequestTypes.GET,
  };

  const url = buildQuery({}, PROVIDERS_VERIFY_STUDIONAME, RequestEndpoints.API, { studioName });

  request(STUDIO_NAME_VERIFY_ACTION, url, reqObj, dispatch);
}

mapDispatchToProps:

export const mapDispatchToProps = (dispatch: ThunkDispatch<ProState, void, Action>): IStudioActions => ({
  checkStudio: () => dispatch(thunkActions.checkStudioName()),
});

那很好。 但是,我进行了一些测试,以确保状态在thunk完成之后(但在我打电话时)已更新

it('updates state when checking if studio name exists', () => {
    customGlobal.fetch.mockResponseOnce(JSON.stringify({ exist: false }));

  return store.dispatch(thunkActions.checkStudioName()).then(async () => {
    await flushAsync();

    expect(isStudioNameValid(store.getState())).toEqual(true);
  });
});

store.dispatch期望一个普通的动作,而不是一个会产生错误的重击动作。 我尝试使用redux-mock-store(类型宽松的调度),但无法跟踪状态。

有什么方法可以迫使商店使用正确的调度方式?

如果我将studioName登录到thunk,则在运行测试时可以在控制台中看到它,同时仍然得到'getState is not a function'和''ThunkAction<Promise<void>, ..... is not assignable to parameter of type 'AnyAction'.类型的参数'< / p>

0 个答案:

没有答案