Skip to content

Commit

Permalink
feat(autocomplete): Add AutocompleteSessionToken (#500)
Browse files Browse the repository at this point in the history
Previously the `google.maps.places.AutocompleteSessionToken` class
was not mocked by this library.

Developers implementing custom autocomplete operations would likely
encounter the following error message in their tests:

```
TypeError: google.maps.places.AutocompleteSessionToken is not a constructor
```

This commit adds a mock and exports it for the `AutocompleteSessionToken`
class. The constructor accepts no parameters, and the class does not
expose any public functions/methods.

resolves: #495
  • Loading branch information
jessedoyle authored Sep 13, 2023
1 parent fb9a1a8 commit a59d1d8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Geocoder } from "./places/geocoder/geocoder";
import { Autocomplete } from "./places/autocomplete";
import { MaxZoomService } from "./drawing/max-zoom/max-zoom";
import { AutocompleteService } from "./places/autocomplete-service/autocomplete-service";
import { AutocompleteSessionToken } from "./places/autocomplete-session-token/autocomplete-session-token";
import { DistanceMatrixService } from "./routes/distance-matrix-service/distance-matrix-service";
import { ElevationService } from "./routes/elevation-service/elevation-service";
import { Circle } from "./drawing/polygons/circle";
Expand Down Expand Up @@ -91,6 +92,7 @@ const initialize = function (): void {
SearchBox: SearchBox,
PlacesService,
AutocompleteService,
AutocompleteSessionToken,
},
Geocoder,
Polygon: Polygon,
Expand Down Expand Up @@ -120,6 +122,7 @@ export {
Autocomplete,
PlacesService,
AutocompleteService,
AutocompleteSessionToken,
Geocoder,
Circle,
Data,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2023 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 {
AutocompleteSessionToken,
initialize,
mockInstances,
} from "../../index";

beforeEach(initialize);

test("autocomplete session token is mocked", async () => {
const token = new google.maps.places.AutocompleteSessionToken();

expect(token).toBeTruthy();
});

test("register mocks", () => {
new google.maps.places.AutocompleteSessionToken();
const mocks = mockInstances.get(AutocompleteSessionToken);

expect(mocks).toHaveLength(1);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2023 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 { __registerMockInstance } from "../../registry";

export class AutocompleteSessionToken
implements google.maps.places.AutocompleteSessionToken
{
constructor() {
__registerMockInstance(this.constructor, this);
}
}

0 comments on commit a59d1d8

Please sign in to comment.