Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngrx + marble testing + delay

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. 
like image 667
Cor Kalom Avatar asked Nov 28 '18 13:11

Cor Kalom


People also ask

How do I use marble testing with ngrx?

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.

How to test delay with marbles in Angular 2?

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.

Why can't I use marble diagrams with RxJS?

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.

How to create an observable stream in RX marbles?

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.


1 Answers

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 });
})
like image 138
bys Avatar answered Oct 12 '22 21:10

bys