I've been looking for information on how to test NgRX singalStore I use in my project. Unlike @ngrx/store, there is no documentation regarding testing (store testing link).
With presented code:
export const store = signalStore(
{ providedIn: 'root' },
withState(initialState),
withComputed(() => ({
// ...
})),
withMethods(state => {
// ...
}),
withHooks({
onInit() {
// ...
},
}),
);
I'd like to know:
- How to mock store initial state?
- How to test withHooks part?
About 1. I could potentially set state using my state methods, but this is not the best approach. Also, I found out that you can make class-based signalStore in this FAQ, so perhaps I can pass my mockedInitialState there.
Edit: After playing, I managed to wrap this signalStore into a function that gets initialState as an argument, defaulting to my desired initialState.
export const TaskStore = (state = initialState) =>
signalStore(..)
About 2. I can think of mocking a component, running ngOnInit in it then expecting state changes.
Do you guys have other ideas, or maybe working examples?
There is an open issue about missing testing docs link