mardi 27 novembre 2018

How to perform unit tests from onMapReady() in ngx-leaflet (Angular 2)

I have a pretty complex map component built with ngx-leaflet in a big Angular 2 app. I'm trying to write unit tests but can't figure out how to get onMapReady() to 'run' in my test (component.spec.ts) file.

My HTML code for the map itself is pretty straightforward:

<div class="map"
     leaflet
     [leafletOptions]="mapOptions"
     [leafletLayers]="layers"
     [leafletFitBounds]="fitBounds"
     [leafletLayersControl]="layersControl"
     [leafletLayersControlOptions]="layersControlOptions"
     (leafletMapReady)="onMapReady($event)">
</div>

The corresponding TS file:

onMapReady(map: L.Map) {
    this.leafletMap = map;
    ...
}

onMapReady is filled with a bunch of different map code, some observables, etc. For the map state to persist as the user navigates to other pages within the app, the code has to be in onMapReady.

My test file, again, is pretty standard. Here's a snippet of the beforeEach method:

beforeEach(() => {
  fixture = TestBed.createComponent(OspMapComponent);
  component = fixture.componentInstance;
  ...
  component.ngOnInit();
  fixture.detectChanges();
});

As I understand it, including component.ngOnInit() in the beforeEach method runs all of the code within the component's ngOnInit method. I need to do the same for the onMapReady method, but I'm confused about how to handle it and its $event object.

Including component.onMapReady(); in beforeEach gives the error 'Expected 1 arguments, but got 0.' Including component.onMapReady(map: L.map); gives the error 'Expected 1 arguments, but got 2.' Several variations of this haven't worked for me. Is there a way to run all of the contents of onMapReady in the test file properly?

Aucun commentaire:

Enregistrer un commentaire