Search code examples
rxjsngrx

The "hot" observable doesn't work when setting time delay in rxjs


I'm writing a unit test with ngrx for effect, when I run the test with hot observable on the effect which has delay on pipe chain, the receive has an empty value.

const output = loginSuccess(loginResult);

actions$ = hot('^a', { a: action });
const expected = cold('30ms c', { c: output })

expect(effect.login$).toBeObservable(expected);

My function is:

login$ = createEffect(() =>
    this.actions$.pipe(
      ofType(login),
      delay(20),
      map((payload) => loginSuccess({
        data: {
          username: payload.credential.username,
          age: 18,
          name: 'name',
        },
      }))
    )
  );

It works fine if I remove the delay operator (and correct the time in the test case), what I did wrong?

Thank you!


Solution

  • You need to use the test scheduler. See the NgRx docs for an example - https://ngrx.io/guide/effects/testing#effects-with-parameters

    effects.search$({
      scheduler: getTestScheduler(),
    });