Reputation: 73
I have a component with an entity selector 'selectAllProperties' in ngOnInit, and I want to test this component:
ngOnInit() {
this.store.dispatch(new LoadPropertiesRequested());
this.properties$ = this.store.pipe(select(selectAllProperties));
this.loading$ = this.store.pipe(select(selectPropertiesLoading));
this.logs$ = this.store.pipe(select(selectPropertiesLogs));
}
In my spec file, i initialized the store like in the ngrx doc:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({
...fromRoot.reducers,
feature: combineReducers(fromProperties.reducer),
})
],
declarations: [
SidebarPropertiesComponent,
SidebarElementComponent
]
})
.compileComponents();
}));
When I launch the tests, I have 'TypeError: Cannot read property 'ids' of undefined'. All the others selectors do not produce errors
I also would like to mock the Observable returned by each selector.
Thanks
Upvotes: 2
Views: 846
Reputation: 73
I have found the problem, in TestBed.configureTestingModule
instead of
imports: [
StoreModule.forRoot({
...fromRoot.reducers,
feature: combineReducers(fromProperties.reducer),
})
],
use
imports: [
StoreModule.forRoot(reducers, { metaReducers }),
StoreModule.forFeature('properties', fromProperties.reducer),
],
it('should have 2 properties elements', () => { store.dispatch(new LoadPropertiesSuccess({properties: propertiesMock})); fixture.detectChanges(); const list = debugElement.queryAll(By.css('li')); expect(list.length).toBe(2); });
Upvotes: 1