Skip to content

Commit

Permalink
Fix marker popup console error (#213)
Browse files Browse the repository at this point in the history
* Add a failing test

* Fix failing test and fix the bug

* Update popup.component.spec.ts
  • Loading branch information
HarelM authored Nov 7, 2024
1 parent 1b15e11 commit b48fa56
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
73 changes: 73 additions & 0 deletions projects/ngx-maplibre-gl/src/lib/popup/popup.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Component } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { of } from 'rxjs';
import { MapService } from '../map/map.service';
import { MarkerComponent } from '../marker/marker.component';
import { PopupComponent } from './popup.component';

const getMapServiceStub = () =>
jasmine.createSpyObj(
[
'addMarker',
'removeMarker',
'removePopupFromMap',
'createPopup',
'addPopupToMarker'
],
{
mapCreated$: of(true),
}
);

@Component({
template: `
@if (show) {
<mgl-marker [lngLat]="[0,0]" #myMarker>
...
</mgl-marker>
<mgl-popup [marker]="myMarker">
Hello from marker!
</mgl-popup>
}
`,
standalone: true,
imports: [MarkerComponent, PopupComponent]
})
class MarkerPopupTestComponent {
show = true;
}

describe('PopupComponent', () => {
let mapServiceStub: jasmine.SpyObj<MapService>;
let component: MarkerPopupTestComponent;
let fixture: ComponentFixture<MarkerPopupTestComponent>;

beforeEach(waitForAsync(() => {
mapServiceStub = getMapServiceStub();
mapServiceStub.createPopup.and.returnValue({ } as any);
mapServiceStub.addMarker.and.returnValue({ } as any);
TestBed.configureTestingModule({
imports: [MarkerPopupTestComponent],
})
.overrideComponent(MarkerPopupTestComponent, {
set: {
providers: [{ provide: MapService, useValue: mapServiceStub }],
},
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(MarkerPopupTestComponent);
component = fixture.componentInstance;
});

describe('Init/Destroy tests', () => {
it('should remove the popup when marker is removed', () => {
fixture.detectChanges();
component.show = false;
fixture.detectChanges();
expect(mapServiceStub.removePopupFromMap).toHaveBeenCalled();
});
});
});
2 changes: 2 additions & 0 deletions projects/ngx-maplibre-gl/src/lib/popup/popup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ export class PopupComponent implements OnChanges, OnInit, OnDestroy {
this.mapService.removePopupFromMap(this.popupInstance);
} else if (markerInstance) {
this.mapService.removePopupFromMarker(markerInstance);
} else {
this.mapService.removePopupFromMap(this.popupInstance);
}
}
this.popupInstance = null;
Expand Down

0 comments on commit b48fa56

Please sign in to comment.