Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test an effect that returns multiple actions

I have an effect that returns action A then action B

@Effect() myEffect$: Observable <Action> = this.actions$
  .ofType('MY_ACTION')
  .switchMap(() => Observable.of(
    // subscribers will be notified
    { type: 'ACTION_ONE' },
    // subscribers will be notified (again ...)
    { type: 'ACTION_TWO' }
  ));

How can I test the two successive returned actions ?

it('should return action one then action two', () => {
  runner.queue(new myAction());
  const expectedResult = twoSuccesiveActions;
  sessionEffect.myEffect$.subscribe(result => {
    // how do I test those two succesively returned actions
    expect(result).toEqual(expectedResult);
  });
});
like image 938
Lev Avatar asked Mar 14 '26 22:03

Lev


2 Answers

You could use one with take(1) and one with skip(1):

it('should return action one then action two', () => {
  const expectedResult = twoSuccesiveActions;
  sessionEffect.myEffect$.take(1).subscribe(result => {
    // first action
    expect(result).toEqual(expectedResult);
  });

  sessionEffect.myEffect$.skip(1).take(1).subscribe(result => {
    // second action
    expect(result).toEqual(expectedResult);
  });

  runner.queue(new myAction());
});

In any case I would suggest you to use take(1) if you don't manually unsubscribe to ensure no leaks into other tests ect...

like image 61
olsn Avatar answered Mar 18 '26 06:03

olsn


In case someone is still wondering how to do it, this is another way to do it

effects.myEffect$
  .pipe(
    bufferCount(2)
  )
  .subscribe((emittedActions) => {
    /* You could also include here callings to services
        verify(myServiceMock.execute(anything()))
          .called();
    */
    expect(emittedActions.map((action) => action.type))
      .toEqual([
        myFirstAction,
        mySecondAction,
      ]);
    done();
  });
like image 40
yngrdyn Avatar answered Mar 18 '26 05:03

yngrdyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!