I am new to angular and i tried to write simple unit test for my component. My test fail time to time ant i get the following error: An error was thrown in afterAll TypeError: Cannot read property 'ids' of undefined and i also attached the error trace
This is my component implementation:
ngOnInit() {
this.groupValueForm.valueChanges.subscribe((group: BasicGroup) => {
this.store.dispatch(GroupActions.selectGroup({ group: group }));
});
this.store.dispatch(GroupActions.getAllGroups());
this.store.pipe(select(selectAllGroups)).subscribe((basicGroups: BasicGroup[]) => {
this.groups = basicGroups;
});
}
this is my test class implementation:
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
TopPanelModule,
STORE_MODULE_TEST
],
providers: [
{ provide: COUNTRY_ID, useValue: 'sv-SE' },
{ provide: TranslateService, useValue: mockTranslateService },
provideMockStore({ initialState })
]
});
store = TestBed.inject(MockStore);
fixture = TestBed.createComponent(TopPanelComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should use the groupList from store', (done: DoneFn) => {
spyOn(store, 'pipe').and.returnValue(of(groups));
component.ngOnInit();
store.pipe().subscribe(resp=>{
expect(component.groups).toEqual(groups);
done();
})
});
it('should dispatch getAllGroups', () => {
spyOn(component['store'], 'dispatch');
component.ngOnInit();
expect(component['store'].dispatch).toHaveBeenCalledWith(GroupActions.getAllGroups());
});
afterEach(() => {
fixture.destroy();
});