-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathanywhere_test.js
206 lines (185 loc) · 6.79 KB
/
anywhere_test.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var click = require('../assets/click');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var DBLCLICKDELAY = require('@src/plot_api/plot_config').dfltConfig.doubleClickDelay;
var clickEvent;
var clickedPromise;
function resetEvents(gd) {
clickEvent = null;
gd.removeAllListeners();
clickedPromise = new Promise(function(resolve) {
gd.on('plotly_click', function(data) {
clickEvent = data.points[0];
resolve();
});
});
}
describe('Click-to-select', function() {
var mock14PtsScatter = {
'in-margin': { x: 28, y: 28 },
'point-0': { x: 92, y: 102 },
'between-point-0-and-1': { x: 117, y: 110 },
'point-11': { x: 339, y: 214 },
};
var expectedEventsScatter = {
'in-margin': false,
'point-0': {
curveNumber: 0,
pointIndex: 0,
pointNumber: 0,
x: 0.002,
y: 16.25
},
'between-point-0-and-1': { x: 0.002990379231567056, y: 14.169142943944111 },
'point-11': {
curveNumber: 0,
pointIndex: 11,
pointNumber: 11,
x: 0.125,
y: 2.125
},
};
var mockPtsGeoscatter = {
'start': {lat: 40.7127, lon: -74.0059},
'end': {lat: 51.5072, lon: 0.1275},
};
var mockPtsGeoscatterClick = {
'in-margin': { x: 28, y: 28 },
'start': {x: 239, y: 174},
'end': {x: 426, y: 157},
'iceland': {x: 322, y: 150},
};
var expectedEventsGeoscatter = {
'in-margin': false,
'start': {
curveNumber: 0,
pointIndex: 0,
pointNumber: 0,
lat: 40.7127,
lon: -74.0059,
},
'end': {
curveNumber: 0,
pointIndex: 1,
pointNumber: 1,
lat: 51.5072,
lon: 51.5072,
},
'iceland': {lat: -18.666562962962963, lon: 56.66635185185185},
};
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(function() {
resetEvents(gd);
destroyGraphDiv();
});
function plotMock14Anywhere(layoutOpts) {
var mock = require('@mocks/14.json');
var defaultLayoutOpts = {
layout: {
clickmode: 'event+anywhere',
hoverdistance: 1
}
};
var mockCopy = Lib.extendDeep(
{},
mock,
defaultLayoutOpts,
{ layout: layoutOpts });
return Plotly.newPlot(gd, mockCopy.data, mockCopy.layout);
}
function plotMock14AnywhereSelect(layoutOpts) {
var mock = require('@mocks/14.json');
var defaultLayoutOpts = {
layout: {
clickmode: 'select+event+anywhere',
hoverdistance: 1
}
};
var mockCopy = Lib.extendDeep(
{},
mock,
defaultLayoutOpts,
{ layout: layoutOpts });
return Plotly.newPlot(gd, mockCopy.data, mockCopy.layout);
}
function plotGeoscatterAnywhere() {
var layout = {
clickmode: 'event+anywhere',
hoverdistance: 1
};
var data = [{
type: 'scattergeo',
lat: [ mockPtsGeoscatter.start.lat, mockPtsGeoscatter.end.lat ],
lon: [ mockPtsGeoscatter.start.lon, mockPtsGeoscatter.end.lat ],
mode: 'lines',
line: {
width: 2,
color: 'blue'
}
}];
return Plotly.newPlot(gd, data, layout);
}
function isSubset(superObj, subObj) {
return superObj === subObj ||
typeof superObj === 'object' &&
typeof subObj === 'object' && (
subObj.valueOf() === superObj.valueOf() ||
Object.keys(subObj).every(function(k) { return isSubset(superObj[k], subObj[k]); })
);
}
/**
* Executes a click and before resets event handlers.
* Returns the `clickedPromise` for convenience.
*/
function _click(x, y, clickOpts) {
resetEvents(gd);
setTimeout(function() {
click(x, y, clickOpts);
}, DBLCLICKDELAY * 1.03);
return clickedPromise;
}
function clickAndTestPoint(mockPts, expectedEvents, pointKey, clickOpts) {
var x = mockPts[pointKey].x;
var y = mockPts[pointKey].y;
var expectedEvent = expectedEvents[pointKey];
var result = _click(x, y, clickOpts);
if(expectedEvent) {
result.then(function() {
expect(isSubset(clickEvent, expectedEvent)).toBe(true);
});
} else {
expect(clickEvent).toBe(null);
result = null;
}
return result;
}
it('selects point and/or coordinate when clicked - scatter - event+anywhere', function(done) {
plotMock14Anywhere()
.then(function() { return clickAndTestPoint(mock14PtsScatter, expectedEventsScatter, 'in-margin'); })
.then(function() { return clickAndTestPoint(mock14PtsScatter, expectedEventsScatter, 'point-0'); })
.then(function() { return clickAndTestPoint(mock14PtsScatter, expectedEventsScatter, 'between-point-0-and-1'); })
.then(function() { return clickAndTestPoint(mock14PtsScatter, expectedEventsScatter, 'point-11'); })
.then(done, done.fail);
});
it('selects point and/or coordinate when clicked - scatter - select+event+anywhere', function(done) {
plotMock14AnywhereSelect()
.then(function() { return clickAndTestPoint(mock14PtsScatter, expectedEventsScatter, 'in-margin'); })
.then(function() { return clickAndTestPoint(mock14PtsScatter, expectedEventsScatter, 'point-0'); })
.then(function() { return clickAndTestPoint(mock14PtsScatter, expectedEventsScatter, 'between-point-0-and-1'); })
.then(function() { return clickAndTestPoint(mock14PtsScatter, expectedEventsScatter, 'point-11'); })
.then(done, done.fail);
});
it('selects point and/or coordinate when clicked - geoscatter - event+anywhere', function(done) {
plotGeoscatterAnywhere()
.then(function() { return clickAndTestPoint(mockPtsGeoscatterClick, expectedEventsGeoscatter, 'in-margin'); })
.then(function() { return clickAndTestPoint(mockPtsGeoscatterClick, expectedEventsGeoscatter, 'start'); })
.then(function() { return clickAndTestPoint(mockPtsGeoscatterClick, expectedEventsGeoscatter, 'end'); })
.then(function() { return clickAndTestPoint(mockPtsGeoscatterClick, expectedEventsGeoscatter, 'iceland'); })
.then(done, done.fail);
});
});