Skip to content

Commit a95e1c0

Browse files
committed
events: update tests
* Test the property event in `plotly_click`, `plotly_hover` and `plotly_unhover`. * Test modified clicks (e.g. ctrlKey). * Updated the helper function click. Added a click event after mouseup, otherwise click events in pie plots don't get triggered.
1 parent 22a9396 commit a95e1c0

File tree

7 files changed

+981
-19
lines changed

7 files changed

+981
-19
lines changed

test/jasmine/assets/click.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var mouseEvent = require('./mouse_event');
22

3-
module.exports = function click(x, y) {
4-
mouseEvent('mousemove', x, y);
5-
mouseEvent('mousedown', x, y);
6-
mouseEvent('mouseup', x, y);
3+
module.exports = function click(x, y, opts) {
4+
mouseEvent('mousemove', x, y, opts);
5+
mouseEvent('mousedown', x, y, opts);
6+
mouseEvent('mouseup', x, y, opts);
7+
mouseEvent('click', x, y, opts);
78
};

test/jasmine/assets/mouse_event.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ module.exports = function(type, x, y, opts) {
99
if(opts && opts.buttons) {
1010
fullOpts.buttons = opts.buttons;
1111
}
12+
if(opts && opts.altKey) {
13+
fullOpts.altKey = opts.altKey;
14+
}
15+
if(opts && opts.ctrlKey) {
16+
fullOpts.ctrlKey = opts.ctrlKey;
17+
}
18+
if(opts && opts.metaKey) {
19+
fullOpts.metaKey = opts.metaKey;
20+
}
21+
if(opts && opts.shiftKey) {
22+
fullOpts.shiftKey = opts.shiftKey;
23+
}
1224

1325
var el = (opts && opts.element) || document.elementFromPoint(x, y),
1426
ev;

test/jasmine/tests/click_test.js

Lines changed: 114 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ var customMatchers = require('../assets/custom_matchers');
1616
var click = require('../assets/click');
1717
var doubleClickRaw = require('../assets/double_click');
1818

19+
function move(fromX, fromY, toX, toY, delay) {
20+
return new Promise(function(resolve) {
21+
mouseEvent('mousemove', fromX, fromY);
22+
23+
setTimeout(function() {
24+
mouseEvent('mousemove', toX, toY);
25+
resolve();
26+
}, delay || DBLCLICKDELAY / 4);
27+
});
28+
}
29+
30+
function drag(fromX, fromY, toX, toY, delay) {
31+
return new Promise(function(resolve) {
32+
mouseEvent('mousemove', fromX, fromY);
33+
mouseEvent('mousedown', fromX, fromY);
34+
mouseEvent('mousemove', toX, toY);
35+
36+
setTimeout(function() {
37+
mouseEvent('mouseup', toX, toY);
38+
resolve();
39+
}, delay || DBLCLICKDELAY / 4);
40+
});
41+
}
42+
1943

2044
describe('Test click interactions:', function() {
2145
var mock = require('@mocks/14.json');
@@ -39,19 +63,6 @@ describe('Test click interactions:', function() {
3963

4064
afterEach(destroyGraphDiv);
4165

42-
function drag(fromX, fromY, toX, toY, delay) {
43-
return new Promise(function(resolve) {
44-
mouseEvent('mousemove', fromX, fromY);
45-
mouseEvent('mousedown', fromX, fromY);
46-
mouseEvent('mousemove', toX, toY);
47-
48-
setTimeout(function() {
49-
mouseEvent('mouseup', toX, toY);
50-
resolve();
51-
}, delay || DBLCLICKDELAY / 4);
52-
});
53-
}
54-
5566
function doubleClick(x, y) {
5667
return doubleClickRaw(x, y).then(function() {
5768
return Plotly.Plots.previousPromises(gd);
@@ -87,6 +98,55 @@ describe('Test click interactions:', function() {
8798
expect(pt.pointNumber).toEqual(11);
8899
expect(pt.x).toEqual(0.125);
89100
expect(pt.y).toEqual(2.125);
101+
102+
var evt = futureData.event;
103+
expect(evt.clientX).toEqual(pointPos[0]);
104+
expect(evt.clientY).toEqual(pointPos[1]);
105+
});
106+
});
107+
108+
describe('modified click events', function() {
109+
var clickOpts = {
110+
altKey: true,
111+
ctrlKey: true,
112+
metaKey: true,
113+
shiftKey: true
114+
},
115+
futureData;
116+
117+
beforeEach(function(done) {
118+
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
119+
120+
gd.on('plotly_click', function(data) {
121+
futureData = data;
122+
});
123+
});
124+
125+
it('should not be trigged when not on data points', function() {
126+
click(blankPos[0], blankPos[1], clickOpts);
127+
expect(futureData).toBe(undefined);
128+
});
129+
130+
it('should contain the correct fields', function() {
131+
click(pointPos[0], pointPos[1], clickOpts);
132+
expect(futureData.points.length).toEqual(1);
133+
134+
var pt = futureData.points[0];
135+
expect(Object.keys(pt)).toEqual([
136+
'data', 'fullData', 'curveNumber', 'pointNumber',
137+
'x', 'y', 'xaxis', 'yaxis'
138+
]);
139+
expect(pt.curveNumber).toEqual(0);
140+
expect(pt.pointNumber).toEqual(11);
141+
expect(pt.x).toEqual(0.125);
142+
expect(pt.y).toEqual(2.125);
143+
144+
var evt = futureData.event;
145+
expect(evt.clientX).toEqual(pointPos[0]);
146+
expect(evt.clientY).toEqual(pointPos[1]);
147+
Object.getOwnPropertyNames(clickOpts).forEach(function(opt) {
148+
expect(evt[opt]).toEqual(clickOpts[opt], opt);
149+
});
90150
});
91151
});
92152

@@ -191,6 +251,46 @@ describe('Test click interactions:', function() {
191251
expect(pt.pointNumber).toEqual(11);
192252
expect(pt.x).toEqual(0.125);
193253
expect(pt.y).toEqual(2.125);
254+
255+
var evt = futureData.event;
256+
expect(evt.clientX).toEqual(pointPos[0]);
257+
expect(evt.clientY).toEqual(pointPos[1]);
258+
});
259+
});
260+
261+
describe('plotly_unhover event with hoverinfo set to none', function() {
262+
var futureData;
263+
264+
beforeEach(function(done) {
265+
266+
var modifiedMockCopy = Lib.extendDeep({}, mockCopy);
267+
modifiedMockCopy.data[0].hoverinfo = 'none';
268+
Plotly.plot(gd, modifiedMockCopy.data, modifiedMockCopy.layout)
269+
.then(done);
270+
271+
gd.on('plotly_unhover', function(data) {
272+
futureData = data;
273+
});
274+
});
275+
276+
it('should contain the correct fields despite hoverinfo: "none"', function(done) {
277+
move(pointPos[0], pointPos[1], blankPos[0], blankPos[1]).then(function() {
278+
expect(futureData.points.length).toEqual(1);
279+
280+
var pt = futureData.points[0];
281+
expect(Object.keys(pt)).toEqual([
282+
'data', 'fullData', 'curveNumber', 'pointNumber',
283+
'x', 'y', 'xaxis', 'yaxis'
284+
]);
285+
expect(pt.curveNumber).toEqual(0);
286+
expect(pt.pointNumber).toEqual(11);
287+
expect(pt.x).toEqual(0.125);
288+
expect(pt.y).toEqual(2.125);
289+
290+
var evt = futureData.event;
291+
expect(evt.clientX).toEqual(blankPos[0]);
292+
expect(evt.clientY).toEqual(blankPos[1]);
293+
}).then(done);
194294
});
195295
});
196296

@@ -817,6 +917,7 @@ describe('Test click interactions:', function() {
817917
});
818918
});
819919

920+
820921
describe('dragbox', function() {
821922

822923
afterEach(destroyGraphDiv);

0 commit comments

Comments
 (0)