diff --git a/src/index.ts b/src/index.ts index d804d6cbd..b692ce9fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,6 +25,7 @@ import { Autocomplete } from "./places/autocomplete"; import { MaxZoomService } from "./drawing/max-zoom/max-zoom"; import { AutocompleteService } from "./places/autocomplete-service/autocomplete-service"; import { DistanceMatrixService } from "./routes/distance-matrix-service/distance-matrix-service"; +import { ElevationService } from "./routes/elevation-service/elevation-service"; import { Circle } from "./drawing/polygons/circle"; import { Data } from "./drawing/data/data"; import { DataPolygon } from "./drawing/data/data.polygon"; @@ -105,6 +106,7 @@ const initialize = function (): void { MaxZoomService, DirectionsService, DistanceMatrixService, + ElevationService, marker: { PinView, AdvancedMarkerView, @@ -149,6 +151,7 @@ export { MaxZoomService, DirectionsService, DistanceMatrixService, + ElevationService, AdvancedMarkerView, PinView, FeatureLayer, diff --git a/src/routes/elevation-service/elevation-service.test.ts b/src/routes/elevation-service/elevation-service.test.ts new file mode 100644 index 000000000..358939b03 --- /dev/null +++ b/src/routes/elevation-service/elevation-service.test.ts @@ -0,0 +1,44 @@ +/** + * Copyright 2022 Google LLC. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ElevationService, initialize, mockInstances } from "../../index"; + +beforeEach(() => { + initialize(); +}); + +test("elevation service is mocked", async () => { + const service = new google.maps.ElevationService(); + + expect(await service.getElevationForLocations(null)).toBeTruthy(); + expect(await service.getElevationAlongPath(null)).toBeTruthy(); +}); + +test("registers mocks", () => { + const service = new google.maps.ElevationService(); + service.getElevationAlongPath(null); + service.getElevationForLocations(null); + + const mocks = mockInstances.get(ElevationService); + + expect(mocks).toHaveLength(1); + expect( + mockInstances.get(ElevationService)[0].getElevationAlongPath + ).toHaveBeenCalledWith(null); + expect( + mockInstances.get(ElevationService)[0].getElevationForLocations + ).toHaveBeenCalledWith(null); +}); diff --git a/src/routes/elevation-service/elevation-service.ts b/src/routes/elevation-service/elevation-service.ts new file mode 100644 index 000000000..6aa675ecb --- /dev/null +++ b/src/routes/elevation-service/elevation-service.ts @@ -0,0 +1,53 @@ +/** + * Copyright 2022 Google LLC. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* eslint-disable @typescript-eslint/no-unused-vars */ + +import { __registerMockInstance } from "../../registry"; + +export class ElevationService implements google.maps.ElevationService { + public getElevationAlongPath = jest.fn().mockImplementation( + ( + request: google.maps.PathElevationRequest, + callback?: ( + elevationResults?: Array, + // @ts-expect-error + elevationStatus: google.maps.ElevationStatus + ) => void + ): Promise => + Promise.resolve({ + results: [] satisfies Array, + }) + ); + + public getElevationForLocations = jest.fn().mockImplementation( + ( + request: google.maps.LocationElevationRequest, + callback?: ( + elevationResults?: Array, + // @ts-expect-error + elevationStatus: google.maps.ElevationStatus + ) => void + ): Promise => + Promise.resolve({ + results: [] satisfies Array, + }) + ); + + constructor() { + __registerMockInstance(this.constructor, this); + } +}