使用TestScheduler测试publishReplay

时间:2018-09-03 15:54:43

标签: javascript testing rxjs jestjs

我正在尝试对一个可观察对象是否正确发布一个重播(即:publishReplay(1))进行单元测试。我希望观察对象在订阅时发出{value: 1},而不是{value: 0}。我怀疑TestScheduler的无知是解决问题的关键。

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

我刚开始使用TestScheduler,所以对我来说可能是无知:

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);
  });
});

Jest差异/输出如下:

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}}}]

0 个答案:

没有答案