Let's say I have an effect
@Effect()
someEffect$ = this.actions$.pipe(ofType(X), switchMap(() =>
of(Y).pipe(delay(3000)))
How should marble test look like?
const action = new X();
const result = new Y();
actions$.stream = hot('-x', { x: action });
const expected = cold('-y', { y: result }); // ? adding frames or 3s doesn't work
expect(effects.someEffect$).toBeObservable(expected);
In return I get
Expected $.lenght = 0 to equal 1.
Here are some links to more information about marble testing with NgRx: In summary, we can use marble-diagram-like strings to describe an observable stream over time. This enables us to synchronously test asynchronous observable streams. There are two primary functions that we will be using: hot () creates a hot observable stream.
Angular handily provides us with the fakeAsync function which lets us control the flow of time. delay has its concept of time based on the scheduler it uses, so in order to test this with marbles we would have to (somehow) tell it that we want to use the TestScheduler alongside hot and cold rather than the default async Scheduler.
If you have RxJS code that uses asynchronous scheduling - e.g. Promises, etc. - you can't reliably use marble diagrams for that particular code. This is because those other scheduling methods won't be virtualized or known to TestScheduler.
Rx Marbles is a standard for defining observable streams. We use it as an easy way to generate an observable stream. To create them there are some symbols you need to know: You can create either a cold or a hot observable with Marbles. These are created with the functions: cold and hot “-” is 10 frames, for indicating time has passed.
If you dont want to pass scheduler to delay you can also do sth like:
import { cold, hot, getTestScheduler } from "jasmine-marbles";
const scheduler = getTestScheduler();
scheduler.run(helpers => {
const action = new X();
const result = new Y();
actions$ = helpers.hot('-x', { x: action });
helpers.expectObservable(effects.someEffect$).toBe('- 3s y', { y: result });
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With