I'm working on a Angular 6 application with NGXS and I'm struggling with some tests...
One of our asynchronous action was returning a subscription, something like :
@Action(MyActions.MyAction, {cancelUncompleted: true})
myAction(ctx: StateContext<MyModel>) {
return this.someHttpCall().subscribe(xxx => ctx.patchState({value: xxx}));
}
It was working well but the cancellation mechanism was not working because NGXS want us to return an observable and not a subscription so I fixed it following the guidelines (https://www.ngxs.io/advanced/actions-life-cycle#asynchronous-actions)
@Action(MyActions.MyAction, {cancelUncompleted: true})
myAction(ctx: StateContext<MyModel>) {
return this.someHttpCall().pipe(tap(xxx => ctx.patchState({value: xxx})));
}
Now the cancellation mechanism is working but some tests failed because it seems the state is never patched.
I checked those links :
https://www.ngxs.io/recipes/unit-testing#testing-asynchronous-actions
Pipe and Tap VS subscribe with ngxs
And it seems I have to subscribe to the returned observable if I want the tap to be triggered so in my test I did something like this :
store.dispatch(new MyAction()).subscribe()
The subscribe in the test (last code) is called, the state is also called but the "tap" still never called...
I also try to change the state code to :
@Action(MyActions.MyAction, {cancelUncompleted: true})
myAction(ctx: StateContext<MyModel>) {
return this.someHttpCall().pipe(tap(xxx => ctx.patchState({value: xxx}))).subscribe()
}
It works (the "tap" is called and the state is patched) but obviously the cancel mechanism is not working anymore...
Thanks in advance :-)