When testing a function, I encountered an issue where a variable is created and assigned a value, but the payload constant always remains undefined.
campaigns-card.component.ts
async ngOnInit() {
const { uid } = await this.auth.currentUser
const { payload } = await this.firestore.surveysUsers(this.survey.id).doc(uid).snapshotChanges().pipe(first()).toPromise();
this.completed = payload.exists
}
component-card.component.spec.ts
beforeEach(waitForAsync(() => {
fireStoreServiceMock.surveysUsers.and.returnValue({
doc: () => { return {
snapshotChanges: () => { return {
pipe: () => { return {
toPromise: () => { return { exists: true }}
}}
}}
}}
});
}));
describe('tests in delete', () => {
it('should update surveys', fakeAsync(() => {
fixture.detectChanges();
component.delete().then(() => {
});
fixture.detectChanges();
tick();
expect(fireStoreServiceMock.surveys).toHaveBeenCalled();
expect(snackBarMock.open).toHaveBeenCalled();
}));
});
Error Message:
Error: Uncaught (in promise): TypeError: Cannot read property 'exists' of undefined
TypeError: Cannot read property 'exists' of undefined
at CampaignsCardComponent.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/modules/campaigns/pages/campaigns-card/campaigns-card.component.ts:43:4)
<Rest of error stack trace>