lundi 3 septembre 2018

Testing publishReplay with TestScheduler

I'm trying to unit test whether an observable correctly publishes one replay (ie: publishReplay(1)). I'm expecting the observable to emit {value: 1} upon subscription, not {value: 0}. I suspect my TestScheduler ignorance is key to solving.

export function createStore(reducers$, initialState$ = of()) {
  return merge(initialState$, reducers$).pipe(
    scan((state, [scope, reducer]) => ({
      ...state,
      [scope]: reducer(state[scope])
    })),
    publishReplay(1),
    refCount()
  );
}

I've just started using TestScheduler so it may be ignorance on my part:

it('should emit 1 replay when subscribing', () => {
  scheduler.run(({ hot, expectObservable }) => {
    const action$ = hot('1----^-----');
    const actionReducer$ = action$.pipe(map(() => () => 1));
    const reducer$ = actionReducer$.pipe(map(state => ['value', state]))
    const initialState$ = of({ value: 0 });

    const store$ = createStore(reducer$, initialState$);

    const expected = 'a';
    const values = { a: { value: 1 } };
    expectObservable(store$).toBe(expected, values);
  });
});

The Jest diff/output is the following:

Expected value to equal:
  [{"frame": 0, "notification": {"error": undefined, "hasValue": true, "kind": "N", "value":{"value": 1}}}]
Received:
  [{"frame": 0, "notification": {"error": undefined, "hasValue": true, "kind": "N", "value":{"value": 0}}}]

Aucun commentaire:

Enregistrer un commentaire