NgRx Singals - How to test signal store?
Asked Answered
R

0

8

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:

  1. How to mock store initial state?
  2. 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

Ruthanneruthe answered 2/1 at 16:38 Comment(1)
I'm having the same problem and for now my only solution has been to move anything from inside the "onInit" hook into a separate function in the signal store and run unit test on that.Teyde

© 2022 - 2024 — McMap. All rights reserved.