Browse Source

fix: bail if no map when adding listeners (#29)

main
Justin Poehnelt 4 years ago committed by GitHub
parent
commit
574240945a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      src/marker.test.ts
  2. 5
      src/marker.ts

20
src/marker.test.ts

@ -46,7 +46,10 @@ beforeEach(() => {
}); });
test("should have listeners after multiple calls to setMap", () => { test("should have listeners after multiple calls to setMap", () => {
const map = jest.fn() as any; const map = (jest.fn() as any) as google.maps.Map;
(google.maps.Marker.prototype.getMap as jest.Mock).mockImplementation(() => {
return map;
});
const marker = new MarkerWithLabel({ labelContent: "foo" }); const marker = new MarkerWithLabel({ labelContent: "foo" });
marker.setMap(map); marker.setMap(map);
@ -64,6 +67,9 @@ test("should have listeners after multiple calls to setMap", () => {
test("should have interactive listeners", () => { test("should have interactive listeners", () => {
const marker = new MarkerWithLabel({ labelContent: "foo" }); const marker = new MarkerWithLabel({ labelContent: "foo" });
(google.maps.Marker.prototype.getMap as jest.Mock).mockImplementation(() => {
return {} as google.maps.Map;
});
marker["addInteractiveListeners"](); marker["addInteractiveListeners"]();
expect( expect(
@ -82,3 +88,15 @@ test("should have interactive listeners", () => {
] ]
`); `);
}); });
test("should not have interactive listeners if no map", () => {
const marker = new MarkerWithLabel({ labelContent: "foo" });
(google.maps.Marker.prototype.getMap as jest.Mock).mockImplementation(() => {
return;
});
marker["addInteractiveListeners"]();
expect(google.maps.event.addDomListener as jest.Mock).toHaveBeenCalledTimes(
0
);
});

5
src/marker.ts

@ -122,6 +122,11 @@ export class MarkerWithLabel extends MarkerSafe {
private addInteractiveListeners() { private addInteractiveListeners() {
if (!this.interactiveListeners) { if (!this.interactiveListeners) {
// If the map is not set, do not set listeners
if (!this.getMap()) {
return;
}
this.interactiveListeners = [ this.interactiveListeners = [
this.label.addDomListener(MOUSEOVER, (e) => { this.label.addDomListener(MOUSEOVER, (e) => {
if (!this.isTouchScreen) { if (!this.isTouchScreen) {

Loading…
Cancel
Save