-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathgeo_interact_test.js
540 lines (426 loc) · 19 KB
/
geo_interact_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
var d3 = require('d3');
var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var mouseEvent = require('../assets/mouse_event');
describe('Test geo interactions', function() {
'use strict';
afterEach(destroyGraphDiv);
describe('mock geo_first.json', function() {
var mock = require('@mocks/geo_first.json');
var gd;
function mouseEventScatterGeo(type) {
mouseEvent(type, 300, 235);
}
function mouseEventChoropleth(type) {
mouseEvent(type, 400, 160);
}
function countTraces(type) {
return d3.selectAll('g.trace.' + type).size();
}
function countGeos() {
return d3.select('div.geo-container').selectAll('div').size();
}
function countColorBars() {
return d3.select('g.infolayer').selectAll('.cbbg').size();
}
beforeEach(function(done) {
gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
});
describe('scattergeo hover labels', function() {
beforeEach(function() {
mouseEventScatterGeo('mouseover');
});
it('should show one hover text group', function() {
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
});
it('should show longitude and latitude values', function() {
var node = d3.selectAll('g.hovertext').selectAll('tspan')[0][0];
expect(node.innerHTML).toEqual('(0°, 0°)');
});
it('should show the trace name', function() {
var node = d3.selectAll('g.hovertext').selectAll('text')[0][0];
expect(node.innerHTML).toEqual('trace 0');
});
});
describe('scattergeo hover events', function() {
var ptData;
beforeEach(function() {
gd.on('plotly_hover', function(eventData) {
ptData = eventData.points[0];
});
mouseEventScatterGeo('mouseover');
});
it('should contain the correct fields', function() {
expect(Object.keys(ptData)).toEqual([
'data', 'fullData', 'curveNumber', 'pointNumber',
'lon', 'lat', 'location'
]);
});
it('should show the correct point data', function() {
expect(ptData.lon).toEqual(0);
expect(ptData.lat).toEqual(0);
expect(ptData.location).toBe(null);
expect(ptData.curveNumber).toEqual(0);
expect(ptData.pointNumber).toEqual(0);
});
});
describe('scattergeo click events', function() {
var ptData;
beforeEach(function() {
gd.on('plotly_click', function(eventData) {
ptData = eventData.points[0];
});
mouseEventScatterGeo('click');
});
it('should contain the correct fields', function() {
expect(Object.keys(ptData)).toEqual([
'data', 'fullData', 'curveNumber', 'pointNumber',
'lon', 'lat', 'location'
]);
});
it('should show the correct point data', function() {
expect(ptData.lon).toEqual(0);
expect(ptData.lat).toEqual(0);
expect(ptData.location).toBe(null);
expect(ptData.curveNumber).toEqual(0);
expect(ptData.pointNumber).toEqual(0);
});
});
describe('scattergeo unhover events', function() {
var ptData;
beforeEach(function() {
gd.on('plotly_unhover', function(eventData) {
ptData = eventData.points[0];
});
mouseEventScatterGeo('mouseover');
mouseEventScatterGeo('mouseout');
});
it('should contain the correct fields', function() {
expect(Object.keys(ptData)).toEqual([
'data', 'fullData', 'curveNumber', 'pointNumber',
'lon', 'lat', 'location'
]);
});
it('should show the correct point data', function() {
expect(ptData.lon).toEqual(0);
expect(ptData.lat).toEqual(0);
expect(ptData.location).toBe(null);
expect(ptData.curveNumber).toEqual(0);
expect(ptData.pointNumber).toEqual(0);
});
});
describe('choropleth hover labels', function() {
beforeEach(function() {
mouseEventChoropleth('mouseover');
});
it('should show one hover text group', function() {
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
});
it('should show location and z values', function() {
var node = d3.selectAll('g.hovertext').selectAll('tspan')[0];
expect(node[0].innerHTML).toEqual('RUS');
expect(node[1].innerHTML).toEqual('10');
});
it('should show the trace name', function() {
var node = d3.selectAll('g.hovertext').selectAll('text')[0][0];
expect(node.innerHTML).toEqual('trace 1');
});
});
describe('choropleth hover events', function() {
var ptData;
beforeEach(function() {
gd.on('plotly_hover', function(eventData) {
ptData = eventData.points[0];
});
mouseEventChoropleth('mouseover');
});
it('should contain the correct fields', function() {
expect(Object.keys(ptData)).toEqual([
'data', 'fullData', 'curveNumber', 'pointNumber',
'location', 'z'
]);
});
it('should show the correct point data', function() {
expect(ptData.location).toBe('RUS');
expect(ptData.z).toEqual(10);
expect(ptData.curveNumber).toEqual(1);
expect(ptData.pointNumber).toEqual(2);
});
});
describe('choropleth click events', function() {
var ptData;
beforeEach(function() {
gd.on('plotly_click', function(eventData) {
ptData = eventData.points[0];
});
mouseEventChoropleth('click');
});
it('should contain the correct fields', function() {
expect(Object.keys(ptData)).toEqual([
'data', 'fullData', 'curveNumber', 'pointNumber',
'location', 'z'
]);
});
it('should show the correct point data', function() {
expect(ptData.location).toBe('RUS');
expect(ptData.z).toEqual(10);
expect(ptData.curveNumber).toEqual(1);
expect(ptData.pointNumber).toEqual(2);
});
});
describe('choropleth unhover events', function() {
var ptData;
beforeEach(function() {
gd.on('plotly_unhover', function(eventData) {
ptData = eventData.points[0];
});
mouseEventChoropleth('mouseover');
mouseEventChoropleth('mouseout');
});
it('should contain the correct fields', function() {
expect(Object.keys(ptData)).toEqual([
'data', 'fullData', 'curveNumber', 'pointNumber',
'location', 'z'
]);
});
it('should show the correct point data', function() {
expect(ptData.location).toBe('RUS');
expect(ptData.z).toEqual(10);
expect(ptData.curveNumber).toEqual(1);
expect(ptData.pointNumber).toEqual(2);
});
});
describe('trace visibility toggle', function() {
it('should toggle scattergeo elements', function(done) {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
Plotly.restyle(gd, 'visible', false, [0]).then(function() {
expect(countTraces('scattergeo')).toBe(0);
expect(countTraces('choropleth')).toBe(1);
return Plotly.restyle(gd, 'visible', true, [0]);
}).then(function() {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
done();
});
});
it('should toggle choropleth elements', function(done) {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
Plotly.restyle(gd, 'visible', false, [1]).then(function() {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(0);
return Plotly.restyle(gd, 'visible', true, [1]);
}).then(function() {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
done();
});
});
});
describe('deleting traces and geos', function() {
it('should delete traces in succession', function(done) {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
expect(countGeos()).toBe(1);
expect(countColorBars()).toBe(1);
Plotly.deleteTraces(gd, [0]).then(function() {
expect(countTraces('scattergeo')).toBe(0);
expect(countTraces('choropleth')).toBe(1);
expect(countGeos()).toBe(1);
expect(countColorBars()).toBe(1);
return Plotly.deleteTraces(gd, [0]);
}).then(function() {
expect(countTraces('scattergeo')).toBe(0);
expect(countTraces('choropleth')).toBe(0);
expect(countGeos()).toBe(0, '- trace-less geo subplot are deleted');
expect(countColorBars()).toBe(0);
return Plotly.relayout(gd, 'geo', null);
}).then(function() {
expect(countTraces('scattergeo')).toBe(0);
expect(countTraces('choropleth')).toBe(0);
expect(countGeos()).toBe(0);
expect(countColorBars()).toBe(0);
done();
});
});
});
describe('streaming calls', function() {
var INTERVAL = 10;
var N_MARKERS_AT_START = Math.min(
mock.data[0].lat.length,
mock.data[0].lon.length
);
var N_LOCATIONS_AT_START = mock.data[1].locations.length;
var lonQueue = [45, -45, 12, 20],
latQueue = [-75, 80, 5, 10],
textQueue = ['c', 'd', 'e', 'f'],
locationsQueue = ['AUS', 'FRA', 'DEU', 'MEX'],
zQueue = [100, 20, 30, 12];
beforeEach(function(done) {
var update = {
mode: 'lines+markers+text',
text: [['a', 'b']],
'marker.size': 10
};
Plotly.restyle(gd, update, [0]).then(done);
});
function countScatterGeoLines() {
return d3.selectAll('g.trace.scattergeo')
.selectAll('path.js-line')
.size();
}
function countScatterGeoMarkers() {
return d3.selectAll('g.trace.scattergeo')
.selectAll('path.point')
.size();
}
function countScatterGeoTextGroups() {
return d3.selectAll('g.trace.scattergeo')
.selectAll('g')
.size();
}
function countScatterGeoTextNodes() {
return d3.selectAll('g.trace.scattergeo')
.selectAll('g')
.select('text')
.size();
}
function checkScatterGeoOrder() {
var order = ['js-path', 'point', null];
var nodes = d3.selectAll('g.trace.scattergeo');
nodes.each(function() {
var list = [];
d3.select(this).selectAll('*').each(function() {
var className = d3.select(this).attr('class');
list.push(className);
});
var listSorted = list.slice().sort(function(a, b) {
return order.indexOf(a) - order.indexOf(b);
});
expect(list).toEqual(listSorted);
});
}
function countChoroplethPaths() {
return d3.selectAll('g.trace.choropleth')
.selectAll('path.choroplethlocation')
.size();
}
it('should be able to add line/marker/text nodes', function(done) {
var i = 0;
var interval = setInterval(function() {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
expect(countScatterGeoLines()).toBe(1);
expect(countScatterGeoMarkers()).toBe(N_MARKERS_AT_START + i);
expect(countScatterGeoTextGroups()).toBe(N_MARKERS_AT_START + i);
expect(countScatterGeoTextNodes()).toBe(N_MARKERS_AT_START + i);
checkScatterGeoOrder();
var trace = gd.data[0];
trace.lon.push(lonQueue[i]);
trace.lat.push(latQueue[i]);
trace.text.push(textQueue[i]);
if(i === lonQueue.length - 1) {
clearInterval(interval);
done();
}
Plotly.plot(gd);
i++;
}, INTERVAL);
});
it('should be able to shift line/marker/text nodes', function(done) {
var i = 0;
var interval = setInterval(function() {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
expect(countScatterGeoLines()).toBe(1);
expect(countScatterGeoMarkers()).toBe(N_MARKERS_AT_START);
expect(countScatterGeoTextGroups()).toBe(N_MARKERS_AT_START);
expect(countScatterGeoTextNodes()).toBe(N_MARKERS_AT_START);
checkScatterGeoOrder();
var trace = gd.data[0];
trace.lon.push(lonQueue[i]);
trace.lat.push(latQueue[i]);
trace.text.push(textQueue[i]);
trace.lon.shift();
trace.lat.shift();
trace.text.shift();
if(i === lonQueue.length - 1) {
clearInterval(interval);
done();
}
Plotly.plot(gd);
i++;
}, INTERVAL);
});
it('should be able to update line/marker/text nodes', function(done) {
var i = 0;
var interval = setInterval(function() {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
expect(countScatterGeoLines()).toBe(1);
expect(countScatterGeoMarkers()).toBe(N_MARKERS_AT_START);
expect(countScatterGeoTextGroups()).toBe(N_MARKERS_AT_START);
expect(countScatterGeoTextNodes()).toBe(N_MARKERS_AT_START);
checkScatterGeoOrder();
var trace = gd.data[0];
trace.lon.push(lonQueue[i]);
trace.lat.push(latQueue[i]);
trace.text.push(textQueue[i]);
trace.lon.shift();
trace.lat.shift();
trace.text.shift();
if(i === lonQueue.length - 1) {
clearInterval(interval);
done();
}
Plotly.plot(gd);
i++;
}, INTERVAL);
});
it('should be able to delete line/marker/text nodes and choropleth paths', function(done) {
var trace0 = gd.data[0];
trace0.lon.shift();
trace0.lat.shift();
trace0.text.shift();
var trace1 = gd.data[1];
trace1.locations.shift();
Plotly.plot(gd).then(function() {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
expect(countScatterGeoLines()).toBe(1);
expect(countScatterGeoMarkers()).toBe(N_MARKERS_AT_START - 1);
expect(countScatterGeoTextGroups()).toBe(N_MARKERS_AT_START - 1);
expect(countScatterGeoTextNodes()).toBe(N_MARKERS_AT_START - 1);
checkScatterGeoOrder();
expect(countChoroplethPaths()).toBe(N_LOCATIONS_AT_START - 1);
done();
});
});
it('should be able to update line/marker/text nodes and choropleth paths', function(done) {
var trace0 = gd.data[0];
trace0.lon = lonQueue;
trace0.lat = latQueue;
trace0.text = textQueue;
var trace1 = gd.data[1];
trace1.locations = locationsQueue;
trace1.z = zQueue;
Plotly.plot(gd).then(function() {
expect(countTraces('scattergeo')).toBe(1);
expect(countTraces('choropleth')).toBe(1);
expect(countScatterGeoLines()).toBe(1);
expect(countScatterGeoMarkers()).toBe(lonQueue.length);
expect(countScatterGeoTextGroups()).toBe(textQueue.length);
expect(countScatterGeoTextNodes()).toBe(textQueue.length);
checkScatterGeoOrder();
expect(countChoroplethPaths()).toBe(locationsQueue.length);
done();
});
});
});
});
});