[Unit testing RxJS] Test asynchronous operations with marbles

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

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

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

  it("should let you test asynchronous operations", () => {
    testScheduler.run((helpers) => {
      const { expectObservable } = helpers;
      const source$ = from([1, 2, 3]);
      const final$ = source$.pipe(delay(5));
      const expected = "-- -- -(abc|)";
      expectObservable(final$).toBe(expected, { a: 1, b: 2, c: 3 });

      const final2$ = source$.pipe(delay(2001));
      // 2s: 2 seconds
      const expected2 = "2s -(abc|)";
      expectObservable(final2$).toBe(expected2, { a: 1, b: 2, c: 3 });
    });
  });
});

 

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