Skip to content

Commit

Permalink
internal
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 549768635
  • Loading branch information
mcreinhard authored and copybara-github committed Jul 21, 2023
1 parent 3b94419 commit 42d7b25
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/place_picker/place_picker_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

// import 'jasmine'; (google3-only)
import '../testing/fake_gmp_components.js';

import {html, TemplateResult} from 'lit';

Expand Down Expand Up @@ -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`<gmpx-place-picker></gmpx-place-picker>`);
Expand Down
9 changes: 8 additions & 1 deletion src/testing/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/testing/fake_gmp_components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
*/

import {LitElement} from 'lit';
import {customElement} from 'lit/decorators.js';

declare global {
interface HTMLElementTagNameMap {
'gmp-map': FakeMapElement;
}
}

@customElement('gmp-map')
export class FakeMapElement extends LitElement {
readonly innerMap = {} as google.maps.Map;
}
4 changes: 2 additions & 2 deletions src/testing/fake_lat_lng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 26 additions & 3 deletions src/testing/fake_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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.
*
Expand All @@ -28,5 +39,17 @@ const EMPTY_FAKE_ROUTE: DirectionsRoute = {
*/
export function makeFakeRoute(fields: Partial<DirectionsRoute> = {}):
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> = {}):
DirectionsLeg {
return {...EMPTY_FAKE_LEG, ...fields};
}
2 changes: 1 addition & 1 deletion src/utils/place_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down

0 comments on commit 42d7b25

Please sign in to comment.