Я пытаюсь провести модульный тест, правильно ли наблюдаемая публикует один повтор (т. Е. 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 diff / output является следующим:
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}}}]