Skip to content

Commit

Permalink
Add check for startPos to fix #7864 (#8462)
Browse files Browse the repository at this point in the history
* Add check for startPos to fix #7864

* add test
  • Loading branch information
msbarry authored and mourner committed Jul 11, 2019
1 parent 48af940 commit 718fba7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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});
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

0 comments on commit 718fba7

Please sign in to comment.