diff --git a/src/index.ts b/src/index.ts index b683531a0..8e4c0fff2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { LatLng, LatLngBounds } from "./maps/coordinates/latlng"; +import { DirectionsService } from "./routes/directions-service/directions-service"; import { Geocoder } from "./places/geocoder/geocoder"; import { Autocomplete } from "./places/autocomplete"; import { DistanceMatrixService } from "./routes/distance-matrix-service/distance-matrix-service"; @@ -90,6 +91,7 @@ const initialize = function (): void { MapPanes: MapPanes, VisibleRegion: VisibleRegion, InfoWindow: InfoWindow_, + DirectionsService, DistanceMatrixService, }, }; @@ -123,6 +125,7 @@ export { StreetViewService, VisibleRegion, InfoWindow_ as InfoWindow, + DirectionsService, DistanceMatrixService, mockInstances, initialize, diff --git a/src/routes/directions-service/directions-service.test.ts b/src/routes/directions-service/directions-service.test.ts new file mode 100644 index 000000000..18bcd4fba --- /dev/null +++ b/src/routes/directions-service/directions-service.test.ts @@ -0,0 +1,42 @@ +/** + * 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 { initialize, mockInstances } from "../../index"; +import { DirectionsService } from "../../index"; + +beforeEach(() => { + initialize(); +}); + +test("directions service is mocked", async () => { + const directionsService = new google.maps.DirectionsService(); + + expect(directionsService).toBeTruthy(); +}); + +test("registers mocks", () => { + const directionsService = new google.maps.DirectionsService(); + + directionsService.route(null); + + const mocks = mockInstances.get(DirectionsService); + + expect(mocks).toHaveLength(1); + expect(directionsService).toBeInstanceOf(DirectionsService); + expect(mockInstances.get(DirectionsService)[0].route).toHaveBeenCalledWith( + null + ); +}); diff --git a/src/routes/directions-service/directions-service.ts b/src/routes/directions-service/directions-service.ts new file mode 100644 index 000000000..9b7d33913 --- /dev/null +++ b/src/routes/directions-service/directions-service.ts @@ -0,0 +1,41 @@ +/** + * 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 DirectionsService implements google.maps.DirectionsService { + public route = jest.fn().mockImplementation( + ( + request: google.maps.DirectionsRequest, + callback?: ( + directionsResult?: Array, + // @ts-expect-error + directionsStatus: google.maps.DirectionsStatus + ) => void + ): Promise => + Promise.resolve({ + routes: [] as Array, + available_travel_modes: [] as Array, + geocoded_waypoints: [] as Array, + }) + ); + + constructor() { + __registerMockInstance(this.constructor, this); + } +}