Angular Spectator Testing ErrorHandler HttpErrorResponse

860 Views Asked by At

Summarize the problem: I want to test my custom ErrorHandler with Spectator and Jest. I expected to pass a simple test where a specific method handleFailFastError gets called one time for a HttpErrorResponse with status code 500. Actual result is that the HttpErrorResponse gets handled as a TypeError within another method.

Describe what you've tried: I checked if I am inside the method and yes I am. Checked with console.log calls and printed out the error. It is an {castToWriteable: [Function]}Object. In consequence my condition to check if it is an instance of HttpErrorResponseis failing and my handleError handles the Response as an TypeError. Orientated my self on this example. Also checked this stackoverflow entry out.

Show some code ErrorHandler:

    @Injectable()

    export class ErrorHandlerInterceptor implements ErrorHandler, OnDestroy {

    private _destroy$ = new Subject();

    constructor(
        private _injector: Injector
    ) {
    }

    public ngOnDestroy(): void {
        this._destroy$.next();
        this._destroy$.complete();
    }

    public handleError(error: Error | HttpErrorResponse): void {
        console.log('Did i get called?', error);
        const failFast: number[] = [401, 403, 500];
        if (error instanceof HttpErrorResponse) {
            if (navigator.onLine) {
                        if (failFast.includes(error.status)) {
                            handleFailFastError();
                        }
                        if (error.status === 409) {
                            //handleConflictError();
                        }
                        if (error.status === 400) {
                            //handleBadRequestError();
                        }
                        //handleAnythingElse();
            } else {
                //Errors, when the user is offline
                //handleOfflineError();
            }
        } else {
            //Type errors
            console.log('Type Error',error);
            //handleTypeError();
        }
    }

    //functions
    public handleFailFastError(): void {
        console.log('Handled 500');
    }

    }

My test so far using spectator and jest:

    import {ErrorHandlerInterceptor} from './error-handler.interceptor';
    import {createServiceFactory} from '@ngneat/spectator';
    import {createSpyObject, SpectatorService} from '@ngneat/spectator/jest';
    import {HttpErrorResponse} from '@angular/common/http';
    import {CoreModule} from '@app/core/core.module';

    describe('ErrorHandlerInterceptor', () => {
        const createErrorHandler = createServiceFactory({
            service: ErrorHandlerInterceptor,
            imports: [CoreModule],
            mocks: []
        });

        let specErrorHandler: SpectatorService<ErrorHandlerInterceptor>;
        const dummyError = createSpyObject(HttpErrorResponse);

        const handleError = (error: HttpErrorResponse): void => {
            specErrorHandler.service.handleError(dummyError);
        };

        beforeEach(() => specErrorHandler = createErrorHandler());
        it('should create the error handler', () => {
            const errorHandler = specErrorHandler.service;
            expect(errorHandler).toBeTruthy();
        });

        it('at least console should be called 2 times', () => {
            const spyConsole = spyOn(console, 'log');
            [500].forEach(status => {
                handleError(new HttpErrorResponse({status}));
                expect(spyConsole).toHaveBeenCalledTimes(5);
            });
        });

        it('should redirect, and delete sessionStorage', () => {
            [500].forEach(status => {
                handleError(new HttpErrorResponse({status}));
                expect(specErrorHandler.service.handleFailFastError).toBeCalled();
            });
        });
    });
0

There are 0 best solutions below