Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { LatLng, LatLngBounds } from "./maps/coordinates/latlng";
import { Geocoder } from "./places/geocoder/geocoder";
import { Autocomplete } from "./places/autocomplete";
import { AutocompleteService } from "./places/autocomplete-service/autocomplete-service";
import { DistanceMatrixService } from "./routes/distance-matrix-service/distance-matrix-service";
import { Circle } from "./drawing/polygons/circle";
import { Data } from "./drawing/data/data";
Expand Down Expand Up @@ -78,6 +79,7 @@ const initialize = function (): void {
places: {
Autocomplete: Autocomplete,
SearchBox: SearchBox,
AutocompleteService,
},
Geocoder,
Polygon: Polygon,
Expand All @@ -97,6 +99,7 @@ const initialize = function (): void {

export {
Autocomplete,
AutocompleteService,
Geocoder,
Circle,
Data,
Expand Down
45 changes: 45 additions & 0 deletions src/places/autocomplete-service/autocomplete-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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 { AutocompleteService, initialize, mockInstances } from "../../index";

beforeEach(() => {
initialize();
});

test("autocomplete service is mocked", async () => {
const autocompleteService = new google.maps.places.AutocompleteService();
expect(autocompleteService).toBeTruthy();
});

test("register mocks", () => {
const autocompleteService = new google.maps.places.AutocompleteService();

const callback = jest.fn();

autocompleteService.getPlacePredictions(null);
autocompleteService.getQueryPredictions(null, callback);

const mocks = mockInstances.get(AutocompleteService);

expect(mocks).toHaveLength(1);
expect(
mockInstances.get(AutocompleteService)[0].getPlacePredictions
).toHaveBeenCalledWith(null);
expect(
mockInstances.get(AutocompleteService)[0].getQueryPredictions
).toHaveBeenCalledWith(null, callback);
});
54 changes: 54 additions & 0 deletions src/places/autocomplete-service/autocomplete-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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-empty-function */
/* eslint-disable @typescript-eslint/no-unused-vars */

import { __registerMockInstance } from "../../registry";

export class AutocompleteService
implements google.maps.places.AutocompleteService
{
public getPlacePredictions = jest.fn().mockImplementation(
(
request: google.maps.places.AutocompletionRequest,
callback?: (
autocompletePrediction?: Array<google.maps.places.AutocompletePrediction>,
// @ts-expect-error
placesServiceStatus: google.maps.places.PlacesServiceStatus
) => void
): Promise<google.maps.places.AutocompleteResponse> =>
Promise.resolve({
predictions: [] as Array<google.maps.places.AutocompletePrediction>,
})
);

public getQueryPredictions = jest
.fn()
.mockImplementation(
(
request: google.maps.places.QueryAutocompletionRequest,
callback: (
placesServiceStatus: google.maps.places.PlacesServiceStatus,
queryAutocompletePrediction?: Array<google.maps.places.QueryAutocompletePrediction>
) => void
): void => {}
);

constructor() {
__registerMockInstance(this.constructor, this);
}
}