Skip to content

Commit 8c1f569

Browse files
committed
Add tests for "anywhere" events over editable shapes
1 parent e481f14 commit 8c1f569

1 file changed

Lines changed: 189 additions & 3 deletions

File tree

test/jasmine/tests/hover_click_anywhere_test.js

Lines changed: 189 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var createGraphDiv = require('../assets/create_graph_div');
66
var destroyGraphDiv = require('../assets/destroy_graph_div');
77
var click = require('../assets/click');
88

9-
function makePlot(gd, layoutExtras) {
9+
function makePlot(gd, layoutExtras = {}, configExtras) {
1010
return Plotly.newPlot(
1111
gd,
1212
[
@@ -26,8 +26,9 @@ function makePlot(gd, layoutExtras) {
2626
yaxis: { range: [0, 10] },
2727
hovermode: 'closest'
2828
},
29-
layoutExtras || {}
30-
)
29+
layoutExtras
30+
),
31+
configExtras
3132
);
3233
}
3334

@@ -149,6 +150,101 @@ describe('hoveranywhere', function () {
149150
})
150151
.then(done, done.fail);
151152
});
153+
154+
it('emits plotly_hover over an editable shape', (done) => {
155+
let hoverData;
156+
157+
makePlot(gd, {
158+
hoveranywhere: true,
159+
shapes: [
160+
{
161+
type: 'rect',
162+
x0: 6,
163+
x1: 9,
164+
y0: 6,
165+
y1: 9,
166+
fillcolor: 'rgba(0, 128, 255, 0.8)',
167+
editable: true
168+
}
169+
]
170+
})
171+
.then(() => {
172+
gd.on('plotly_hover', (d) => {
173+
hoverData = d;
174+
});
175+
176+
// Dispatch mousemove directly on the shape path element,
177+
// which has pointer-events that intercept events from the
178+
// underlying maindrag.
179+
const shapePath = gd.querySelector('.shape-group path');
180+
expect(shapePath).toBeDefined();
181+
182+
const bb = gd.getBoundingClientRect();
183+
const s = gd._fullLayout._size;
184+
// center of shape at data (7.5, 7.5) = plot-area px (225, 75)
185+
shapePath.dispatchEvent(
186+
new MouseEvent('mousemove', {
187+
bubbles: true,
188+
clientX: bb.left + s.l + 225,
189+
clientY: bb.top + s.t + 75
190+
})
191+
);
192+
Lib.clearThrottle();
193+
194+
expect(hoverData).toBeDefined();
195+
expect(hoverData.points).toEqual([]);
196+
expect(hoverData.xvals[0]).toBeCloseTo(7.5, 1);
197+
expect(hoverData.yvals[0]).toBeCloseTo(7.5, 1);
198+
})
199+
.then(done, done.fail);
200+
});
201+
202+
it('emits plotly_hover over a shape with edits.shapePosition', (done) => {
203+
let hoverData;
204+
205+
makePlot(
206+
gd,
207+
{
208+
hoveranywhere: true,
209+
shapes: [
210+
{
211+
type: 'rect',
212+
x0: 6,
213+
x1: 9,
214+
y0: 6,
215+
y1: 9,
216+
fillcolor: 'rgba(0, 128, 255, 0.8)'
217+
}
218+
]
219+
},
220+
{ edits: { shapePosition: true } }
221+
)
222+
.then(() => {
223+
gd.on('plotly_hover', (d) => {
224+
hoverData = d;
225+
});
226+
227+
const shapePath = gd.querySelector('.shape-group path');
228+
expect(shapePath).toBeDefined();
229+
230+
const bb = gd.getBoundingClientRect();
231+
const s = gd._fullLayout._size;
232+
shapePath.dispatchEvent(
233+
new MouseEvent('mousemove', {
234+
bubbles: true,
235+
clientX: bb.left + s.l + 225,
236+
clientY: bb.top + s.t + 75
237+
})
238+
);
239+
Lib.clearThrottle();
240+
241+
expect(hoverData).toBeDefined();
242+
expect(hoverData.points).toEqual([]);
243+
expect(hoverData.xvals[0]).toBeCloseTo(7.5, 1);
244+
expect(hoverData.yvals[0]).toBeCloseTo(7.5, 1);
245+
})
246+
.then(done, done.fail);
247+
});
152248
});
153249

154250
describe('clickanywhere', function () {
@@ -205,4 +301,94 @@ describe('clickanywhere', function () {
205301
})
206302
.then(done, done.fail);
207303
});
304+
305+
it('emits plotly_click over an editable shape', (done) => {
306+
let clickData;
307+
308+
makePlot(gd, {
309+
clickanywhere: true,
310+
shapes: [
311+
{
312+
type: 'rect',
313+
x0: 6,
314+
x1: 9,
315+
y0: 6,
316+
y1: 9,
317+
fillcolor: 'rgba(0, 128, 255, 0.8)',
318+
editable: true
319+
}
320+
]
321+
})
322+
.then(() => {
323+
gd.on('plotly_click', (d) => {
324+
clickData = d;
325+
});
326+
327+
const shapePath = gd.querySelector('.shape-group path');
328+
expect(shapePath).toBeDefined();
329+
330+
const bb = gd.getBoundingClientRect();
331+
const s = gd._fullLayout._size;
332+
// center of shape at data (7.5, 7.5) = plot-area px (225, 75)
333+
shapePath.dispatchEvent(
334+
new MouseEvent('click', {
335+
bubbles: true,
336+
clientX: bb.left + s.l + 225,
337+
clientY: bb.top + s.t + 75
338+
})
339+
);
340+
341+
expect(clickData).toBeDefined();
342+
expect(clickData.points).toEqual([]);
343+
expect(clickData.xvals[0]).toBeCloseTo(7.5, 1);
344+
expect(clickData.yvals[0]).toBeCloseTo(7.5, 1);
345+
})
346+
.then(done, done.fail);
347+
});
348+
349+
it('emits plotly_click over a shape with edits.shapePosition', (done) => {
350+
let clickData;
351+
352+
makePlot(
353+
gd,
354+
{
355+
clickanywhere: true,
356+
shapes: [
357+
{
358+
type: 'rect',
359+
x0: 6,
360+
x1: 9,
361+
y0: 6,
362+
y1: 9,
363+
fillcolor: 'rgba(0, 128, 255, 0.8)'
364+
}
365+
]
366+
},
367+
{ edits: { shapePosition: true } }
368+
)
369+
.then(() => {
370+
gd.on('plotly_click', (d) => {
371+
clickData = d;
372+
});
373+
374+
const shapePath = gd.querySelector('.shape-group path');
375+
expect(shapePath).toBeDefined();
376+
377+
const bb = gd.getBoundingClientRect();
378+
const s = gd._fullLayout._size;
379+
shapePath.dispatchEvent(
380+
new MouseEvent('click', {
381+
bubbles: true,
382+
clientX: bb.left + s.l + 225,
383+
clientY: bb.top + s.t + 75
384+
})
385+
);
386+
387+
expect(clickData).toBeDefined();
388+
expect(clickData.points).toEqual([]);
389+
expect(clickData.xvals[0]).toBeCloseTo(7.5, 1);
390+
expect(clickData.yvals[0]).toBeCloseTo(7.5, 1);
391+
})
392+
.then(done, done.fail);
393+
});
208394
});

0 commit comments

Comments
 (0)