I have this class sample that is using ngrx store an other services in the constructor, What is the best way to pass the store in the constructor when I am testing this class in jest?
This class is not a component or injectable.
export class Sample {
private users;
constructor(
private readonly userService: UserService,
private readonly store: Store
) { }
public setUsersDataFromStore() {
this.store.select(selectUsers).pipe(
filter((user) => !!users),
tap((users => (this.users = users)))
).subscribe();
}
}
Unit test.
fit('should Sample Object defined', () => {
const sampleObj = new Sample(
mockUserService any,
initialState as any
);
sampleObj.setUsersDataFromStore();
expect(sampleObj.users).toBeDefined();
});
But the unit test is failing the selector is not getting the data from the initial state from the constructor, the initial state is a simple object, also I tried with the object in an observable of({initialState}), what is the best option to fix?
You can use
getMockStoreto create a mocked instance of the store, I briefly touch on it on this subject in my blog post Testing an NgRx project.