[Using angular and ngrx.]
I'm using jest and jest-marbles for testing a service. I'm using the store inside this service. The service depends on a boolean value from the store (isDisplayItems).
If it is true, only then the other functions inside the service works.
So I need to mock a value so that I can make it working. It will be possible only if I can provide a fake value like this.
jest.spyOn(mockStore.pipe(select(s => s.isDisplayItems))).and.returnValue(true)
The idea here is to return a fake/stub value for a particular selector, so that I can test service.
it('should make items visible', () =>
{
jest.spyOn(mockStore.pipe(select(s => s.isDisplayItems))).and.returnValue(true)
});
How can I achieve that?
This is my test bed setup
beforeEach(() =>
{
TestBed.configureTestingModule({
providers: [provideMockStore({ initialState }),],
});
service = TestBed.inject(FilterService);
mockStore = TestBed.inject(MockStore);
});
Please follow ngrx docs how to test them: https://ngrx.io/guide/store/testing#using-mock-selectors
There's mockStore.overrideSelector
for that.
mockStore.overrideSelector(
yourSelectorHere,
valueYouWantToReturnAsResult
);
for example
mockStore.overrideSelector(
currentUser,
null,
);
this.store.select(currentUser).subscribe(console.log); // prints null.