【发布时间】:2019-12-08 18:43:12
【问题描述】:
我的 NgRx 实现有一个智能组件测试,看起来像这样:
describe( 'Component', () => {
let store: MockStore<State>;
beforeEach( async( () => {
TestBed.configureTestingModule( {
/* ... */
providers: [
provideMockStore( { initialState: fromReducer.initialState } )
]
} ).compileComponents();
store = TestBed.get<Store<State>>( Store );
} ) );
it( 'should load items in #ngOnInit', () => {
store.setState( {
item: {
...fromReducer.initialState,
entities: { [item.id]: item },
},
otherFeature: null,
otherFeature: null,
otherFeature: null
} );
component.items$.subscribe( items =>
store.select( ItemStoreSelectors.selectItems ).subscribe( fromStore => expect( items ).toEqual( fromStore ) )
);
} );
});
我使用 provideMockStore 和 setState 来模拟我的 NgRx 状态。这种方式一切正常,但我真的不喜欢这部分:
store.setState( {
item: {
...fromReducer.initialState,
entities: { [item.id]: item },
},
otherFeature: null,
otherFeature: null,
otherFeature: null
} );
我必须将我的状态的所有其他功能切片添加到 setState 函数。否则 Typescript 会抛出错误。
所以最好我不想设置根状态,而是像这样设置一个特定的特征片:
store.setState( {
...fromReducer.initialState,
entities: { [item.id]: item },
} );
我找不到任何有关如何将 provideMockStore 用于状态 here 的特定切片的文档。
【问题讨论】:
标签: angular typescript ngrx ngrx-store angular-test