I've been working on a unit test for the subscribe call, but I'm struggling to cover the error handling aspect of the subscribe method. The handleError function deals with statusCode=403 errors and other status codes. Any assistance would be greatly appreciated!
component.ts file
getData(){
this.someService.getResponse().subscribe(res=>{
this.showForm = true;
this.Form.patchValue({
flag:res && res.someflag? "true" : "false"
})
}, (error: HttpErrorResponse) => {
this.handleError(error);
});
}
handleError() function in component.ts file
handleError(error){
this.showForm=false;
if(error.error && error.error.statusCode === 403){
this.itemError=false;
this.overlayError=true;
}
else{
this.itemError=true;
this.overlayError=false;
}
}
component.spec.ts file
I attempted the following test case, but it doesn't fully cover the scenario.
it('should handle error for 403',async()=>{
const param={statusCode:403};
const errorResponse=new Error('403 errror');
spyOn(component["someService"],"getResponse").and.returnValue(throwError(errorResponse));
component.handleStatusCodeError(param);
expect(component.showForm).toBe(false);
expect(component.itemError).toBe(false);
expect(component.overlayError).toBe(true);
})