Skip to content

Commit

Permalink
chore: add testing fakes for Distance Matrix
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 577270364
  • Loading branch information
awmack authored and copybara-github committed Oct 28, 2023
1 parent b69d8bd commit 40efe98
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/testing/fake_distance_matrix.ts
Original file line number Diff line number Diff line change
@@ -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<unknown, number>()): 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};
}
30 changes: 29 additions & 1 deletion src/testing/fake_google_maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/googlemaps_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 40efe98

Please sign in to comment.