Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
feat(autocomplete): add locate event when pin is clicked + tests (#1063)
Browse files Browse the repository at this point in the history
docs(documentation): add locate event for pin
  • Loading branch information
MarcL authored May 26, 2020
1 parent cab29b8 commit 9b9efdc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/source/documentation.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,19 @@ live on the [map example](examples.html#link-to-a-map).
</tr>
<tr>
<td markdown="1">
<div class="api-entry" id="api-events-locate"><code>locate</code></div>

Event data: `undefined`

</td>
<td markdown="1">
Fired when the location pin is clicked. This event doesn't return anything.

You can use this event when the user has chosen to enable geolcation in the browser by clicking on the pin icon.
</td>
</tr>
<tr>
<td markdown="1">
<div class="api-entry" id="api-events-limit"><code>limit</code></div>

Event data:
Expand Down
1 change: 1 addition & 0 deletions src/places.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export default function places(options) {
pin.addEventListener('click', () => {
autocompleteDataset.source.configure({ useDeviceLocation: true });
autocompleteInstance.focus();
placesInstance.emit('locate');
});

clear.addEventListener('click', () => {
Expand Down
28 changes: 27 additions & 1 deletion src/places.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,19 @@ describe('places', () => {

describe('places autocomplete', () => {
let placesInstance;
let configureMock;

beforeEach(() => {
autocomplete.mockClear();
createAutocompleteDataset.mockClear();
configureMock = jest.fn();
createAutocompleteDataset.mockImplementation(() => ({
source: {
configure: configureMock,
},
other: 'autocompleteDataset',
}));

const container = document
.querySelector('body')
.appendChild(document.createElement('input'));
Expand All @@ -184,7 +194,7 @@ describe('places', () => {
},
{
source: {
configure: 'configure',
configure: configureMock,
},
other: 'autocompleteDataset',
}
Expand Down Expand Up @@ -309,6 +319,22 @@ describe('places', () => {
expect(pinButton.getAttribute('aria-label')).toEqual('focus');
});

it('triggers a locate event on pin click', () => {
return new Promise((done) => {
const pinButton = document.querySelector('.ap-icon-pin');
placesInstance.once('locate', () => {
expect(configureMock).toHaveBeenCalledWith({
useDeviceLocation: true,
});

expect(autocomplete.__instance.focus).toHaveBeenCalled();
done();
});

pinButton.dispatchEvent(new Event('click'));
});
});

describe('input listener', () => {
let input;
let pinButton;
Expand Down

0 comments on commit 9b9efdc

Please sign in to comment.