I am working on the unit testing,i have one function which calls multiple API in one shot.I have written spyOn on the service function but I am not able to return multiple response.Can any one please suggest where i am missing ?
component.ts
loadMappedVendorDoc() {
this.destroySearch$ = this.apiServ.cloneSubscribe();
this.isDataLoading = true;
const curdAPIsJoin = [
this.apiServ.awsGet('api_path_one),
this.authSer.awsGet('api_path_two')];
forkJoin(curdAPIsJoin).pipe(takeUntil(this.destroySearch$)).subscribe(reps => {
this.isDataLoading = false;
if (reps[0].status_code === '200') {
this.originalmappedDocTypeData = JSON.parse(JSON.stringify(reps[0].response));
this.mappedDocTypeData = reps[0].response;
} else {
this.mappedDocTypeData = [];
}
if (reps[1].status_code === '200') {
this.authSer.ihsDocType = reps[1].response;
} else {
this.authSer.ihsDocType = [];
}
});
}
Note: awsGet returns the observable
component.spec.ts
it('loadMappedVendorDoc shoud call', fakeAsync(() => {
spyOn(apiService, 'awsGet').and.returnValue(of([{ status_code: '200', response: mappedStub }, { status_code: '200', response: mappedStub2 }]));
component.originalmappedDocTypeData = mappedStub;
component.mappedDocTypeData = mappedStub;
component.loadMappedVendorDoc();
fixture.detectChanges();
tick(1000);
expect(component.mappedDocTypeData.length).toBe(3);
// flush();
}));
Note: mappedStub is dummy response
Problem Statement:In code coverage if block is not executing because of that data is not displaying on the page.
Achieved using following code block
spyOn(apiService, 'awsGet')
.withArgs('api_path_one').and.returnValue(of({ status_code: '200', response: modalInfoStub }))
.withArgs('api_path_two').and.returnValue(of({ status_code: '200', response: doctype }));
find more about withArgs