diff --git a/src/index.ts b/src/index.ts index 98d3b74d0..12f64f6d6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,7 @@ import { LatLng, LatLngBounds } from "./maps/coordinates/latlng"; import { Autocomplete } from "./places/autocomplete"; +import { DistanceMatrixService } from "./routes/distance-matrix-service/distance-matrix-service"; import { Circle } from "./drawing/polygons/circle"; import { Data } from "./drawing/data/data"; import { Feature } from "./drawing/data/data.feature"; @@ -88,6 +89,7 @@ const initialize = function (): void { MapPanes: MapPanes, VisibleRegion: VisibleRegion, InfoWindow: InfoWindow_, + DistanceMatrixService, }, }; }; @@ -119,6 +121,7 @@ export { StreetViewService, VisibleRegion, InfoWindow_ as InfoWindow, + DistanceMatrixService, mockInstances, initialize, }; diff --git a/src/routes/distance-matrix-service/distance-matrix-service.test.ts b/src/routes/distance-matrix-service/distance-matrix-service.test.ts new file mode 100644 index 000000000..9793ff9a5 --- /dev/null +++ b/src/routes/distance-matrix-service/distance-matrix-service.test.ts @@ -0,0 +1,39 @@ +/** + * 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 { DistanceMatrixService, initialize, mockInstances } from "../../index"; + +beforeEach(() => { + initialize(); +}); + +test("distance matrix service is mocked", async () => { + const service = new google.maps.DistanceMatrixService(); + + expect(await service.getDistanceMatrix(null)).toBeTruthy(); +}); + +test("registers mocks", () => { + const service = new google.maps.DistanceMatrixService(); + service.getDistanceMatrix(null); + + const mocks = mockInstances.get(DistanceMatrixService); + + expect(mocks).toHaveLength(1); + expect( + mockInstances.get(DistanceMatrixService)[0].getDistanceMatrix + ).toHaveBeenCalledWith(null); +}); diff --git a/src/routes/distance-matrix-service/distance-matrix-service.ts b/src/routes/distance-matrix-service/distance-matrix-service.ts new file mode 100644 index 000000000..84cb079ec --- /dev/null +++ b/src/routes/distance-matrix-service/distance-matrix-service.ts @@ -0,0 +1,43 @@ +/** + * 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 DistanceMatrixService + implements google.maps.DistanceMatrixService +{ + public getDistanceMatrix = jest.fn().mockImplementation( + ( + request: google.maps.DistanceMatrixRequest, + callback?: ( + distanceMatrixResponse?: Array, + // @ts-expect-error + distanceMatrixStatus: google.maps.DistanceMatrixStatus + ) => void + ): Promise => + Promise.resolve({ + destinationAddresses: [] as Array, + originAddresses: [] as Array, + rows: [] as Array, + }) + ); + + constructor() { + __registerMockInstance(this.constructor, this); + } +}