Skip to content
Open
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
"docs": "typedoc src/index.ts",
"format": "eslint . --fix",
"lint": "eslint .",
"prepack": "rm -rf dist && rollup -c",
"prepack": "npm run typecheck && rm -rf dist && rollup -c",
"test": "jest src/*",
"typecheck": "tsc --noEmit -p .",
"test:watch": "jest --watch src/*"
},
"devDependencies": {
"@rollup/plugin-typescript": "^12.3.0",
"@types/google.maps": "^3.58.1",
"@types/google.maps": "^3.66.10",
"@types/jest": "^30.0.0",
"@typescript-eslint/eslint-plugin": ">=8.62.1",
"@typescript-eslint/parser": ">=8.62.0",
Expand Down
9 changes: 9 additions & 0 deletions src/drawing/DOM/overlayview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import { Map_ } from "../../maps/maps/map";
import { MVCObject } from "../../maps/event/mvcobject";

export class OverlayView extends MVCObject implements google.maps.OverlayView {
public static preventMapHitsAndGesturesFrom = jest
.fn()
.mockImplementation((element: Element): void => {});
public static preventMapHitsFrom = jest
.fn()
.mockImplementation((element: Element): void => {});

public draw = jest.fn();
public getMap = jest
.fn()
Expand All @@ -44,3 +51,5 @@ export class OverlayView extends MVCObject implements google.maps.OverlayView {
null
);
}

OverlayView satisfies typeof google.maps.OverlayView;
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ export class AdvancedMarkerElement
}

customElements.define("gmp-advanced-marker", AdvancedMarkerElement);

AdvancedMarkerElement satisfies typeof google.maps.marker.AdvancedMarkerElement;
2 changes: 2 additions & 0 deletions src/drawing/advanced-marker-element/pin-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ export class PinElement
}
}
customElements.define("gmp-pin", PinElement);

PinElement satisfies typeof google.maps.marker.PinElement;
2 changes: 2 additions & 0 deletions src/drawing/data/data.feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ export class Feature extends MVCObject implements google.maps.Data.Feature {
return null;
});
}

Feature satisfies typeof google.maps.Data.Feature;
6 changes: 6 additions & 0 deletions src/drawing/data/data.polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { MVCObject } from "../../maps/event/mvcobject";
export class DataPolygon extends MVCObject implements google.maps.Data.Polygon {
constructor(
elements?:
| (
| google.maps.Data.LinearRing
| (google.maps.LatLng | google.maps.LatLngLiteral)[]
)[]
| google.maps.MVCArray<google.maps.MVCArray<google.maps.LatLng>>
| google.maps.MVCArray<google.maps.LatLng>
| google.maps.LatLng[][]
Expand Down Expand Up @@ -47,3 +51,5 @@ export class DataPolygon extends MVCObject implements google.maps.Data.Polygon {
public getLength = jest.fn().mockImplementation((): number => 0);
public getType = jest.fn().mockImplementation((): string => "MultiPolygon");
}

DataPolygon satisfies typeof google.maps.Data.Polygon;
15 changes: 15 additions & 0 deletions src/drawing/data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,18 @@ export class Data extends MVCObject implements google.maps.Data {
return null;
});
}

/**
* `google.maps.Data` geometry classes that have no mock yet. Listed explicitly
* so this check still catches any other static member that goes missing.
*/
type UnmockedDataGeometries =
| "GeometryCollection"
| "LinearRing"
| "LineString"
| "MultiLineString"
| "MultiPoint"
| "MultiPolygon"
| "Point";

Data satisfies Omit<typeof google.maps.Data, UnmockedDataGeometries>;
2 changes: 2 additions & 0 deletions src/drawing/kml/kmllayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ export class KmlLayer extends MVCObject implements google.maps.KmlLayer {
super();
}
}

KmlLayer satisfies typeof google.maps.KmlLayer;
2 changes: 2 additions & 0 deletions src/drawing/marker/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ export class Marker extends MVCObject implements google.maps.Marker {
): google.maps.MapsEventListener => MapsEventListener
);
}

Marker satisfies typeof google.maps.Marker;
2 changes: 2 additions & 0 deletions src/drawing/max-zoom/max-zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ export class MaxZoomService implements google.maps.MaxZoomService {
__registerMockInstance(this.constructor, this);
}
}

MaxZoomService satisfies typeof google.maps.MaxZoomService;
2 changes: 2 additions & 0 deletions src/drawing/polygons/circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ export class Circle extends MVCObject implements google.maps.Circle {
public setRadius = jest.fn().mockImplementation((radius: number) => {});
public setVisible = jest.fn().mockImplementation((visible: boolean) => {});
}

Circle satisfies typeof google.maps.Circle;
2 changes: 2 additions & 0 deletions src/drawing/polygons/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ export class Polygon extends MVCObject implements google.maps.Polygon {
.fn()
.mockImplementation((visible: boolean): void => {});
}

Polygon satisfies typeof google.maps.Polygon;
2 changes: 2 additions & 0 deletions src/drawing/polygons/polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ export class Polyline extends MVCObject implements google.maps.Polyline {
.fn()
.mockImplementation((visible: boolean): void => {});
}

Polyline satisfies typeof google.maps.Polyline;
2 changes: 2 additions & 0 deletions src/drawing/polygons/rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ export class Rectangle extends MVCObject implements google.maps.Rectangle {
);
public setVisible = jest.fn((visible: boolean) => {});
}

Rectangle satisfies typeof google.maps.Rectangle;
5 changes: 5 additions & 0 deletions src/maps/coordinates/latlng.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ test("LatLngBounds registers mocks", () => {
expect(mocks).toHaveLength(2);
expect(mocks[0].equals).toHaveBeenCalledWith(mocks[1]);
});

test("LatLngBounds.MAX_BOUNDS is not a registered instance", () => {
expect(google.maps.LatLngBounds.MAX_BOUNDS).toBeInstanceOf(LatLngBounds);
expect(mockInstances.get(LatLngBounds)).toHaveLength(0);
});
22 changes: 21 additions & 1 deletion src/maps/coordinates/latlng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* limitations under the License.
*/

import { __registerMockInstance } from "../../registry";
import {
__registerMockInstance,
__unregisterMockInstance,
} from "../../registry";

export class LatLng implements google.maps.LatLng {
constructor(
Expand Down Expand Up @@ -111,4 +114,21 @@ export class LatLngBounds implements google.maps.LatLngBounds {
other: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral
): google.maps.LatLngBounds => this
);

/**
* Mirrors `google.maps.LatLngBounds.MAX_BOUNDS`. Created once at module load
* and kept out of `mockInstances`, so it doesn't show up as a test-created
* instance.
*/
public static readonly MAX_BOUNDS: google.maps.LatLngBounds = (() => {
const bounds = new LatLngBounds(
{ lat: -90, lng: -180 },
{ lat: 90, lng: 180 }
);
__unregisterMockInstance(LatLngBounds, bounds);
return bounds;
})();
}

LatLng satisfies typeof google.maps.LatLng;
LatLngBounds satisfies typeof google.maps.LatLngBounds;
2 changes: 1 addition & 1 deletion src/maps/coordinates/latlngaltitude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test("registers mocks", () => {
const latLng = new google.maps.LatLng(0, 0);
const latLngAltitude = new google.maps.LatLngAltitude(latLng);

latLngAltitude.altitude = 100;
(latLngAltitude as unknown as LatLngAltitude).altitude = 100;

const mocks = mockInstances.get(LatLngAltitude);

Expand Down
4 changes: 3 additions & 1 deletion src/maps/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const MapsEventListener: google.maps.MapsEventListener = {
remove: jest.fn(),
};

export class event implements google.maps.event {
export class event {
public static addDomListener: (
instance: object,
eventName: string,
Expand Down Expand Up @@ -65,3 +65,5 @@ export class event implements google.maps.event {
...eventArgs: any[]
) => void = jest.fn(() => null);
}

event satisfies typeof google.maps.event;
9 changes: 9 additions & 0 deletions src/maps/maps/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ test("registers mocks", () => {
expect(mockInstances.get(Map_)[0].fitBounds).toHaveBeenCalledWith(null);
expect(mockInstances.get(Map_)[0].data.get).toHaveBeenCalledWith(null);
});

test("exposes DEMO_MAP_ID", () => {
expect(google.maps.Map.DEMO_MAP_ID).toBe("DEMO_MAP_ID");
});

test("getInternalUsageAttributionIds returns null by default", () => {
const map = new google.maps.Map(document.createElement("div"));
expect(map.getInternalUsageAttributionIds()).toBeNull();
});
8 changes: 8 additions & 0 deletions src/maps/maps/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { FeatureLayer } from "./featurelayer";
import { ControlPosition } from "../controls/controlposition";

export class Map_ extends MVCObject implements google.maps.Map {
public static readonly DEMO_MAP_ID = "DEMO_MAP_ID";

public controls: Array<google.maps.MVCArray<HTMLElement>>;
public data: google.maps.Data;
public mapTypes: google.maps.MapTypeRegistry;
Expand All @@ -35,6 +37,10 @@ export class Map_ extends MVCObject implements google.maps.Map {
(featureType: google.maps.FeatureType) => new FeatureLayer()
);

public getInternalUsageAttributionIds = jest
.fn()
.mockImplementation((): Iterable<string> | null => null);

public getMapCapabilities = jest.fn(() => {
return {
isAdvancedMarkersAvailable: false,
Expand Down Expand Up @@ -174,3 +180,5 @@ export class Map_ extends MVCObject implements google.maps.Map {
this.overlayMapTypes = new google.maps.MVCArray();
}
}

Map_ satisfies typeof google.maps.Map;
12 changes: 12 additions & 0 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@ export function __registerMockInstance(ctr: Function, instance: any): void {
const existing = MOCKS_REGISTRY.get(ctr.name) || [];
MOCKS_REGISTRY.set(ctr.name, [...existing, instance]);
}

/* Internal */
export function __unregisterMockInstance(ctr: Function, instance: any): void {
const existing = MOCKS_REGISTRY.get(ctr.name);
if (!existing) return;
const remaining = existing.filter((item) => item !== instance);
if (remaining.length) {
MOCKS_REGISTRY.set(ctr.name, remaining);
} else {
MOCKS_REGISTRY.delete(ctr.name);
}
}
Loading