Reputation: 5435
I have two files: x.component and x.service
x.service.ts
store$: Store<DogsStore>;
constructor(private store: Store<DogsStore>) {
this.store$ = store;
}
x.component.ts
constructor(private xService: X) {}
ngOnInit() {
this.xService.store$.select('something').subscribe(...)
}
Testing x.component
I'm getting an error that cannot subscribe of undefined
. In x.spec.ts
I'm using x.service.spy.ts where I have
store$: Store<DogsStore>;
as you can see I didn't initialize it because I can't use something like store$: Store<DogsStore> = new Store()
. How can I get the store and assign it to variable? Maybe there is other issue to do that?
Upvotes: 1
Views: 800
Reputation: 14375
If I got the question right, here is how to do it:
Basically, your app module provides the store, yet your test doesn't. So in your spec file, part of the beforeEach
should provide it by importing it:
TestBed.configureTestingModule({
imports: [
SomeModule,
StoreModule.provideStore(reducer), // or your reducer name
....
],
....
});
and then spy on it (still in the beforeEach
call):
spyOn(getTestBed().get(Store), 'dispatch').and.callThrough();
This way your component will get it.
Now to get access to the dispatch wrapper you should inject it in the test:
it('test something', inject([Type1, Type2, Store],
(dep1: Type1, dep2: Type2, store) => {
expect(store.dispatch.calls.count()).toEqual(<count_value>);
}
Now note that in the method signature I left store untyped, this is because otherwise the static checker complains that store.dispatch
has no member calls
Upvotes: 1