ngrx / store

RxJS powered state management for Angular applications, inspired by Redux
MIT License
3.9k stars 310 forks source link

Injecting a New Store by Hand #427

Closed TomPridham closed 7 years ago

TomPridham commented 7 years ago

I'm trying to test a service that has an ngrx store injected by Angular's injector. I'm not clear on how to actually inject a store by hand. Everything I've seen goes into injecting ngrx into a component with a module.

All I want to do is something like new service(someDependency, new Store(reducers, defaultState)), but Typescript complains that the signature doesn't match. What is the correct way to do this?

Thanks.

jotatoledo commented 7 years ago

Why dont you just inject the store into the service where you require it? :confused:

TomPridham commented 7 years ago

What would that look like?

jotatoledo commented 7 years ago

Im not entirely sure if u can directly inject the store instance into a service. You should try. If not, you could try to configurate a custom factoryMethod for the service that depends on the store. See here for the second

brandonroberts commented 7 years ago

@TomPridham are you using the TestBed? If so, just provide the TestBed.configureModule with the StoreModule.provideStore(reducer), then you get can get the instance of the store using TestBed.get(Store)

TomPridham commented 7 years ago

Sorry for the late reply, I'm just getting back to implementing these tests. That put me on the right track, Brandon. I ended up doing imports:[StoreModule.provideStore(rootReducer, defaultState)]

Thanks for the help!