Reputation: 111
I'm trying to test a component which has injected a Store. The test does not pass and gives me the following error: "Cannot read property 'ids' of undefined"
Test:
const initialState = {
quoteInfo: {
quoteLoaded: true,
ids: [0],
entities: {
'0': {
id: 0,
quoteId: 'string',
quoteStatus: 0,
reference: 'ref01',
}
}
},
quoteSettings: {
quoteSettingsReceived: true,
ids: [1],
entities: {
'1': {
id: 1,
status: 0,
customer: 'string',
customerId: 'cus01',
carrier: 'car01',
contact: 'contact01',
contactId: 'c01',
priceCategory: 'vip',
priceCategoryId: 'pr1',
creationDate: null,
expirationDate: null,
deliveryDate: null,
currency: null,
currencyId: null,
paymentDescription: 'pay desc',
tax: 0,
comments: 'comments',
shippingAddress: null,
billingAddress: null
}
}
}
};
Test beforeEach:
beforeEach( async(() => {
TestBed.configureTestingModule({
declarations: [GeneralComponent],
imports: [
BrowserAnimationsModule,
HttpClientModule,
StoreModule.forRoot({ feature: combineReducers({quoteSettings: settingsReducer, quoteInfo: infoReducer }, initialState)}),
],
providers: [
]
}).compileComponents();
}));
The error appears in component initialization which code is the following:
ngOnInit() {
this.randomObservable$ = this._store.pipe(select(randomSelector));
}
Random Selector
export const randomFeatureSelector= createFeatureSelector<RandomState>('random');
export const randomSelector = createSelector(
randomFeatureSelector, // this is null
fromRandomReducer.selectAll
);
Upvotes: 4
Views: 2908
Reputation: 111
The problem was in the html file were I was using the observable from the selector directly and if the store is still not initialized, it gives that error
Upvotes: 2
Reputation: 2146
For mocking Store I usually use BehaviorSubject from rx.js:
let mockStore;
beforeEach( async(() => {
mockStore = new BehaviorSubject(initialState);
TestBed.configureTestingModule({
declarations: [GeneralComponent],
imports: [
BrowserAnimationsModule,
HttpClientModule
],
providers: [
{provide: Store, useValue: mockStore}
]
}).compileComponents();
}));
...
Upvotes: 0