From 718fba7a1f2900d8e7354eed596ff5cc13cc8179 Mon Sep 17 00:00:00 2001 From: Michael Barry Date: Thu, 11 Jul 2019 09:40:05 -0400 Subject: [PATCH] Add check for startPos to fix #7864 (#8462) * Add check for startPos to fix #7864 * add test --- src/ui/bind_handlers.js | 2 +- test/unit/ui/map_events.test.js | 18 +++++++++++++++++- test/util/simulate_interaction.js | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/ui/bind_handlers.js b/src/ui/bind_handlers.js index 9ed7c877ab4..b66144ffd1f 100644 --- a/src/ui/bind_handlers.js +++ b/src/ui/bind_handlers.js @@ -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)); } } diff --git a/test/unit/ui/map_events.test.js b/test/unit/ui/map_events.test.js index 42d783fc3f8..c836e982277 100644 --- a/test/unit/ui/map_events.test.js +++ b/test/unit/ui/map_events.test.js @@ -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); @@ -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}); + canvas.dispatchEvent(event); + t.ok(click.called); + + map.remove(); + t.end(); +}); diff --git a/test/util/simulate_interaction.js b/test/util/simulate_interaction.js index c800a0be84a..b85479ae7f8 100644 --- a/test/util/simulate_interaction.js +++ b/test/util/simulate_interaction.js @@ -1,5 +1,5 @@ -function window(target) { +export function window(target) { if (target.ownerDocument) { return target.ownerDocument.defaultView; } else if (target.defaultView) {