ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
8.02k stars 1.97k forks source link

Unit Testing (mocking) watchState with Signal Store #4553

Open tgrux opened 1 day ago

tgrux commented 1 day ago

Information

Context: We are using the new Signals Store with Angular v18 and I have a component that is listening to state like so:

constructor() {
    watchState(this.quoteDraftStore, (state) => this.currentStateSignal.set(state));
}

When unit testing the component I prefer to mock the store because we have a lot of logic in our store methods (including the HTTP Client). When mocking the store I can't figure out how to deal with watchState. The testing documentation doesn't have any information about this (at least I can't find it).

I'd be happy to submit a PR for this, but I don't know the answer to it.

Documentation page

https://ngrx.io/guide/signals/signal-store/testing

I would be willing to submit a PR to fix this issue

rainerhahnekamp commented 1 day ago

@tgrux I'd say you would have to come up with a mock, which is itself a minimal version of your real SignalStore. That is, because watchState requires that the state update happens via patchState.