Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved map unit test coverage #33

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions test/maps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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, {
cgalvan marked this conversation as resolved.
Show resolved Hide resolved
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, {});

Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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, {});

Expand Down Expand Up @@ -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);
});
Loading