From 6232fed5c0077f3de4321b3cb7b3f3501d1de20c Mon Sep 17 00:00:00 2001 From: Sabrina Jodexnis Date: Sat, 11 Mar 2023 19:02:47 +0100 Subject: [PATCH 1/2] feat: add mock elevation service --- src/index.ts | 3 ++ .../elevation-service.test.ts | 44 +++++++++++++++ .../elevation-service/elevation-service.ts | 53 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 src/routes/elevation-service/elevation-service.test.ts create mode 100644 src/routes/elevation-service/elevation-service.ts diff --git a/src/index.ts b/src/index.ts index e9823cb57..509eed9be 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 { ElevationsService } 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"; @@ -102,6 +103,7 @@ const initialize = function (): void { MaxZoomService, DirectionsService, DistanceMatrixService, + ElevationsService, }, }; }; @@ -141,6 +143,7 @@ export { MaxZoomService, DirectionsService, DistanceMatrixService, + ElevationsService, mockInstances, initialize, }; 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..58db1016a --- /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 { ElevationsService, 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(ElevationsService); + + expect(mocks).toHaveLength(1); + expect( + mockInstances.get(ElevationsService)[0].getElevationAlongPath + ).toHaveBeenCalledWith(null); + expect( + mockInstances.get(ElevationsService)[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..a4ca7314d --- /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 ElevationsService 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); + } +} From b1b07b49701f736b5b84aa275ff83f3449c69217 Mon Sep 17 00:00:00 2001 From: Sabrina Jodexnis Date: Thu, 23 Mar 2023 18:32:53 +0100 Subject: [PATCH 2/2] fix(elevation-service): fix typo --- src/index.ts | 6 +++--- src/routes/elevation-service/elevation-service.test.ts | 8 ++++---- src/routes/elevation-service/elevation-service.ts | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index c0b814bab..b692ce9fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,7 +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 { ElevationsService } from "./routes/elevation-service/elevation-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"; @@ -106,7 +106,7 @@ const initialize = function (): void { MaxZoomService, DirectionsService, DistanceMatrixService, - ElevationsService, + ElevationService, marker: { PinView, AdvancedMarkerView, @@ -151,7 +151,7 @@ export { MaxZoomService, DirectionsService, DistanceMatrixService, - ElevationsService, + ElevationService, AdvancedMarkerView, PinView, FeatureLayer, diff --git a/src/routes/elevation-service/elevation-service.test.ts b/src/routes/elevation-service/elevation-service.test.ts index 58db1016a..358939b03 100644 --- a/src/routes/elevation-service/elevation-service.test.ts +++ b/src/routes/elevation-service/elevation-service.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { ElevationsService, initialize, mockInstances } from "../../index"; +import { ElevationService, initialize, mockInstances } from "../../index"; beforeEach(() => { initialize(); @@ -32,13 +32,13 @@ test("registers mocks", () => { service.getElevationAlongPath(null); service.getElevationForLocations(null); - const mocks = mockInstances.get(ElevationsService); + const mocks = mockInstances.get(ElevationService); expect(mocks).toHaveLength(1); expect( - mockInstances.get(ElevationsService)[0].getElevationAlongPath + mockInstances.get(ElevationService)[0].getElevationAlongPath ).toHaveBeenCalledWith(null); expect( - mockInstances.get(ElevationsService)[0].getElevationForLocations + 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 index a4ca7314d..6aa675ecb 100644 --- a/src/routes/elevation-service/elevation-service.ts +++ b/src/routes/elevation-service/elevation-service.ts @@ -18,7 +18,7 @@ import { __registerMockInstance } from "../../registry"; -export class ElevationsService implements google.maps.ElevationService { +export class ElevationService implements google.maps.ElevationService { public getElevationAlongPath = jest.fn().mockImplementation( ( request: google.maps.PathElevationRequest,