|
1 | 1 | var d3 = require('d3');
|
2 | 2 |
|
3 | 3 | var Plotly = require('@lib/index');
|
| 4 | +var Lib = require('@src/lib'); |
4 | 5 |
|
5 | 6 | var createGraphDiv = require('../assets/create_graph_div');
|
6 | 7 | var destroyGraphDiv = require('../assets/destroy_graph_div');
|
@@ -38,7 +39,10 @@ describe('Test geo interactions', function() {
|
38 | 39 |
|
39 | 40 | beforeEach(function(done) {
|
40 | 41 | gd = createGraphDiv();
|
41 |
| - Plotly.plot(gd, mock.data, mock.layout).then(done); |
| 42 | + |
| 43 | + var mockCopy = Lib.extendDeep({}, mock); |
| 44 | + |
| 45 | + Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done); |
42 | 46 | });
|
43 | 47 |
|
44 | 48 | describe('scattergeo hover labels', function() {
|
@@ -261,5 +265,221 @@ describe('Test geo interactions', function() {
|
261 | 265 | });
|
262 | 266 | });
|
263 | 267 |
|
| 268 | + describe('streaming calls', function() { |
| 269 | + var INTERVAL = 10; |
| 270 | + |
| 271 | + var N_MARKERS_AT_START = Math.min( |
| 272 | + mock.data[0].lat.length, |
| 273 | + mock.data[0].lon.length |
| 274 | + ); |
| 275 | + |
| 276 | + var N_LOCATIONS_AT_START = mock.data[1].locations.length; |
| 277 | + |
| 278 | + var lonQueue = [45, -45, 12, 20], |
| 279 | + latQueue = [-75, 80, 5, 10], |
| 280 | + textQueue = ['c', 'd', 'e', 'f'], |
| 281 | + locationsQueue = ['AUS', 'FRA', 'DEU', 'MEX'], |
| 282 | + zQueue = [100, 20, 30, 12]; |
| 283 | + |
| 284 | + beforeEach(function(done) { |
| 285 | + var update = { |
| 286 | + mode: 'lines+markers+text', |
| 287 | + text: [['a', 'b']], |
| 288 | + 'marker.size': 10 |
| 289 | + }; |
| 290 | + |
| 291 | + Plotly.restyle(gd, update, [0]).then(done); |
| 292 | + }); |
| 293 | + |
| 294 | + function countScatterGeoLines() { |
| 295 | + return d3.selectAll('g.trace.scattergeo') |
| 296 | + .selectAll('path.js-line') |
| 297 | + .size(); |
| 298 | + } |
| 299 | + |
| 300 | + function countScatterGeoMarkers() { |
| 301 | + return d3.selectAll('g.trace.scattergeo') |
| 302 | + .selectAll('path.point') |
| 303 | + .size(); |
| 304 | + } |
| 305 | + |
| 306 | + function countScatterGeoTextGroups() { |
| 307 | + return d3.selectAll('g.trace.scattergeo') |
| 308 | + .selectAll('g') |
| 309 | + .size(); |
| 310 | + } |
| 311 | + |
| 312 | + function countScatterGeoTextNodes() { |
| 313 | + return d3.selectAll('g.trace.scattergeo') |
| 314 | + .selectAll('g') |
| 315 | + .select('text') |
| 316 | + .size(); |
| 317 | + } |
| 318 | + |
| 319 | + function checkScatterGeoOrder() { |
| 320 | + var order = ['js-path', 'point', null]; |
| 321 | + var nodes = d3.selectAll('g.trace.scattergeo'); |
| 322 | + |
| 323 | + nodes.each(function() { |
| 324 | + var list = []; |
| 325 | + |
| 326 | + d3.select(this).selectAll('*').each(function() { |
| 327 | + var className = d3.select(this).attr('class'); |
| 328 | + list.push(className); |
| 329 | + }); |
| 330 | + |
| 331 | + var listSorted = list.slice().sort(function(a, b) { |
| 332 | + return order.indexOf(a) - order.indexOf(b); |
| 333 | + }); |
| 334 | + |
| 335 | + expect(list).toEqual(listSorted); |
| 336 | + }); |
| 337 | + } |
| 338 | + |
| 339 | + function countChoroplethPaths() { |
| 340 | + return d3.selectAll('g.trace.choropleth') |
| 341 | + .selectAll('path.choroplethlocation') |
| 342 | + .size(); |
| 343 | + } |
| 344 | + |
| 345 | + it('should be able to add line/marker/text nodes', function(done) { |
| 346 | + var i = 0; |
| 347 | + |
| 348 | + var interval = setInterval(function() { |
| 349 | + expect(countTraces('scattergeo')).toBe(1); |
| 350 | + expect(countTraces('choropleth')).toBe(1); |
| 351 | + expect(countScatterGeoLines()).toBe(1); |
| 352 | + expect(countScatterGeoMarkers()).toBe(N_MARKERS_AT_START + i); |
| 353 | + expect(countScatterGeoTextGroups()).toBe(N_MARKERS_AT_START + i); |
| 354 | + expect(countScatterGeoTextNodes()).toBe(N_MARKERS_AT_START + i); |
| 355 | + checkScatterGeoOrder(); |
| 356 | + |
| 357 | + var trace = gd.data[0]; |
| 358 | + trace.lon.push(lonQueue[i]); |
| 359 | + trace.lat.push(latQueue[i]); |
| 360 | + trace.text.push(textQueue[i]); |
| 361 | + |
| 362 | + if(i === lonQueue.length - 1) { |
| 363 | + clearInterval(interval); |
| 364 | + done(); |
| 365 | + } |
| 366 | + |
| 367 | + Plotly.plot(gd); |
| 368 | + i++; |
| 369 | + }, INTERVAL); |
| 370 | + }); |
| 371 | + |
| 372 | + it('should be able to shift line/marker/text nodes', function(done) { |
| 373 | + var i = 0; |
| 374 | + |
| 375 | + var interval = setInterval(function() { |
| 376 | + expect(countTraces('scattergeo')).toBe(1); |
| 377 | + expect(countTraces('choropleth')).toBe(1); |
| 378 | + expect(countScatterGeoLines()).toBe(1); |
| 379 | + expect(countScatterGeoMarkers()).toBe(N_MARKERS_AT_START); |
| 380 | + expect(countScatterGeoTextGroups()).toBe(N_MARKERS_AT_START); |
| 381 | + expect(countScatterGeoTextNodes()).toBe(N_MARKERS_AT_START); |
| 382 | + checkScatterGeoOrder(); |
| 383 | + |
| 384 | + var trace = gd.data[0]; |
| 385 | + trace.lon.push(lonQueue[i]); |
| 386 | + trace.lat.push(latQueue[i]); |
| 387 | + trace.text.push(textQueue[i]); |
| 388 | + trace.lon.shift(); |
| 389 | + trace.lat.shift(); |
| 390 | + trace.text.shift(); |
| 391 | + |
| 392 | + if(i === lonQueue.length - 1) { |
| 393 | + clearInterval(interval); |
| 394 | + done(); |
| 395 | + } |
| 396 | + |
| 397 | + Plotly.plot(gd); |
| 398 | + i++; |
| 399 | + }, INTERVAL); |
| 400 | + }); |
| 401 | + |
| 402 | + it('should be able to update line/marker/text nodes', function(done) { |
| 403 | + var i = 0; |
| 404 | + |
| 405 | + var interval = setInterval(function() { |
| 406 | + expect(countTraces('scattergeo')).toBe(1); |
| 407 | + expect(countTraces('choropleth')).toBe(1); |
| 408 | + expect(countScatterGeoLines()).toBe(1); |
| 409 | + expect(countScatterGeoMarkers()).toBe(N_MARKERS_AT_START); |
| 410 | + expect(countScatterGeoTextGroups()).toBe(N_MARKERS_AT_START); |
| 411 | + expect(countScatterGeoTextNodes()).toBe(N_MARKERS_AT_START); |
| 412 | + checkScatterGeoOrder(); |
| 413 | + |
| 414 | + var trace = gd.data[0]; |
| 415 | + trace.lon.push(lonQueue[i]); |
| 416 | + trace.lat.push(latQueue[i]); |
| 417 | + trace.text.push(textQueue[i]); |
| 418 | + trace.lon.shift(); |
| 419 | + trace.lat.shift(); |
| 420 | + trace.text.shift(); |
| 421 | + |
| 422 | + if(i === lonQueue.length - 1) { |
| 423 | + clearInterval(interval); |
| 424 | + done(); |
| 425 | + } |
| 426 | + |
| 427 | + Plotly.plot(gd); |
| 428 | + i++; |
| 429 | + }, INTERVAL); |
| 430 | + }); |
| 431 | + |
| 432 | + it('should be able to delete line/marker/text nodes and choropleth paths', function(done) { |
| 433 | + var trace0 = gd.data[0]; |
| 434 | + trace0.lon.shift(); |
| 435 | + trace0.lat.shift(); |
| 436 | + trace0.text.shift(); |
| 437 | + |
| 438 | + var trace1 = gd.data[1]; |
| 439 | + trace1.locations.shift(); |
| 440 | + |
| 441 | + Plotly.plot(gd).then(function() { |
| 442 | + expect(countTraces('scattergeo')).toBe(1); |
| 443 | + expect(countTraces('choropleth')).toBe(1); |
| 444 | + |
| 445 | + expect(countScatterGeoLines()).toBe(1); |
| 446 | + expect(countScatterGeoMarkers()).toBe(N_MARKERS_AT_START - 1); |
| 447 | + expect(countScatterGeoTextGroups()).toBe(N_MARKERS_AT_START - 1); |
| 448 | + expect(countScatterGeoTextNodes()).toBe(N_MARKERS_AT_START - 1); |
| 449 | + checkScatterGeoOrder(); |
| 450 | + |
| 451 | + expect(countChoroplethPaths()).toBe(N_LOCATIONS_AT_START - 1); |
| 452 | + |
| 453 | + done(); |
| 454 | + }); |
| 455 | + }); |
| 456 | + |
| 457 | + it('should be able to update line/marker/text nodes and choropleth paths', function(done) { |
| 458 | + var trace0 = gd.data[0]; |
| 459 | + trace0.lon = lonQueue; |
| 460 | + trace0.lat = latQueue; |
| 461 | + trace0.text = textQueue; |
| 462 | + |
| 463 | + var trace1 = gd.data[1]; |
| 464 | + trace1.locations = locationsQueue; |
| 465 | + trace1.z = zQueue; |
| 466 | + |
| 467 | + Plotly.plot(gd).then(function() { |
| 468 | + expect(countTraces('scattergeo')).toBe(1); |
| 469 | + expect(countTraces('choropleth')).toBe(1); |
| 470 | + |
| 471 | + expect(countScatterGeoLines()).toBe(1); |
| 472 | + expect(countScatterGeoMarkers()).toBe(lonQueue.length); |
| 473 | + expect(countScatterGeoTextGroups()).toBe(textQueue.length); |
| 474 | + expect(countScatterGeoTextNodes()).toBe(textQueue.length); |
| 475 | + checkScatterGeoOrder(); |
| 476 | + |
| 477 | + expect(countChoroplethPaths()).toBe(locationsQueue.length); |
| 478 | + |
| 479 | + done(); |
| 480 | + }); |
| 481 | + }); |
| 482 | + |
| 483 | + }); |
264 | 484 | });
|
265 | 485 | });
|
0 commit comments