I'm trying to write a unit test for a non-dispatching effect (base on the ngrx guide: https://ngrx.io/guide/effects/testing) But for unknown reasons the effect doesn't seem to catch the action And I don't understand what is wrong with my test.
Effect:
startLocalStorageSync$ = createEffect(() => this.actions$.pipe(
ofType(START_LOCAL_STORAGE_SYNC),
switchMap(() => {
return this.authService.authState$.pipe(
tap((authState) => {
this.localStorageService.set(AUTH_COOKIE, authState);
})
);
})
), {dispatch: false});
Unit test:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
LocalStorageService,
AuthService,
AuthEffects,
provideMockActions(() => actions$)
]
});
effects = TestBed.inject(AuthEffects);
authService = TestBed.inject(AuthService);
localStorageService = TestBed.inject(LocalStorageService);
});
it('should call set to local storage', () => {
const setSpy : jasmine.Spy = spyOn(localStorageService, 'set');
actions$ = cold('a', {a: new AuthActions.StartLocalStorageSync()});
effects.startLocalStorageSync$.subscribe(()=>{
expect(setSpy).toHaveBeenCalled();
expect(setSpy).toHaveBeenCalledWith(AUTH_COOKIE, authState);
});
});
If add the following line, then the effect catches the action and enters the switchmap + the logic in the tap:
expect(effects.initAuthFromLocalStorage$).toBeObservable();
Although I get an error for this line and the actual value of the effect (initAuthFromLocalStorage$) is an object and not an observable.
Thanks!
After reading
A non-dispatching Effect
in the link you provided, try the following: