I have been stuck on this issue, which I do not now how to resolve or find a solution for.
I have an angular service which I use to get a day entry from a shopping list from my NGXS store.
However, it throws an error in the ng test saying "Cannot read property 'undefined' of undefined"
The code in the service looks like this, fairly simple.
getDataFromStore(daySelected: string): DailyShopList {
return this.store.selectSnapshot<DailyShopList>(
state => state.shoppingList.dailyEntries[daySelected] <--- issue is here
);
}
If you look at the line state => state.shoppingList.dailyEntries[daySelected], this is where I get the error. When I remove the [daySelected] from the end of the object, I get no errors.
So, in my test file, I tried to mock the data as it would come from the store. But it still throws the error.
describe('ShoppingListService', () => {
let service: ShoppingListService;
const data = {
state: {
shoppingList: {
dailyEntries: {
mon: {},
tue: {},
},
},
},
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
NgxsModule.forRoot([MyNGXSstate]),
],
});
service = TestBed.inject(ShoppingListService);
const store: Store = TestBed.get(Store);
spyOn(store, 'selectSnapshot').and.returnValue(data); // trying to mock data from store
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
What am I missing?
Thank you.