mercredi 22 janvier 2020

Angular test: Cannot read property 'length' of undefined while testing interceptor

I try to test that my interceptor is adding header 'KONTEKST' with proper value but when I try to use has() method from HttpHeaders I recive error:

TypeError: Cannot read property 'length' of undefined
    at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.applyUpdate (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm5/http.js:200:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm5/http.js:171:60
    at Array.forEach (<anonymous>)
    at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.init (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm5/http.js:171:1)
    at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.has (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/common/fesm5/http.js:121:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/http-interceptors/routes-interceptor.spec.ts:33:40)
    at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:391:1)
    at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:289:1)
    at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:390:1)
    at Zone../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:150:1)

My interceptor:

import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {ActivatedRoute, Router} from '@angular/router';
import {Observable} from 'rxjs';
import {Context} from '../common-enum/context.enum';
import {KontekstService} from '../common-services/kontekst/kontekst.service';

@Injectable({
  providedIn: 'root'
})
export class RoutesInterceptor implements HttpInterceptor {

  constructor(private router: Router, private route: ActivatedRoute, private kontekstService: KontekstService) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const kontekst: Context = this.kontekstService.get();
    if (Context.UKNOWN === kontekst) {
      this.router.navigate(['error'], { queryParams: {message: 'Błędny kontekst usługi'}, skipLocationChange: true});
    } else {
      req = req.clone({
        setHeaders: {
          'KONTEKST' : kontekst
        }
      });
      return next.handle(req);
    }
  }

}

My spec.ts:

import {TestBed} from '@angular/core/testing';
import {HTTP_INTERCEPTORS, HttpClient, HttpParams, HttpRequest} from '@angular/common/http';
import {RoutesInterceptor} from './routes-interceptor';
import {COMMON_TEST_IMPORTS} from '../common-testing';
import {HttpTestingController} from '@angular/common/http/testing';
import {SterowanieLikwidacjaService} from '../domain-clients/sterowanie-likwidacja/sterowanie-likwidacja.service';

fdescribe('RoutesInterceptor', () => {

  let service: SterowanieLikwidacjaService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [...COMMON_TEST_IMPORTS],
      providers: [SterowanieLikwidacjaService, {
        provide: HTTP_INTERCEPTORS,
        useClass: RoutesInterceptor,
        multi: true
      }]
    });
    service = TestBed.get(SterowanieLikwidacjaService);
    httpMock = TestBed.get(HttpTestingController);
  });

  it('should add an KONTEKST header', () => {
    service.pobierzPodstawoweDaneTypuSzkody('12345').subscribe(response => {
      expect(response).toBeTruthy();
    });

    const httpRequest = httpMock.expectOne('/api');
    console.log(httpRequest.request.headers);
    expect(httpRequest.request.headers.has('KONTEKST')).toEqual(true);

  });

});

Printed httpRequest.request.headers:

headers: Map(0)
  size: (...)
  __proto__: Map
  [[Entries]]: Array(0)
    length: 0
lazyInit: null
lazyUpdate: Array(1)
  0:
    name: "KONTEKST"
    op: "s"
    value: undefined
    __proto__: Object
    length: 1
  __proto__: Array(0)
normalizedNames: Map(0)
  size: (...)
  __proto__: Map
  [[Entries]]: Array(0)
    length: 0

Is anybody have any idea where I made mistake? Why in printed HttpHeaders value of 'KONTEKST' is undefined?

Aucun commentaire:

Enregistrer un commentaire