From 42d7b2517d921ca2edb21d6906cdeb4c912d9d93 Mon Sep 17 00:00:00 2001 From: Michael Reinhard Date: Thu, 20 Jul 2023 16:09:39 -0700 Subject: [PATCH] internal PiperOrigin-RevId: 549768635 --- src/place_picker/place_picker_test.ts | 5 ++++- src/testing/environment.ts | 9 ++++++++- src/testing/fake_gmp_components.ts | 2 -- src/testing/fake_lat_lng.ts | 4 ++-- src/testing/fake_route.ts | 29 ++++++++++++++++++++++++--- src/utils/place_utils.ts | 2 +- 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/place_picker/place_picker_test.ts b/src/place_picker/place_picker_test.ts index 6e11142..de6046e 100644 --- a/src/place_picker/place_picker_test.ts +++ b/src/place_picker/place_picker_test.ts @@ -5,7 +5,6 @@ */ // import 'jasmine'; (google3-only) -import '../testing/fake_gmp_components.js'; import {html, TemplateResult} from 'lit'; @@ -68,6 +67,10 @@ const FAKE_PLACES_LIBRARY = { describe('PlacePicker', () => { const env = new Environment(); + beforeAll(() => { + env.defineFakeMapElement(); + }); + async function prepareState(template?: TemplateResult) { const root = env.render(template ?? html``); diff --git a/src/testing/environment.ts b/src/testing/environment.ts index c7d472a..4cfceb7 100644 --- a/src/testing/environment.ts +++ b/src/testing/environment.ts @@ -9,7 +9,9 @@ import {ReactiveElement, render as litRender, TemplateResult} from 'lit'; import {APILoader} from '../api_loader/api_loader.js'; -import {FAKE_GOOGLE_MAPS} from '../testing/fake_google_maps.js'; + +import {FakeMapElement} from './fake_gmp_components.js'; +import {FAKE_GOOGLE_MAPS} from './fake_google_maps.js'; declare global { module jasmine { @@ -97,6 +99,11 @@ export class Environment { return root; } + defineFakeMapElement() { + if (!customElements.get('gmp-map')) { + customElements.define('gmp-map', FakeMapElement); + } + } /** * Waits for all Lit `ReactiveElement` children of the given parent node to diff --git a/src/testing/fake_gmp_components.ts b/src/testing/fake_gmp_components.ts index 249bbbc..8cb2bc6 100644 --- a/src/testing/fake_gmp_components.ts +++ b/src/testing/fake_gmp_components.ts @@ -19,7 +19,6 @@ */ import {LitElement} from 'lit'; -import {customElement} from 'lit/decorators.js'; declare global { interface HTMLElementTagNameMap { @@ -27,7 +26,6 @@ declare global { } } -@customElement('gmp-map') export class FakeMapElement extends LitElement { readonly innerMap = {} as google.maps.Map; } diff --git a/src/testing/fake_lat_lng.ts b/src/testing/fake_lat_lng.ts index 8b3750a..2097b25 100644 --- a/src/testing/fake_lat_lng.ts +++ b/src/testing/fake_lat_lng.ts @@ -15,8 +15,8 @@ type LatLngBoundsLiteral = google.maps.LatLngBoundsLiteral; */ export class FakeLatLng implements LatLng { constructor( - private readonly latitude: number, - private readonly longitude: number, + private readonly latitude = 0, + private readonly longitude = 0, ) {} lat(): number { diff --git a/src/testing/fake_route.ts b/src/testing/fake_route.ts index 1f18206..5bf9d8e 100644 --- a/src/testing/fake_route.ts +++ b/src/testing/fake_route.ts @@ -4,9 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -import {FakeLatLngBounds} from './fake_lat_lng.js'; +import {FakeLatLng, FakeLatLngBounds} from './fake_lat_lng.js'; type DirectionsRoute = google.maps.DirectionsRoute; +type DirectionsLeg = google.maps.DirectionsLeg; const EMPTY_FAKE_ROUTE: DirectionsRoute = { bounds: new FakeLatLngBounds(), @@ -19,6 +20,16 @@ const EMPTY_FAKE_ROUTE: DirectionsRoute = { waypoint_order: [], }; +const EMPTY_FAKE_LEG: DirectionsLeg = { + end_address: '', + end_location: new FakeLatLng(), + start_address: '', + start_location: new FakeLatLng(), + steps: [], + traffic_speed_entry: [], + via_waypoints: [], +}; + /** * Makes a fake `google.maps.DirectionsRoute` object for testing purposes. * @@ -28,5 +39,17 @@ const EMPTY_FAKE_ROUTE: DirectionsRoute = { */ export function makeFakeRoute(fields: Partial = {}): DirectionsRoute { - return {...EMPTY_FAKE_ROUTE, ...fields}; -} + return {...EMPTY_FAKE_ROUTE, ...fields}; + } + +/** + * Makes a fake `google.maps.DirectionsLeg` object for testing purposes. + * + * @param fields - An object of fields of the `DirectionsLeg`. Any fields not + * provided will default to empty strings, empty arrays, or the LatLng + * (0, 0). + */ +export function makeFakeLeg(fields: Partial = {}): + DirectionsLeg { + return {...EMPTY_FAKE_LEG, ...fields}; + } diff --git a/src/utils/place_utils.ts b/src/utils/place_utils.ts index 737e46a..ee8cd65 100644 --- a/src/utils/place_utils.ts +++ b/src/utils/place_utils.ts @@ -59,7 +59,7 @@ function isPlace(data: LatLng|LatLngLiteral|Place): data is Place { return data.hasOwnProperty('id'); } -function isLatLng(data: LatLng|LatLngLiteral): data is LatLng { +export function isLatLng(data: LatLng|LatLngLiteral): data is LatLng { return typeof data.lat === 'function'; }