I'm trying to test an Interceptor that modifies the response from an HTTP Request.
Here's my current code:
@Injectable()
export class ResponseCamelCaseInterceptor implements HttpInterceptor {
intercept(
httpRequest: HttpRequest<Record<string, unknown>>,
httpHandler: HttpHandler,
): Observable<HttpEvent<Record<string, unknown>>> {
return httpHandler.handle(httpRequest).pipe(
filter(
(value): value is HttpResponse<Record<string, unknown>> =>
value instanceof HttpResponse,
),
filter(({ body }) => isPlainObject(body)),
map(httpEvent =>
httpEvent.clone({ body: snakeToCamelCase(httpEvent.body) }),
),
);
}
}
and its corresponding test file I have so far:
describe(ResponseCamelCaseInterceptor.name, () => {
const createService = createServiceFactory(ResponseCamelCaseInterceptor);
test('some description', () => {
const { service } = createService();
const fakeHttpRequest = new HttpRequest('POST', 'https://...', { custom_key: '1' });
service.intercept(fakeHttpRequest, 'what should I put here for HttpHandler?').subscribe(() => {
// expect(httpResponse.body).toStrictEqual({ customKey: '1' });
});
});
});
Note that I'm using Angular 10.x.y, Jest 26.x.y and Spectator 5.x.y.