diff --git a/src/maps.ts b/src/maps.ts index a3b4578..5ce1904 100644 --- a/src/maps.ts +++ b/src/maps.ts @@ -254,7 +254,7 @@ class MigrationMap { // positions that MapLibre does not offer #addNavigationControl(zoomControlOptions) { // remove old navControl so we don't have multiple - if (this.#navigationControl && this.#map.hasControl(this.#navigationControl)) { + if (this.#navigationControl) { this.#map.removeControl(this.#navigationControl); } // add new navControl diff --git a/test/maps.test.ts b/test/maps.test.ts index ecb0785..d4169b9 100644 --- a/test/maps.test.ts +++ b/test/maps.test.ts @@ -14,6 +14,8 @@ import { Map, MapOptions, NavigationControl } from "maplibre-gl"; const testLat = 30.268193; // Austin, TX :) const testLng = -97.7457518; +jest.spyOn(console, "error").mockImplementation(() => {}); + afterEach(() => { jest.clearAllMocks(); }); @@ -71,6 +73,16 @@ test("should set migration map options with control position not available in Ma expect(Map.prototype.addControl).toHaveBeenCalledWith(expect.any(NavigationControl), "bottom-right"); }); +test("should log error with invalid map option center", () => { + const testMap = new MigrationMap(null, { + center: "THIS_IS_NOT_A_VALID_CENTER", + }); + + expect(testMap).not.toBeNull(); + expect(console.error).toHaveBeenCalledTimes(1); + expect(console.error).toHaveBeenCalledWith("Unrecognized center option", "THIS_IS_NOT_A_VALID_CENTER"); +}); + test("should call setZoom from migration map", () => { const testMap = new MigrationMap(null, {}); @@ -180,6 +192,19 @@ test("should call setOptions from migration map and add new NavigationControl wi expect(Map.prototype.addControl).toHaveBeenCalledTimes(2); expect(Map.prototype.addControl).toHaveBeenCalledWith(expect.any(NavigationControl), "bottom-right"); expect(Map.prototype.addControl).toHaveBeenCalledWith(expect.any(NavigationControl), "top-right"); + expect(Map.prototype.removeControl).toHaveBeenCalledTimes(1); + expect(Map.prototype.removeControl).toHaveBeenCalledWith(expect.any(NavigationControl)); +}); + +test("should log error when setOptions is called with invalid center", () => { + const testMap = new MigrationMap(null, {}); + + testMap.setOptions({ + center: "ANOTHER_INVALID_CENTER", + }); + + expect(console.error).toHaveBeenCalledTimes(1); + expect(console.error).toHaveBeenCalledWith("Unrecognized center option", "ANOTHER_INVALID_CENTER"); }); test("should call setTilt from migration map", () => { @@ -241,6 +266,20 @@ test("should call moveCamera from migration map", () => { }); }); +test("should log error when moveCamera is called with invalid center", () => { + const testMap = new MigrationMap(null, {}); + + testMap.moveCamera({ + center: "NOT_A_REAL_CENTER", + zoom: 16, + heading: 90, + tilt: 45, + }); + + expect(console.error).toHaveBeenCalledTimes(1); + expect(console.error).toHaveBeenCalledWith("Unrecognized center option", "NOT_A_REAL_CENTER"); +}); + test("should call panBy from migration map", () => { const testMap = new MigrationMap(null, {}); @@ -572,3 +611,21 @@ test("should call handler with translated MapMouseEvent after mouseover", () => expect(handlerSpy).toHaveBeenCalledTimes(1); expect(handlerSpy).toHaveBeenCalledWith(expectedGoogleMapMouseEvent); }); + +test("should call GoogleMapEvent handler after tilesloaded", () => { + // mock map so that we can mock on so that we can mock tilesloaded + const mockMap = { + on: jest.fn(), + }; + const migrationMap = new MigrationMap(null, {}); + migrationMap._setMap(mockMap); + + // add spy as handler + const handlerSpy = jest.fn(); + migrationMap.addListener("tilesloaded", handlerSpy); + + // Simulate mocked tilesloaded call + mockMap.on.mock.calls[0][1](); + + expect(handlerSpy).toHaveBeenCalledTimes(1); +});