Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for startPos to fix #7864 #8462

Merged
merged 2 commits into from
Jul 11, 2019
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
2 changes: 1 addition & 1 deletion src/ui/bind_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default function bindHandlers(map: Map, options: {interactive: boolean, c

function onClick(e: MouseEvent) {
const pos = DOM.mousePos(el, e);
if (pos.equals(startPos) || pos.dist(startPos) < options.clickTolerance) {
if (!startPos || pos.equals(startPos) || pos.dist(startPos) < options.clickTolerance) {
map.fire(new MapMouseEvent('click', map, e));
}
}
Expand Down
18 changes: 17 additions & 1 deletion test/unit/ui/map_events.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from '../../util/test';
import { createMap } from '../../util';
import simulate from '../../util/simulate_interaction';
import simulate, { window } from '../../util/simulate_interaction';

test('Map#on adds a non-delegated event listener', (t) => {
const map = createMap(t);
Expand Down Expand Up @@ -585,3 +585,19 @@ test(`Map#on mousedown does not fire subsequent click event if mouse position ch
map.remove();
t.end();
});

test(`Map#on click fires subsequent click event if there is no corresponding mousedown/mouseup event`, (t) => {
const map = createMap(t, { clickTolerance: 4 });

const click = t.spy();
map.on('click', click);
const canvas = map.getCanvas();

const MouseEvent = window(canvas).MouseEvent;
const event = new MouseEvent('click', {bubbles: true, clientX: 100, clientY: 100});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we could expose a simulate.fakeClick utility, but I didn't think we'd want to encourage anyone to re-use that since it only applies to a corner case.

canvas.dispatchEvent(event);
t.ok(click.called);

map.remove();
t.end();
});
2 changes: 1 addition & 1 deletion test/util/simulate_interaction.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

function window(target) {
export function window(target) {
if (target.ownerDocument) {
return target.ownerDocument.defaultView;
} else if (target.defaultView) {
Expand Down