zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[Unit testing RxJS] Test hot observables with marbles

with test Rxjs Testing unit hot Observables
2023-09-14 09:00:45 时间
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);
    });
  });
});