RxJS Testing & Experimental - Tuong-Nguyen/Angular-D3-Cometd GitHub Wiki

RxJS

When using RxJS, we need:

  • understand the operators of rxjs for using them correctly.
  • test the combination of operators.

We can use jasmine-marbles for doing that.

jasmine marbles

Create observable:

  • hot: create an observable which is emitting items

hot('--a--b') will emit "a" and "b" hot('--a--b', { a: 1, b: 2 }) will emit 1 and 2.

  • cold: create an observable which waits for subscription to emit items.

hot('---#') will emit error "error" hot('---#', null, new SpecialError('test')) will emit new SpecialError('test')

Assertion method:

expectObservable(actual: Observable<T>).toBe(marbles: string, values?: object, error?: any) - schedules an assertion for when the TestScheduler flushes. The TestScheduler will automatically flush at the end of your jasmine it block.

Marble Syntax:

  • "-" time: 10 "frames" of time passage.
  • "|" complete: The successful completion of an observable. This is the observable producer signaling complete()
  • "#" error: An error terminating the observable. This is the observable producer signaling error()
  • "a" any character: All other characters represent a value being emitted by the producure signaling next()
  • "()" sync groupings: When multiple events need to single in the same frame synchronously, parenthesis are used to group those events. You can group nexted values, a completion or an error in this manner. The position of the initial ( determines the time at which its values are emitted.
  • "^" subscription point: (hot observables only) shows the point at which the tested observables will be subscribed to the hot observable. This is the "zero frame" for that observable, every frame before the ^ will be negative.

References:

https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md
https://alligator.io/rxjs/marble-testing/