I have a smart component test for my NgRx implementation that looks something like this:
describe( 'Component', () => {
let store: MockStore<State>;
beforeEach( async( () => {
TestBed.configureTestingModule( {
/* ... */
providers: [
provideMockStore( { initialState: fromReducer.initialState } )
]
} ).compileComponents();
store = TestBed.get<Store<State>>( Store );
} ) );
it( 'should load items in #ngOnInit', () => {
store.setState( {
item: {
...fromReducer.initialState,
entities: { [item.id]: item },
},
otherFeature: null,
otherFeature: null,
otherFeature: null
} );
component.items$.subscribe( items =>
store.select( ItemStoreSelectors.selectItems ).subscribe( fromStore => expect( items ).toEqual( fromStore ) )
);
} );
});
I use provideMockStore
and setState
to mock my NgRx state. Everything works fine this way, but I really don't like this part of it:
store.setState( {
item: {
...fromReducer.initialState,
entities: { [item.id]: item },
},
otherFeature: null,
otherFeature: null,
otherFeature: null
} );
I have to add every other feature slice of my state to the setState
function. Otherwise Typescript will throw Errors.
So preferably I don't want to set the root state, but rather a specific feature slice like this:
store.setState( {
...fromReducer.initialState,
entities: { [item.id]: item },
} );
I couldn't find any documentation on how to use provideMockStore
for specific slices of the state here.