I have a component that has the following function:
onClearSearch() {
this.store$.dispatch(some action);
}
Trying to unit test the component and trigger the store dispatch is not working. My component test looks like this:
let store: MockStore<State>;
beforeEach(waitForAsync(() => {
TestBed.configureTestModule({
...
providers: [
SearchStoreEffects,
provideMockActions(() => actions$),
provideMockStore({initialState, selectors: [...]})
],
...
}).compileComponents();
...
store = TestBed.inject(MockStore);
fixture.detectChanges();
}));
it('should clear search value', () => {
const storeSpy = spyOn(store, 'dispatch').and.callThrough();
component.onClearSearch();
fixture.detectChanges();
expect(storeSpy).toHaveBeenCalledTimes(1);
});
The test always fails with test has been called zero times. What am I missing?
You need to spy on the component instance
it('should clear search value', () => {
const storeSpy = spyOn(component.store$, 'dispatch').and.callThrough();
component.onClearSearch();
fixture.detectChanges();
expect(storeSpy).toHaveBeenCalledTimes(1);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With