@@ -2,45 +2,23 @@ import { getRootElement, triggerEvent } from '@ember/test-helpers';
22import debug from 'debug' ;
33const 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 ( / m a t r i x \( ( .* ) \) / ) ;
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}
0 commit comments