I have written unit test cases using karma and jasmine ,everything works fine except whatever code written inside the subscribe block is not covered in code coverage.
component.ts function which is testing.
updateDocType(uRec, rType) {
console.log(uRec)
const obj = {
vendor_mapping_list: uRec
};
const remainingItems = [];
const executedItems = [];
this.destroySearch$ = this.apiServ.cloneSubscribe();
this.recordStatusMsg = 'Saving record';
this.apiServ.awsPost('api_URL', obj)
.pipe(takeUntil(this.destroySearch$))
.subscribe((res) => {
if (res.status_code === '200') {
this.recordStatusMsg = 'Record saved';
const clearIt = setTimeout(() => {
this.isToBeMappedEditMode = false;
this.isMappedEditMode = false;
this.recordStatusMsg = 'Viewing record';
clearTimeout(clearIt);
}, 1000);
this.isChangeInData = true;
if (rType === 'to_be_mapped') {
this.dataMaintenanceGrid.api.forEachNodeAfterFilterAndSort((rowNode, index) => {
if (rowNode.data.isUpdated) {
executedItems.push(rowNode.data);
} else {
remainingItems.push(rowNode.data);
}
});
this.originalMaintenanceData = remainingItems;
this.maintenanceData = JSON.parse(JSON.stringify(this.originalMaintenanceData));
this.dataMaintenanceGrid.api.updateRowData(this.maintenanceData);
executedItems.map(o => {
o.isUpdated = false;
this.originalmappedDocTypeData.push(o);
});
this.mappedDocTypeData = JSON.parse(JSON.stringify(this.originalmappedDocTypeData));
this.mappedVendorGrid.api.updateRowData(this.mappedDocTypeData);
this.defaultColEditDef = false;
this.isToBeMappedItemChanged = false;
this.isToBeMappedMultiRowSelected = false;
} else {
this.mappedDocTypeData.map(r => {
if (r.isUpdated) {
r.isUpdated = false;
}
});
this.originalmappedDocTypeData = JSON.parse(JSON.stringify(this.mappedDocTypeData));
this.mappedVendorGrid.api.setRowData(this.mappedDocTypeData);
this.defaultColEditMappedVenDef = false;
this.isMappedItemChanged = false;
}
}
});
}
spec.ts file unit test code
it('updateDocType calling', fakeAsync(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
// apiService.awsPost.and.returnValue(of({
// status_code: "200",
// response: []
// }))
// const source: Array<Observable<any>> = [of([{ status_code: '200', response: mappedStub }]), of([{ status_code: '200', response: mappedStub }])];
spyOn(apiService, 'awsGet').and.returnValue(of({
status_code: "200",
response: []
}));
component.updateDocType([], 'to_be_mapped');
tick();
const res={
status_code: "200",
response: []
};
fixture.whenStable().then(() => {
expect(true).toBeTrue();
flush();
});
flush();
})
tick(2000)
}));
Problem statement:Everything works fine test cases executed properly but whatever code written inside the subscribe block that will not added in code coverage.
Its showing function not covered.
CodePudding user response:
You are spying awsGet
, but in the code it shows awsPost
.