测试 ngrx 元减速器

Mot*_*lal 2 jasmine ngrx angular

我有一个元减速器,应该在注销时清除状态。

export function clearState(reducer: ActionReducer<any>): ActionReducer<any> {
  return (state, action) => {
    if (action.type === AuthActionTypes.UNAUTHENTICATED) {
      state = undefined;
    }
    return reducer(state, action);
  };
}

export const metaReducers: MetaReducer<any>[] = [clearState];
Run Code Online (Sandbox Code Playgroud)

我想对这段代码进行单元测试。我之前测试过普通的减速器,但对我来说,在为元减速器编写测试时很难应用相同的技术。并且文档中没有示例。

Mot*_*lal 5

在深入研究 ngrx 源代码后,我找到了一种测试它的方法

const invokeActionReducer = (currentState, action: any) => {
  return clearState((state) => state)(currentState, action);
};

it('should set state to initial value when logout action is dispatched', () => {
  const currentState = { /* state here */ };

  expect(invokeActionReducer(currentState, userUnauthenticated)).toBeFalsy();
});
Run Code Online (Sandbox Code Playgroud)

来源