是否可以在测试内部调度一个动作并期望存储,而没有测试效果,减速器,选择器?我确实找不到一个很好的例子,所以我想知道哪种是测试ngrx的最佳实践。
有什么主意吗? 预先感谢
答案 0 :(得分:0)
“商店”是代表整个NgRx系统的术语。减速器是产生状态的。我认为您想测试您的行为如何产生状态。为此,您需要专注于减速器的“状态创建者”
减速器是一种功能。考虑这个简单的示例,其中我激活了一个“产品”,我想测试一下是否出现ActivateProduct动作,表明我的加载状态设置为true
export function productActivationReducer(state = initialState, action: ProductActivationActions): ProductActivationState {
switch (action.type) {
case ProductActivationActionTypes.ActivateProductSuccess:
return {
isLoading: true,
error: null,
data: action.payload,
};
default:
return state;
}
}
您的减速器是一个功能。可以使用以下功能调用该函数:创建一个测试,该测试将调用该函数并测试返回的状态。
import { productActivationReducer, initialState } from './product-activation.reducer';
describe('ActivateProductSuccess', () => {
it('should set the next state', () => {
action = new ActivateProductSuccess({ foo: 'bar'});
result = productActivationReducer(initialState, action);
expect(result.data).toEqual({ foo: 'bar'});
expect(result.error).toBeNull();
expect(result.loading).toBe(true);
});
});