I'm trying to write unit tests together with Angular, Jest and Spectator. Right now I'm struggling with mocking NgRx store. I'm getting a "No provider for MockStore" error when trying to get an instance of the NgRx provided MockStore
from the Spectator instance.
I've read the NgRx documentation regarding mocking and unit tests and I'm able to mock NgRx store without using Spectator by reading the documentation, I've tried Googling around for help but can't seem to find examples of mocking NgRx store together with Spectator and I've left a message on NgRx/store Gitter to see if anyone there is able to help me.
Here's what my code looks like:
let component: MyComponent;
let spectator: Spectator<MyComponent>;
let mockStore: MockStore<selectors.AppState>;
let mockUsernameSelector: MemoizedSelector<AppState, string>;
const createComponent = createComponentFactory({
component: MyComponent,
componentProviders: [
provideMockStore(),
],
shallow: true,
detectChanges: false,
});
beforeEach(() => {
spectator = createComponent();
component = spectator.component;
mockStore = spectator.inject<MockStore<selectors.AppState>>(MockStore);
mockUsernameSelector = mockStore.overrideSelector(selectors.selectUserName, 'Bob');
});
But as I said, as soon as I try to get instance of the MockStore
from Spectator via spectator.inject
(also tried spectator.get
), I get the "No provider for MockStore" -error. I've also tried to provide the MockStore
like this:
const createComponent = createComponentFactory({
component: MyComponent,
componentProviders: [
mockProvider(MockStore, provideMockStore()),
],
shallow: true,
detectChanges: false,
});
But then I get the error "No provider for Store", obviously.
Perhaps someone here is able to help me? Thanks in advance!
You can use convienient mockProvider function.
In providers array: