From 40efe9838f96ba8ed4164a2c12426b31ba29c78b Mon Sep 17 00:00:00 2001 From: awmack Date: Fri, 27 Oct 2023 12:12:35 -0700 Subject: [PATCH] chore: add testing fakes for Distance Matrix PiperOrigin-RevId: 577270364 --- src/testing/fake_distance_matrix.ts | 32 +++++++++++++++++++++++++++++ src/testing/fake_google_maps.ts | 30 ++++++++++++++++++++++++++- src/utils/googlemaps_types.ts | 2 +- 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/testing/fake_distance_matrix.ts diff --git a/src/testing/fake_distance_matrix.ts b/src/testing/fake_distance_matrix.ts new file mode 100644 index 0000000..f3c3857 --- /dev/null +++ b/src/testing/fake_distance_matrix.ts @@ -0,0 +1,32 @@ +/** + * @license + * Copyright 2023 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +export const FAKE_DM_VALUE = 101; + +/** + * Creates a fake Distance Matrix response, based on a map `fakeDistances` that + * specifies the resulting distance for a given _destination_ point. If a + * destination isn't found in the map, 101 is used as the distance. + */ +export function makeFakeDistanceMatrixResponse( + request: google.maps.DistanceMatrixRequest, + fakeDistances = + new Map()): google.maps.DistanceMatrixResponse { + const rows: google.maps.DistanceMatrixResponseRow[] = []; + for (const _ of request.origins) { + const row = []; + for (const destination of request.destinations) { + const fakeValue = fakeDistances.get(destination) ?? FAKE_DM_VALUE; + const result = { + status: 'OK' as google.maps.DistanceMatrixElementStatus, + distance: {value: fakeValue, text: `${fakeValue} ${request.unitSystem}`} + } as google.maps.DistanceMatrixResponseElement; + row.push(result); + } + rows.push({elements: row}); + } + return {originAddresses: [], destinationAddresses: [], rows}; +} \ No newline at end of file diff --git a/src/testing/fake_google_maps.ts b/src/testing/fake_google_maps.ts index 936753a..74be718 100644 --- a/src/testing/fake_google_maps.ts +++ b/src/testing/fake_google_maps.ts @@ -4,8 +4,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -import {Place, PlaceResult} from '../utils/googlemaps_types.js'; +import {LatLng, LatLngLiteral, Place, PlaceResult} from '../utils/googlemaps_types.js'; +import {makeFakeDistanceMatrixResponse} from './fake_distance_matrix.js'; import {makeFakePlace} from './fake_place.js'; import {makeFakeRoute} from './fake_route.js'; @@ -38,6 +39,13 @@ export class FakeGoogleMapsHarness { routes: [makeFakeRoute()], } as google.maps.DirectionsResult); + /** + * Override this function to control the response of a + * `google.maps.DistanceMatrixService.getDistanceMatrix()` request. + */ + distanceMatrixHandler = (request: google.maps.DistanceMatrixRequest) => + makeFakeDistanceMatrixResponse(request); + /** * Override this function to control the response of a * `google.maps.places.PlacesService.findPlaceFromQuery()` request. @@ -138,7 +146,27 @@ export class FakeGoogleMapsHarness { return harness.routeHandler(request); } }, + + DistanceMatrixService: class { + getDistanceMatrix(request: google.maps.DistanceMatrixRequest) { + return Promise.resolve(harness.distanceMatrixHandler(request)); + } + } }, + 'geometry': { + spherical: { + /** + * Fake spherical geometry calculation returns the difference in + * `lat` values. + */ + computeDistanceBetween( + from: LatLng|LatLngLiteral, to: LatLng|LatLngLiteral): number { + const getLat = (x: LatLng|LatLngLiteral) => + typeof x.lat === 'function' ? x.lat() : x.lat; + return Math.abs(getLat(from) - getLat(to)); + } + } + } }; this.sdk = { diff --git a/src/utils/googlemaps_types.ts b/src/utils/googlemaps_types.ts index 81c6e70..dd7fa6f 100644 --- a/src/utils/googlemaps_types.ts +++ b/src/utils/googlemaps_types.ts @@ -62,5 +62,5 @@ export type PriceLevel = google.maps.places.PriceLevel; /** HTML tag names for Maps JS web components. */ export interface HTMLElementTagNameMap { - 'gmp-map': MapElement, + 'gmp-map': MapElement; }