-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathhover_click_anywhere_test.js
More file actions
137 lines (107 loc) · 3.91 KB
/
hover_click_anywhere_test.js
File metadata and controls
137 lines (107 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var Plotly = require('../../../lib/index');
var Fx = require('../../../src/components/fx');
var Lib = require('../../../src/lib');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var click = require('../assets/click');
function makePlot(gd, layoutExtras) {
return Plotly.newPlot(gd, [{
x: [1, 2, 3],
y: [1, 3, 2],
type: 'scatter',
mode: 'markers'
}], Lib.extendFlat({
width: 400, height: 400,
margin: {l: 50, t: 50, r: 50, b: 50},
xaxis: {range: [0, 10]},
yaxis: {range: [0, 10]},
hovermode: 'closest'
}, layoutExtras || {}));
}
describe('hoveranywhere', function() {
'use strict';
var gd;
beforeEach(function() { gd = createGraphDiv(); });
afterEach(destroyGraphDiv);
function _hover(xPixel, yPixel) {
var bb = gd.getBoundingClientRect();
var s = gd._fullLayout._size;
Fx.hover(gd, {
clientX: xPixel + bb.left + s.l,
clientY: yPixel + bb.top + s.t,
target: gd.querySelector('.nsewdrag')
}, 'xy');
Lib.clearThrottle();
}
it('emits plotly_hover with coordinate data on empty space', function(done) {
var hoverData;
makePlot(gd, {hoveranywhere: true}).then(function() {
gd.on('plotly_hover', function(d) { hoverData = d; });
// hover over empty area (no data points nearby)
_hover(250, 50);
expect(hoverData).toBeDefined();
expect(hoverData.points).toEqual([]);
expect(hoverData.xvals.length).toBe(1);
expect(hoverData.yvals.length).toBe(1);
expect(typeof hoverData.xvals[0]).toBe('number');
})
.then(done, done.fail);
});
it('does not fire on empty space by default', function(done) {
var hoverData;
makePlot(gd).then(function() {
gd.on('plotly_hover', function(d) { hoverData = d; });
_hover(250, 50);
expect(hoverData).toBeUndefined();
})
.then(done, done.fail);
});
it('still returns normal point data on traces', function(done) {
var hoverData;
makePlot(gd, {hoveranywhere: true}).then(function() {
gd.on('plotly_hover', function(d) { hoverData = d; });
// hover near (2, 3)
_hover(60, 210);
expect(hoverData.points.length).toBe(1);
expect(hoverData.points[0].x).toBe(2);
expect(hoverData.points[0].y).toBe(3);
})
.then(done, done.fail);
});
it('respects hovermode:false', function(done) {
var hoverData;
makePlot(gd, {hoveranywhere: true, hovermode: false}).then(function() {
gd.on('plotly_hover', function(d) { hoverData = d; });
_hover(250, 50);
expect(hoverData).toBeUndefined();
})
.then(done, done.fail);
});
});
describe('clickanywhere', function() {
'use strict';
var gd;
beforeEach(function() { gd = createGraphDiv(); });
afterEach(destroyGraphDiv);
it('emits plotly_click with empty points on empty space', function(done) {
var clickData;
makePlot(gd, {clickanywhere: true}).then(function() {
gd.on('plotly_click', function(d) { clickData = d; });
var s = gd._fullLayout._size;
click(s.l + 250, s.t + 50);
expect(clickData).toBeDefined();
expect(clickData.points).toEqual([]);
})
.then(done, done.fail);
});
it('does not fire on empty space by default', function(done) {
var clickData;
makePlot(gd).then(function() {
gd.on('plotly_click', function(d) { clickData = d; });
var s = gd._fullLayout._size;
click(s.l + 250, s.t + 50);
expect(clickData).toBeUndefined();
})
.then(done, done.fail);
});
});