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);
});
});
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...
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();
});
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