I am attempting to flush a response with an http 400 error using the HttpTestingController . However it does not appear to ever be flushed, as the delegate of the subscription is never called.
Logging the _cancelled property of the request object indicates that returning an http error of 400 is causing the _cancelled property to be set to true. I would like an error response to be passed into the service method so that it's handling of http error codes can be tested.
The test:
it('routes to error page when get roles receives an error', async(inject(
[AgentManagementService, HttpTestingController],
(service: AgentManagementService, backend: HttpTestingController) => {
service.GetAgentPortalRoles().subscribe( (response) => {
console.log(response); // never gets called
expect(1).toBe(1);
});
const request = backend.expectOne(environment.urlBase + 'v1/account/roles');
expect(request.cancelled).toBeFalsy();
console.log(request.cancelled); // is true
request.flush( {}, { status: 400, statusText: 'Bad Request'});
console.log(request.cancelled); // is false
}
)));
The function in the service being tested:
public GetAgentPortalRoles(): Observable<Array<string>> {
return this.http.get<RolesResponse>(API_URL + 'v1/account/roles', { observe: 'response', headers })
.pipe(
map( response => {
if (response.ok) {
return response.body.roles;
}
this.router.navigateByUrl('/error');
return null;
}),
catchError( () => {
this.router.navigateByUrl('/error');
return EMPTY;
})
);
}