I’m back with another question on how to mock a complex call from AngularFireStore.
I’m hitting an error when executing my tests when calling the importData function in my service. The final get call should return an array that I’m checking, but I can’t get the mock right so the test is blowing up.
I’ve tried several variations on the mock file, but I can’t figure out how to get through this issue where it can’t find ‘where’.
TypeError: Cannot read properties of undefined (reading ‘where’)
service.ts
importData(Id: number) {
this.afs.collection<data>('users/' + this.authService.liveUid + .ref.where('id', '==', Id).get().then(
a => {
if (a.docs.length == 0) {
if (validID(Id, 8, 9)) {
this.http.get<DataImport>('https://url' + Id).subscribe(
//do stuff
)
} else
{
this.urlError = true
this.spinnerImported = true
}
}
else {
this.spinnerImported = true
this.sameImportName = a.docs[0].get('name')
}
}
)
}
service.spec.ts
describe('ImportService', () => {
let service: ImportService;
beforeEach(() => {
TestBed.configureTestingModule(
{
providers: [
{ provide: HttpClient, useValue: httpClientMock },
{ provide: AngularFirestore, useValue: AngularFirestoreMock },
{ provide: AuthService, useValue: AuthServiceMock },
]
});
service = TestBed.inject(ImportService);
});
it('should be created', () => {
service.importData(32198712)
});
});
AngularFiresoreMockt
export const AngularFirestoreMock = jasmine.createSpyObj('AngularFirestoreMock', {
'doc': {
set: function () {
return Promise.resolve()
}
},
'collection':{
get: function () {
return Promise.resolve()
}
}
}
)