Yes, you can test observables like the data source you’ve mentioned.

The test scheduler subscribes to the hot and cold observables and then looks at the notifications that occur in virtual time. So the trick to get the test scheduler to subscribe to an observable that’s something other than a hot or cold observable, is to merge that observable into an observable that’s composed from a hot or cold observable.

An example should make this clearer:

it("should support mergeMapTo", marbles((m) => {  const source = m.cold("-a----");
const expected = "--b-c-";
const subject = new Subject<string>();
m.scheduler.schedule(() => subject.next("b"), 2);
m.scheduler.schedule(() => subject.next("c"), 4);
const destination = source.pipe(mergeMapTo(subject));
m.expect(destination).toBeObservable(expected);
}));

Here, mergeMapTo is used to ‘inject’ a subject into an observable composed from a cold source. When the cold source emits, the notifications from the subject will be merged into its stream.

Note that the test scheduler is used to delay the calls to the subject’s next method.

You could use the same technique with your data source:

  • mergeMapTo the data source from a hot or cold observable; and
  • call filter or sort at a known point in virtual time using the test scheduler.

RxJS core team member; front-end developer; mentor; speaker; open-source contributor

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store