gpt4 book ai didi

angular - 测试 ngrx 元 reducer

转载 作者:行者123 更新时间:2023-12-02 18:59:26 25 4
gpt4 key购买 nike

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

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];

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

最佳答案

在深入研究 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();
});

Source

关于angular - 测试 ngrx 元 reducer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65772928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com