I have been using NGXS widely across an application but there some things that I still can't do in a proper way and the documentation or other questions asked here haven't helped so far. One of such things is regarding the testing of actions, specifically testing that an action was called from within another action. Check the following:
store.ts
@Action(ActionToBeTested)
public doActionToBeTested(
ctx: StateContext<MyState>,
): void {
const state = ctx.getState();
// update state in some way
ctx.patchState(state);
this.restApiService.save(state.businessData)
.pipe(
mergeMap(() =>
this.store.dispatch(new SomeOtherAction(state.businessData)),
)
)
.subscribe();
}
store.spec.ts
it('should test the state was update, restapi was called and action was dispatched', async(() => {
store.reset({...someMockState});
store.dispatch(new ActionToBeTested());
store
.selectOnce((state) => state.businessData)
.subscribe((data) => {
expect(data).toBe('Something I expect');
expect(restApiServiceSpy.save).toHaveBeenCalledTimes(1);
});
}));
With this I can test that the state was updated and the rest service was called but I simply cannot test that the action SomeOtheAction
was dispatched. I have tried a lot of things already since trying to spy on the store after dispatching the first action (kind of crazy I know) to what is described in this answer but with no success.
Has anyone came across similar difficulties and found a solution. If so, please, point me in the correct direction and I'll be happy to walk the path myself. Thank you