Skip to content

Commit 2b9e67d

Browse files
committed
test: fix clickOver test; scaleFactor part didn't work
1 parent 8c50fc7 commit 2b9e67d

File tree

2 files changed

+28
-57
lines changed

2 files changed

+28
-57
lines changed

tests/helpers/click-over.js

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,23 @@ import { getRootElement, triggerEvent } from '@ember/test-helpers';
22
import debug from 'debug';
33
const log = debug('ember-cli-plotly:test');
44

5-
function getScaleFactor(element) {
6-
const { transform } = window.getComputedStyle(element);
7-
let scaleFactor = 1;
8-
if (transform === 'none') {
9-
log(`no transformation applied; keeping default scaleFactor of ${scaleFactor}`);
10-
} else {
11-
const matchResults = transform.match(/matrix\((.*)\)/);
12-
if (matchResults) {
13-
const matrixConstants = matchResults[1].split(',').map(parseFloat);
14-
if (matrixConstants[0] === matrixConstants[3]) {
15-
scaleFactor = matrixConstants[0];
16-
} else {
17-
log('not sure how to determine scale factor from this matrix');
18-
}
19-
} else {
20-
log('unknown transformation');
21-
}
22-
}
23-
return scaleFactor;
24-
}
25-
26-
function getCoordinates(target, container, scaleFactor) {
5+
/// returns coordinates relative to viewport (e.g. `clientX` & `clientY`)
6+
function getCoordinates(target, container) {
277
container = container || target;
28-
scaleFactor = scaleFactor || 1;
298
const containerRect = container.getBoundingClientRect();
309
const c = {
3110
x: containerRect.x, // containerRect.left + containerRect.width/2,
32-
y: containerRect.y //containerRect.top + containerRect.height/2
11+
y: containerRect.y, // containerRect.top + containerRect.height/2
3312
};
34-
3513
const targetRect = target.getBoundingClientRect();
3614
const t = {
3715
x: targetRect.x + targetRect.width/2,
3816
y: targetRect.y + targetRect.height/2
3917
};
4018

4119
const coordinates = {
42-
x: c.x + (t.x - c.x - targetRect.width/2)/scaleFactor,
43-
y: c.y + (t.y - c.y - targetRect.height/2)/scaleFactor,
20+
x: c.x + (t.x - c.x - targetRect.width/2),
21+
y: c.y + (t.y - c.y - targetRect.height/2),
4422
};
4523
log('coordinates', coordinates, containerRect, targetRect);
4624
return coordinates;
@@ -55,28 +33,30 @@ function getCoordinates(target, container, scaleFactor) {
5533
will not result in the `plotly_click` event that would come from
5634
a real pointer click at that same screen location.
5735
See https://community.plot.ly/t/how-to-simulate-mouse-events-e-g-click/8828
58-
- It seems to be incompatible with `transform: scale(0.5)`, which
59-
is applied by default to `#ember-testing`
36+
6037
6138
@public
6239
@param {Element} target the element or selector to click "over"
6340
@return {Promise<void>} resolves after settling
6441
*/
65-
export default function clickOver(target) {
66-
return async function() {
67-
const root = getRootElement();
68-
const scaleFactor = getScaleFactor(root);
69-
const { x, y } = getCoordinates(target, root, scaleFactor);
70-
const mouseEventProps = {
71-
clientX: x,
72-
clientY: y
73-
};
74-
const clickElement = document.elementFromPoint(x, y);
75-
if (!clickElement) {
76-
throw new Error(`Could not identify element at (${x},${y})`, clickElement);
77-
}
78-
await triggerEvent(clickElement, 'mousedown', mouseEventProps);
79-
await triggerEvent(document, 'mouseup', mouseEventProps);
80-
log('clickOver done', root, scaleFactor, mouseEventProps);
81-
}();
42+
export default async function clickOver(target) {
43+
const root = getRootElement();
44+
const { x, y } = getCoordinates(target, root);
45+
const mouseEventProps = {
46+
clientX: x,
47+
clientY: y,
48+
};
49+
// elementFromPoint only works for points that are within the viewport
50+
if (x > window.innerWidth || y > window.innerHeight) {
51+
window.scrollTo(x, y);
52+
// start over since the viewport changed (and thus coordinates changed)
53+
return clickOver(target);
54+
}
55+
const clickElement = document.elementFromPoint(x, y);
56+
if (!clickElement) {
57+
throw new Error(`Could not identify element at (${x},${y})`, clickElement);
58+
}
59+
await triggerEvent(clickElement, 'mousedown', mouseEventProps);
60+
await triggerEvent(document, 'mouseup', mouseEventProps);
61+
log('clickOver done', root, mouseEventProps);
8262
}

tests/integration/components/plot-ly-test.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,11 @@ module('Integration | Component | plot-ly', function (hooks) {
7777
size: 32
7878
}
7979
}],
80-
// FIXME: plotly.js seems to break under `transform: scale(...)`
81-
// so we're "cheating" by moving the dot toward the "top left" of the plot area
82-
// so we can compensate our click location for it
8380
chartLayout: {
84-
xaxis: {
85-
range: [-1, 15]
86-
},
87-
yaxis: {
88-
range: [-8, 1]
89-
}
9081
},
9182
plotlyEvents: ['plotly_click', 'plotly_restyle'],
92-
onPlotlyEvent(eventName) {
93-
log(`onPlotlyEvent('${eventName}') fired`);
83+
onPlotlyEvent(eventName, ...otherArgs) {
84+
log(`onPlotlyEvent('${eventName}') fired`, ...otherArgs);
9485
assert.equal('plotly_click', eventName, 'Should receive plotly_click event');
9586
done();
9687
}

0 commit comments

Comments
 (0)