Skip to content

Commit 9864a40

Browse files
authored
feat: add mock for places service (#390)
1 parent a570523 commit 9864a40

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
/* eslint-disable @typescript-eslint/no-explicit-any */
1818

1919
import { LatLng, LatLngBounds } from "./maps/coordinates/latlng";
20+
import { PlacesService } from "./places/places-service/places-service";
2021
import { DirectionsService } from "./routes/directions-service/directions-service";
2122
import { Geocoder } from "./places/geocoder/geocoder";
2223
import { Autocomplete } from "./places/autocomplete";
@@ -80,6 +81,7 @@ const initialize = function (): void {
8081
places: {
8182
Autocomplete: Autocomplete,
8283
SearchBox: SearchBox,
84+
PlacesService,
8385
AutocompleteService,
8486
},
8587
Geocoder,
@@ -101,6 +103,7 @@ const initialize = function (): void {
101103

102104
export {
103105
Autocomplete,
106+
PlacesService,
104107
AutocompleteService,
105108
Geocoder,
106109
Circle,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2022 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { PlacesService, initialize, mockInstances } from "../../index";
18+
19+
beforeEach(() => {
20+
initialize();
21+
});
22+
23+
test("register mocks", () => {
24+
const placesService = new google.maps.places.PlacesService(null);
25+
26+
expect(placesService).toBeTruthy();
27+
});
28+
29+
test("places service is mocked", () => {
30+
const placesService = new google.maps.places.PlacesService(null);
31+
32+
const callback = jest.fn();
33+
34+
placesService.findPlaceFromPhoneNumber(null, callback);
35+
placesService.findPlaceFromQuery(null, callback);
36+
placesService.getDetails(null, callback);
37+
placesService.nearbySearch(null, callback);
38+
placesService.textSearch(null, callback);
39+
40+
const mocks = mockInstances.get(PlacesService);
41+
42+
expect(mocks).toHaveLength(1);
43+
44+
expect(
45+
mockInstances.get(PlacesService)[0].findPlaceFromPhoneNumber
46+
).toHaveBeenCalledWith(null, callback);
47+
48+
expect(
49+
mockInstances.get(PlacesService)[0].findPlaceFromQuery
50+
).toHaveBeenCalledWith(null, callback);
51+
52+
expect(mockInstances.get(PlacesService)[0].getDetails).toHaveBeenCalledWith(
53+
null,
54+
callback
55+
);
56+
57+
expect(mockInstances.get(PlacesService)[0].nearbySearch).toHaveBeenCalledWith(
58+
null,
59+
callback
60+
);
61+
62+
expect(mockInstances.get(PlacesService)[0].textSearch).toHaveBeenCalledWith(
63+
null,
64+
callback
65+
);
66+
});
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2022 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/* eslint-disable @typescript-eslint/no-empty-function */
18+
/* eslint-disable @typescript-eslint/no-unused-vars */
19+
20+
import { __registerMockInstance } from "../../registry";
21+
22+
export class PlacesService implements google.maps.places.PlacesService {
23+
public findPlaceFromPhoneNumber = jest.fn().mockImplementation(
24+
(
25+
request: google.maps.places.FindPlaceFromPhoneNumberRequest,
26+
callback: (
27+
placesResult?: Array<google.maps.places.PlaceResult>,
28+
// @ts-expect-error
29+
placeServiceStatus: google.maps.places.PlacesServiceStatus
30+
) => void
31+
): void => {}
32+
);
33+
34+
public findPlaceFromQuery = jest.fn().mockImplementation(
35+
(
36+
request: google.maps.places.FindPlaceFromQueryRequest,
37+
callback: (
38+
placeResult?: Array<google.maps.places.PlaceResult>,
39+
// @ts-expect-error
40+
placeServiceStatus: google.maps.places.PlacesServiceStatus
41+
) => void
42+
): void => {}
43+
);
44+
45+
public getDetails = jest.fn().mockImplementation(
46+
(
47+
request: google.maps.places.PlaceDetailsRequest,
48+
callback: (
49+
placeResult?: Array<google.maps.places.PlaceResult>,
50+
// @ts-expect-error
51+
placeServiceStatus: google.maps.places.PlacesServiceStatus
52+
) => void
53+
): void => {}
54+
);
55+
56+
public nearbySearch = jest.fn().mockImplementation(
57+
(
58+
request: google.maps.places.PlaceSearchRequest,
59+
callback: (
60+
placeResult?: Array<google.maps.places.PlaceResult>,
61+
// @ts-expect-error
62+
placesServiceStatus: google.maps.places.PlacesServiceStatus,
63+
pagination?: google.maps.places.PlaceSearchPagination
64+
) => void
65+
): void => {}
66+
);
67+
68+
public textSearch = jest.fn().mockImplementation(
69+
(
70+
request: google.maps.places.TextSearchRequest,
71+
callback: (
72+
placeResult?: Array<google.maps.places.PlaceResult>,
73+
// @ts-expect-error
74+
placesServiceStatus: google.maps.places.PlacesServiceStatus,
75+
pagination?: google.maps.places.PlaceSearchPagination
76+
) => void
77+
): void => {}
78+
);
79+
80+
constructor(attrContainer: HTMLDivElement | null) {
81+
__registerMockInstance(this.constructor, this);
82+
}
83+
}

0 commit comments

Comments
 (0)