-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathplot_api_react_test.js
2027 lines (1771 loc) · 69.3 KB
/
plot_api_react_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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var Plotly = require('@lib/index');
var plotApi = require('@src/plot_api/plot_api');
var Lib = require('@src/lib');
var Axes = require('@src/plots/cartesian/axes');
var subroutines = require('@src/plot_api/subroutines');
var annotations = require('@src/components/annotations');
var images = require('@src/components/images');
var Registry = require('@src/registry');
var d3 = require('d3');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var failTest = require('../assets/fail_test');
var supplyAllDefaults = require('../assets/supply_defaults');
var mockLists = require('../assets/mock_lists');
var mouseEvent = require('../assets/mouse_event');
var drag = require('../assets/drag');
var MAPBOX_ACCESS_TOKEN = require('@build/credentials.json').MAPBOX_ACCESS_TOKEN;
describe('@noCIdep Plotly.react', function() {
var mockedMethods = [
'doTraceStyle',
'doColorBars',
'doLegend',
'layoutStyles',
'doTicksRelayout',
'doModeBar',
'doCamera'
];
var gd;
var afterPlotCnt;
beforeEach(function() {
gd = createGraphDiv();
spyOn(plotApi, 'plot').and.callThrough();
spyOn(Registry, 'call').and.callThrough();
mockedMethods.forEach(function(m) {
spyOn(subroutines, m).and.callThrough();
subroutines[m].calls.reset();
});
spyOn(annotations, 'drawOne').and.callThrough();
spyOn(annotations, 'draw').and.callThrough();
spyOn(images, 'draw').and.callThrough();
spyOn(Axes, 'draw').and.callThrough();
});
afterEach(destroyGraphDiv);
function countPlots() {
plotApi.plot.calls.reset();
subroutines.layoutStyles.calls.reset();
annotations.draw.calls.reset();
annotations.drawOne.calls.reset();
images.draw.calls.reset();
afterPlotCnt = 0;
gd.on('plotly_afterplot', function() { afterPlotCnt++; });
}
function countCalls(counts) {
var callsFinal = Lib.extendFlat({}, counts);
callsFinal.layoutStyles = (counts.layoutStyles || 0) + (counts.plot || 0);
mockedMethods.forEach(function(m) {
expect(subroutines[m]).toHaveBeenCalledTimes(callsFinal[m] || 0);
subroutines[m].calls.reset();
});
// calls to Plotly.plot via plot_api.js or Registry.call('plot')
var plotCalls = plotApi.plot.calls.count() +
Registry.call.calls.all()
.filter(function(d) { return d.args[0] === 'plot'; })
.length;
expect(plotCalls).toBe(counts.plot || 0, 'Plotly.plot calls');
plotApi.plot.calls.reset();
Registry.call.calls.reset();
// only consider annotation and image draw calls if we *don't* do a full plot.
if(!counts.plot) {
expect(annotations.draw).toHaveBeenCalledTimes(counts.annotationDraw || 0);
expect(annotations.drawOne).toHaveBeenCalledTimes(counts.annotationDrawOne || 0);
expect(images.draw).toHaveBeenCalledTimes(counts.imageDraw || 0);
}
annotations.draw.calls.reset();
annotations.drawOne.calls.reset();
images.draw.calls.reset();
expect(afterPlotCnt).toBe(1, 'plotly_afterplot should be called only once per edit');
afterPlotCnt = 0;
}
it('can add / remove traces', function(done) {
var data1 = [{y: [1, 2, 3], mode: 'markers'}];
var data2 = [data1[0], {y: [2, 3, 1], mode: 'markers'}];
var layout = {};
Plotly.newPlot(gd, data1, layout)
.then(countPlots)
.then(function() {
expect(d3.selectAll('.point').size()).toBe(3);
return Plotly.react(gd, data2, layout);
})
.then(function() {
expect(d3.selectAll('.point').size()).toBe(6);
return Plotly.react(gd, data1, layout);
})
.then(function() {
expect(d3.selectAll('.point').size()).toBe(3);
})
.catch(failTest)
.then(done);
});
it('should notice new data by ===, without layout.datarevision', function(done) {
var data = [{y: [1, 2, 3], mode: 'markers'}];
var layout = {};
Plotly.newPlot(gd, data, layout)
.then(countPlots)
.then(function() {
expect(d3.selectAll('.point').size()).toBe(3);
data[0].y.push(4);
return Plotly.react(gd, data, layout);
})
.then(function() {
// didn't pick it up, as we modified in place!!!
expect(d3.selectAll('.point').size()).toBe(3);
countCalls({plot: 0});
data[0].y = [1, 2, 3, 4, 5];
return Plotly.react(gd, data, layout);
})
.then(function() {
// new object, we picked it up!
expect(d3.selectAll('.point').size()).toBe(5);
countCalls({plot: 1});
})
.catch(failTest)
.then(done);
});
it('should notice new layout.datarevision', function(done) {
var data = [{y: [1, 2, 3], mode: 'markers'}];
var layout = {datarevision: 1};
Plotly.newPlot(gd, data, layout)
.then(countPlots)
.then(function() {
expect(d3.selectAll('.point').size()).toBe(3);
data[0].y.push(4);
return Plotly.react(gd, data, layout);
})
.then(function() {
// didn't pick it up, as we didn't modify datarevision
expect(d3.selectAll('.point').size()).toBe(3);
countCalls({plot: 0});
data[0].y.push(5);
layout.datarevision = 'bananas';
return Plotly.react(gd, data, layout);
})
.then(function() {
// new revision, we picked it up!
expect(d3.selectAll('.point').size()).toBe(5);
countCalls({plot: 1});
})
.catch(failTest)
.then(done);
});
it('picks up partial redraws', function(done) {
var data = [{y: [1, 2, 3], mode: 'markers'}];
var layout = {};
Plotly.newPlot(gd, data, layout)
.then(countPlots)
.then(function() {
layout.title = 'XXXXX';
layout.hovermode = 'closest';
data[0].marker = {color: 'rgb(0, 100, 200)'};
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({layoutStyles: 1, doTraceStyle: 1, doModeBar: 1});
expect(d3.select('.gtitle').text()).toBe('XXXXX');
var points = d3.selectAll('.point');
expect(points.size()).toBe(3);
points.each(function() {
expect(window.getComputedStyle(this).fill).toBe('rgb(0, 100, 200)');
});
layout.showlegend = true;
layout.xaxis.tick0 = 0.1;
layout.xaxis.dtick = 0.3;
return Plotly.react(gd, data, layout);
})
.then(function() {
// legend and ticks get called initially, but then plot gets added during automargin
countCalls({doLegend: 1, doTicksRelayout: 1, plot: 1});
data = [{z: [[1, 2], [3, 4]], type: 'surface'}];
layout = {};
return Plotly.react(gd, data, layout);
})
.then(function() {
// we get an extra call to layoutStyles from marginPushersAgain due to the colorbar.
// Really need to simplify that pipeline...
countCalls({plot: 1, layoutStyles: 1});
layout.scene.camera = {up: {x: 1, y: -1, z: 0}};
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({doCamera: 1});
data[0].type = 'heatmap';
delete layout.scene;
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({plot: 1});
// ideally we'd just do this with `surface` but colorbar attrs have editType 'calc' there
// TODO: can we drop them to type: 'colorbars' even for the 3D types?
data[0].colorbar = {len: 0.6};
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({doColorBars: 1, plot: 1});
})
.catch(failTest)
.then(done);
});
it('picks up special dtick geo case', function(done) {
var data = [{type: 'scattergeo'}];
var layout = {};
function countLines() {
var path = d3.select(gd).select('.lataxis > path');
return path.attr('d').split('M').length;
}
Plotly.react(gd, data)
.then(countPlots)
.then(function() {
layout.geo = {lataxis: {showgrid: true, dtick: 10}};
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({plot: 1});
expect(countLines()).toBe(18);
})
.then(function() {
layout.geo.lataxis.dtick = 30;
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({plot: 1});
expect(countLines()).toBe(6);
})
.catch(failTest)
.then(done);
});
it('picks up minimal sequence for cartesian axis range updates', function(done) {
var data = [{y: [1, 2, 1]}];
var layout = {xaxis: {range: [1, 2]}};
var layout2 = {xaxis: {range: [0, 1]}};
Plotly.newPlot(gd, data, layout)
.then(countPlots)
.then(function() {
expect(Axes.draw).toHaveBeenCalledWith(gd, '');
return Plotly.react(gd, data, layout2);
})
.then(function() {
expect(Axes.draw).toHaveBeenCalledWith(gd, 'redraw');
expect(subroutines.layoutStyles).not.toHaveBeenCalled();
})
.catch(failTest)
.then(done);
});
it('redraws annotations one at a time', function(done) {
var data = [{y: [1, 2, 3], mode: 'markers'}];
var layout = {};
var ymax;
Plotly.newPlot(gd, data, layout)
.then(countPlots)
.then(function() {
ymax = layout.yaxis.range[1];
layout.annotations = [{
x: 1,
y: 4,
text: 'Way up high',
showarrow: false
}, {
x: 1,
y: 2,
text: 'On the data',
showarrow: false
}];
return Plotly.react(gd, data, layout);
})
.then(function() {
// autoranged - so we get a full replot
countCalls({plot: 1});
expect(d3.selectAll('.annotation').size()).toBe(2);
layout.annotations[1].bgcolor = 'rgb(200, 100, 0)';
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({annotationDrawOne: 1});
expect(window.getComputedStyle(d3.select('.annotation[data-index="1"] .bg').node()).fill)
.toBe('rgb(200, 100, 0)');
expect(layout.yaxis.range[1]).not.toBeCloseTo(ymax, 0);
layout.annotations[0].font = {color: 'rgb(0, 255, 0)'};
layout.annotations[1].bgcolor = 'rgb(0, 0, 255)';
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({annotationDrawOne: 2});
expect(window.getComputedStyle(d3.select('.annotation[data-index="0"] text').node()).fill)
.toBe('rgb(0, 255, 0)');
expect(window.getComputedStyle(d3.select('.annotation[data-index="1"] .bg').node()).fill)
.toBe('rgb(0, 0, 255)');
Lib.extendFlat(layout.annotations[0], {yref: 'paper', y: 0.8});
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({plot: 1});
expect(layout.yaxis.range[1]).toBeCloseTo(ymax, 0);
})
.catch(failTest)
.then(done);
});
it('redraws images all at once', function(done) {
var data = [{y: [1, 2, 3], mode: 'markers'}];
var layout = {};
var jsLogo = 'https://images.plot.ly/language-icons/api-home/js-logo.png';
var x, y, height, width;
Plotly.newPlot(gd, data, layout)
.then(countPlots)
.then(function() {
layout.images = [{
source: jsLogo,
xref: 'paper',
yref: 'paper',
x: 0.1,
y: 0.1,
sizex: 0.2,
sizey: 0.2
}, {
source: jsLogo,
xref: 'x',
yref: 'y',
x: 1,
y: 2,
sizex: 1,
sizey: 1
}];
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({imageDraw: 1});
expect(d3.selectAll('image').size()).toBe(2);
var n = d3.selectAll('image').node();
x = n.attributes.x.value;
y = n.attributes.y.value;
height = n.attributes.height.value;
width = n.attributes.width.value;
layout.images[0].y = 0.8;
layout.images[0].sizey = 0.4;
return Plotly.react(gd, data, layout);
})
.then(function() {
countCalls({imageDraw: 1});
var n = d3.selectAll('image').node();
expect(n.attributes.x.value).toBe(x);
expect(n.attributes.width.value).toBe(width);
expect(n.attributes.y.value).not.toBe(y);
expect(n.attributes.height.value).not.toBe(height);
})
.catch(failTest)
.then(done);
});
it('can change config, and always redraws', function(done) {
var data = [{y: [1, 2, 3]}];
var layout = {};
Plotly.newPlot(gd, data, layout)
.then(countPlots)
.then(function() {
expect(d3.selectAll('.drag').size()).toBe(11);
expect(d3.selectAll('.gtitle').size()).toBe(0);
return Plotly.react(gd, data, layout, {editable: true});
})
.then(function() {
expect(d3.selectAll('.drag').size()).toBe(11);
expect(d3.selectAll('.gtitle').text()).toBe('Click to enter Plot title');
countCalls({plot: 1});
return Plotly.react(gd, data, layout, {staticPlot: true});
})
.then(function() {
expect(d3.selectAll('.drag').size()).toBe(0);
expect(d3.selectAll('.gtitle').size()).toBe(0);
countCalls({plot: 1});
return Plotly.react(gd, data, layout, {});
})
.then(function() {
expect(d3.selectAll('.drag').size()).toBe(11);
expect(d3.selectAll('.gtitle').size()).toBe(0);
countCalls({plot: 1});
})
.catch(failTest)
.then(done);
});
it('can put polar plots into staticPlot mode', function(done) {
// tested separately since some of the relevant code is actually
// in cartesian/graph_interact... hopefully we'll fix that
// sometime and the test will still pass.
var data = [{r: [1, 2, 3], theta: [0, 120, 240], type: 'scatterpolar'}];
var layout = {};
Plotly.newPlot(gd, data, layout)
.then(countPlots)
.then(function() {
expect(d3.select(gd).selectAll('.drag').size()).toBe(4);
return Plotly.react(gd, data, layout, {staticPlot: true});
})
.then(function() {
expect(d3.select(gd).selectAll('.drag').size()).toBe(0);
return Plotly.react(gd, data, layout, {});
})
.then(function() {
expect(d3.select(gd).selectAll('.drag').size()).toBe(4);
})
.catch(failTest)
.then(done);
});
it('can change from scatter to category scatterpolar and back', function(done) {
function scatter() {
return {
data: [{x: ['a', 'b'], y: [1, 2]}],
layout: {width: 400, height: 400, margin: {r: 80, t: 20}}
};
}
function scatterpolar() {
return {
// the bug https://github.com/plotly/plotly.js/issues/3255
// required all of this to change:
// - type -> scatterpolar
// - category theta
// - margins changed
data: [{type: 'scatterpolar', r: [1, 2, 3], theta: ['a', 'b', 'c']}],
layout: {width: 400, height: 400, margin: {r: 80, t: 50}}
};
}
function countTraces(scatterTraces, polarTraces) {
expect(document.querySelectorAll('.scatter').length)
.toBe(scatterTraces + polarTraces);
expect(document.querySelectorAll('.xy .scatter').length)
.toBe(scatterTraces);
expect(document.querySelectorAll('.polar .scatter').length)
.toBe(polarTraces);
}
Plotly.newPlot(gd, scatter())
.then(function() {
countTraces(1, 0);
return Plotly.react(gd, scatterpolar());
})
.then(function() {
countTraces(0, 1);
return Plotly.react(gd, scatter());
})
.then(function() {
countTraces(1, 0);
})
.catch(failTest)
.then(done);
});
it('can change data in candlesticks multiple times', function(done) {
// test that we've fixed the original issue in
// https://github.com/plotly/plotly.js/issues/2510
function assertCalc(open, high, low, close) {
expect(gd.calcdata[0][0]).toEqual(jasmine.objectContaining({
min: low,
max: high,
med: close,
q1: Math.min(open, close),
q3: Math.max(open, close),
dir: close >= open ? 'increasing' : 'decreasing'
}));
}
var trace = {
type: 'candlestick',
low: [1],
open: [2],
close: [3],
high: [4]
};
Plotly.newPlot(gd, [trace])
.then(function() {
assertCalc(2, 4, 1, 3);
trace.low = [0];
return Plotly.react(gd, [trace]);
})
.then(function() {
assertCalc(2, 4, 0, 3);
trace.low = [-1];
return Plotly.react(gd, [trace]);
})
.then(function() {
assertCalc(2, 4, -1, 3);
trace.close = [1];
return Plotly.react(gd, [trace]);
})
.then(function() {
assertCalc(2, 4, -1, 1);
})
.catch(failTest)
.then(done);
});
function aggregatedPie(i) {
var labels = i <= 1 ?
['A', 'B', 'A', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A'] :
['X', 'Y', 'Z', 'Z', 'Y', 'Z', 'X', 'Z', 'Y', 'Z', 'X'];
var trace = {
type: 'pie',
values: [4, 1, 4, 4, 1, 4, 4, 2, 1, 1, 15],
labels: labels,
transforms: [{
type: 'aggregate',
groups: labels,
aggregations: [{target: 'values', func: 'sum'}]
}]
};
return {
data: [trace],
layout: {
datarevision: i,
colorway: ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
}
};
}
var aggPie1CD = [[
{v: 26, label: 'A', color: 'red', i: 0},
{v: 9, label: 'C', color: 'orange', i: 2},
{v: 6, label: 'B', color: 'yellow', i: 1}
]];
var aggPie2CD = [[
{v: 23, label: 'X', color: 'red', i: 0},
{v: 15, label: 'Z', color: 'orange', i: 2},
{v: 3, label: 'Y', color: 'yellow', i: 1}
]];
function aggregatedScatter(i) {
return {
data: [{
x: [1, 2, 3, 4, 6, 5],
y: [2, 1, 3, 5, 6, 4],
transforms: [{
type: 'aggregate',
groups: [1, -1, 1, -1, 1, -1],
aggregations: i > 1 ? [{func: 'last', target: 'x'}] : []
}]
}],
layout: {daterevision: i + 10}
};
}
var aggScatter1CD = [[
{x: 1, y: 2, i: 0},
{x: 2, y: 1, i: 1}
]];
var aggScatter2CD = [[
{x: 6, y: 2, i: 0},
{x: 5, y: 1, i: 1}
]];
function aggregatedParcoords(i) {
return {
data: [{
type: 'parcoords',
dimensions: [
{label: 'A', values: [1, 2, 3, 4]},
{label: 'B', values: [4, 3, 2, 1]}
],
transforms: i ? [{
type: 'aggregate',
groups: [1, 2, 1, 2],
aggregations: [
{target: 'dimensions[0].values', func: i > 1 ? 'avg' : 'first'},
{target: 'dimensions[1].values', func: i > 1 ? 'first' : 'avg'}
]
}] :
[]
}]
};
}
var aggParcoords0Vals = [[1, 2, 3, 4], [4, 3, 2, 1]];
var aggParcoords1Vals = [[1, 2], [3, 2]];
var aggParcoords2Vals = [[2, 3], [4, 3]];
function checkCalcData(expectedCD) {
return function() {
expect(gd.calcdata.length).toBe(expectedCD.length);
expectedCD.forEach(function(expectedCDi, i) {
var cdi = gd.calcdata[i];
expect(cdi.length).toBe(expectedCDi.length, i);
expectedCDi.forEach(function(expectedij, j) {
expect(cdi[j]).toEqual(jasmine.objectContaining(expectedij));
});
});
};
}
function checkValues(expectedVals) {
return function() {
expect(gd._fullData.length).toBe(1);
var dims = gd._fullData[0].dimensions;
expect(dims.length).toBe(expectedVals.length);
expectedVals.forEach(function(expected, i) {
expect(dims[i].values).toEqual(expected);
});
};
}
function reactTo(fig) {
return function() { return Plotly.react(gd, fig); };
}
it('can change pie aggregations', function(done) {
Plotly.newPlot(gd, aggregatedPie(1))
.then(checkCalcData(aggPie1CD))
.then(reactTo(aggregatedPie(2)))
.then(checkCalcData(aggPie2CD))
.then(reactTo(aggregatedPie(1)))
.then(checkCalcData(aggPie1CD))
.catch(failTest)
.then(done);
});
it('can change scatter aggregations', function(done) {
Plotly.newPlot(gd, aggregatedScatter(1))
.then(checkCalcData(aggScatter1CD))
.then(reactTo(aggregatedScatter(2)))
.then(checkCalcData(aggScatter2CD))
.then(reactTo(aggregatedScatter(1)))
.then(checkCalcData(aggScatter1CD))
.catch(failTest)
.then(done);
});
it('can change parcoords aggregations', function(done) {
Plotly.newPlot(gd, aggregatedParcoords(0))
.then(checkValues(aggParcoords0Vals))
.then(reactTo(aggregatedParcoords(1)))
.then(checkValues(aggParcoords1Vals))
.then(reactTo(aggregatedParcoords(2)))
.then(checkValues(aggParcoords2Vals))
.then(reactTo(aggregatedParcoords(0)))
.then(checkValues(aggParcoords0Vals))
.catch(failTest)
.then(done);
});
it('can change type with aggregations', function(done) {
Plotly.newPlot(gd, aggregatedScatter(1))
.then(checkCalcData(aggScatter1CD))
.then(reactTo(aggregatedPie(1)))
.then(checkCalcData(aggPie1CD))
.then(reactTo(aggregatedParcoords(1)))
.then(checkValues(aggParcoords1Vals))
.then(reactTo(aggregatedScatter(1)))
.then(checkCalcData(aggScatter1CD))
.then(reactTo(aggregatedParcoords(2)))
.then(checkValues(aggParcoords2Vals))
.then(reactTo(aggregatedPie(2)))
.then(checkCalcData(aggPie2CD))
.then(reactTo(aggregatedScatter(2)))
.then(checkCalcData(aggScatter2CD))
.then(reactTo(aggregatedParcoords(0)))
.then(checkValues(aggParcoords0Vals))
.catch(failTest)
.then(done);
});
it('can change frames without redrawing', function(done) {
var data = [{y: [1, 2, 3]}];
var layout = {};
var frames = [{name: 'frame1'}];
Plotly.newPlot(gd, {data: data, layout: layout, frames: frames})
.then(countPlots)
.then(function() {
var frameData = gd._transitionData._frames;
expect(frameData.length).toBe(1);
expect(frameData[0].name).toBe('frame1');
frames[0].name = 'frame2';
return Plotly.react(gd, {data: data, layout: layout, frames: frames});
})
.then(function() {
countCalls({});
var frameData = gd._transitionData._frames;
expect(frameData.length).toBe(1);
expect(frameData[0].name).toBe('frame2');
})
.catch(failTest)
.then(done);
});
// make sure we've included every trace type in this suite
var typesTested = {};
var itemType;
for(itemType in Registry.modules) { typesTested[itemType] = 0; }
for(itemType in Registry.transformsRegistry) { typesTested[itemType] = 0; }
// Not really being supported... This isn't part of the main bundle, and it's pretty broken,
// but it gets registered and used by a couple of the gl2d tests.
delete typesTested.contourgl;
function _runReactMock(mockSpec, done) {
var mock = mockSpec[1];
var initialJson;
function fullJson() {
var out = JSON.parse(Plotly.Plots.graphJson({
data: gd._fullData.map(function(trace) { return trace._fullInput; }),
layout: gd._fullLayout
}));
// TODO: does it matter that ax.tick0/dtick/range and zmin/zmax
// are often not regenerated without a calc step?
// in as far as editor and others rely on _full, I think the
// answer must be yes, but I'm not sure about within plotly.js
[
'xaxis', 'xaxis2', 'xaxis3', 'xaxis4', 'xaxis5',
'yaxis', 'yaxis2', 'yaxis3', 'yaxis4',
'zaxis'
].forEach(function(axName) {
var ax = out.layout[axName];
if(ax) {
delete ax.dtick;
delete ax.tick0;
// TODO this one I don't understand and can't reproduce
// in the dashboard but it's needed here?
delete ax.range;
}
if(out.layout.scene) {
ax = out.layout.scene[axName];
if(ax) {
delete ax.dtick;
delete ax.tick0;
// TODO: this is the only one now that uses '_input_' + key
// as a hack to tell Plotly.react to ignore changes.
// Can we kill this?
delete ax.range;
}
}
});
out.data.forEach(function(trace) {
if(trace.type === 'contourcarpet') {
delete trace.zmin;
delete trace.zmax;
}
});
return out;
}
// Make sure we define `_length` in every trace *in supplyDefaults*.
// This is only relevant for traces that *have* a 1D concept of length,
// and in addition to simplifying calc/plot logic later on, ths serves
// as a signal to transforms about how they should operate. For traces
// that do NOT have a 1D length, `_length` should be `null`.
var mockGD = Lib.extendDeep({}, mock);
supplyAllDefaults(mockGD);
expect(mockGD._fullData.length).not.toBeLessThan((mock.data || []).length, mockSpec[0]);
mockGD._fullData.forEach(function(trace, i) {
var len = trace._length;
if(trace.visible !== false && len !== null) {
expect(typeof len).toBe('number', mockSpec[0] + ' trace ' + i + ': type=' + trace.type);
}
typesTested[trace.type]++;
if(trace.transforms) {
trace.transforms.forEach(function(transform) {
typesTested[transform.type]++;
});
}
});
Plotly.newPlot(gd, mock)
.then(countPlots)
.then(function() {
initialJson = fullJson();
return Plotly.react(gd, mock);
})
.then(function() {
expect(fullJson()).toEqual(initialJson);
countCalls({});
})
.catch(failTest)
.then(done);
}
mockLists.svg.forEach(function(mockSpec) {
it('can redraw "' + mockSpec[0] + '" with no changes as a noop (svg mocks)', function(done) {
_runReactMock(mockSpec, done);
});
});
mockLists.gl.forEach(function(mockSpec) {
it('can redraw "' + mockSpec[0] + '" with no changes as a noop (gl mocks)', function(done) {
_runReactMock(mockSpec, done);
});
});
mockLists.mapbox.forEach(function(mockSpec) {
it('@noCI can redraw "' + mockSpec[0] + '" with no changes as a noop (mapbpox mocks)', function(done) {
Plotly.setPlotConfig({
mapboxAccessToken: MAPBOX_ACCESS_TOKEN
});
_runReactMock(mockSpec, done);
});
});
// since CI breaks up gl/svg types, and drops scattermapbox, this test won't work there
// but I should hope that if someone is doing something as major as adding a new type,
// they'll run the full test suite locally!
it('@noCI tested every trace & transform type at least once', function() {
for(var itemType in typesTested) {
expect(typesTested[itemType]).toBeGreaterThan(0, itemType + ' was not tested');
}
});
});
describe('resizing with Plotly.relayout and Plotly.react', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('recalculates autoranges when height/width change', function(done) {
Plotly.newPlot(gd,
[{y: [1, 2], marker: {size: 100}}],
{width: 400, height: 400, margin: {l: 100, r: 100, t: 100, b: 100}}
)
.then(function() {
expect(gd.layout.xaxis.range).toBeCloseToArray([-1.31818, 2.31818], 3);
expect(gd.layout.yaxis.range).toBeCloseToArray([-0.31818, 3.31818], 3);
return Plotly.relayout(gd, {height: 800, width: 800});
})
.then(function() {
expect(gd.layout.xaxis.range).toBeCloseToArray([-0.22289, 1.22289], 3);
expect(gd.layout.yaxis.range).toBeCloseToArray([0.77711, 2.22289], 3);
gd.layout.width = 500;
gd.layout.height = 500;
return Plotly.react(gd, gd.data, gd.layout);
})
.then(function() {
expect(gd.layout.xaxis.range).toBeCloseToArray([-0.53448, 1.53448], 3);
expect(gd.layout.yaxis.range).toBeCloseToArray([0.46552, 2.53448], 3);
})
.catch(failTest)
.then(done);
});
});
describe('Plotly.react and uirevision attributes', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
function checkCloseIfArray(val1, val2, msg) {
if(Array.isArray(val1) && Array.isArray(val2)) {
if(Array.isArray(val1[0]) && Array.isArray(val2[0])) {
expect(val1).toBeCloseTo2DArray(val2, 2, msg);
}
else {
expect(val1).toBeCloseToArray(val2, 2, msg);
}
}
else {
expect(val1).toBe(val2, msg);
}
}
function checkState(dataKeys, layoutKeys, msg) {
var np = Lib.nestedProperty;
return function() {
dataKeys.forEach(function(traceKeys, i) {
var trace = gd.data[i];
var fullTrace = gd._fullData.filter(function(ft) {
return ft._fullInput.index === i;
})[0]._fullInput;
for(var key in traceKeys) {
var val = traceKeys[key];
var valIn = Array.isArray(val) ? val[0] : val;
var valOut = Array.isArray(val) ? val[val.length - 1] : val;
checkCloseIfArray(np(trace, key).get(), valIn, msg + ': data[' + i + '].' + key);
checkCloseIfArray(np(fullTrace, key).get(), valOut, msg + ': _fullData[' + i + '].' + key);
checkCloseIfArray(np(trace, key).get(), valIn, msg + ': data[' + i + '].' + key);
checkCloseIfArray(np(fullTrace, key).get(), valOut, msg + ': _fullData[' + i + '].' + key);
}
});
for(var key in (layoutKeys || {})) {
var val = layoutKeys[key];
var valIn = Array.isArray(val) ? val[0] : val;
var valOut = Array.isArray(val) ? val[val.length - 1] : val;
checkCloseIfArray(np(gd.layout, key).get(), valIn, msg + ': layout.' + key);
checkCloseIfArray(np(gd._fullLayout, key).get(), valOut, msg + ': _fullLayout.' + key);
}
};
}
function _react(fig) {
return function() {
return Plotly.react(gd, fig);
};
}
it('preserves zoom and trace visibility state until uirevision changes', function(done) {
var checkNoEdits = checkState([{