[Unit testing RxJS] Test hot observables with marbles

const { TestScheduler } = require("rxjs/testing");
const { map, take } = require("rxjs/operators");
const { concat } = require("rxjs");

describe("Marble testing in Rxjs", () => {
  let testScheduler;

  beforeEach(() => {
    testScheduler = new TestScheduler((actual, expected) => {
      expect(actual).toEqual(expected);
    });
  });

  it("should let you test hot observables", () => {
    testScheduler.run((helpers) => {
      const { hot, expectObservable } = helpers;
      // in this case, hot == cold
      const source$ = hot("-a-b--c");
      const expected = "-a-b--c";
      expectObservable(source$).toBe(expected);

      // ^: means there is an subsrcitpion comes in
      const source2$ = hot("-a-b-^-c");
      const expected2 = "--c";
      expectObservable(source2$).toBe(expected2);

      // take completed case
      const final$ = source2$.pipe(take(1));
      const expected3 = "--(c|)";
      expectObservable(final$).toBe(expected3);
    });
  });
});

 

posted @ 2022-10-13 20:24  Zhentiw  阅读(13)  评论(0编辑  收藏  举报