-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
animate_test.js
1072 lines (919 loc) · 39.9 KB
/
animate_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 Lib = require('../../../src/lib');
var Registry = require('../../../src/registry');
var Plots = require('../../../src/plots/plots');
var d3Select = require('../../strict-d3').select;
var d3SelectAll = require('../../strict-d3').selectAll;
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var failTest = require('../assets/fail_test');
var delay = require('../assets/delay');
var mock = require('../../image/mocks/animation');
describe('Plots.supplyAnimationDefaults', function() {
'use strict';
it('supplies transition defaults', function() {
expect(Plots.supplyAnimationDefaults({})).toEqual({
fromcurrent: false,
mode: 'afterall',
direction: 'forward',
transition: {
duration: 500,
easing: 'cubic-in-out'
},
frame: {
duration: 500,
redraw: true
}
});
});
it('uses provided values', function() {
expect(Plots.supplyAnimationDefaults({
mode: 'next',
fromcurrent: true,
direction: 'reverse',
transition: {
duration: 600,
easing: 'elastic-in-out'
},
frame: {
duration: 700,
redraw: false
}
})).toEqual({
mode: 'next',
fromcurrent: true,
direction: 'reverse',
transition: {
duration: 600,
easing: 'elastic-in-out'
},
frame: {
duration: 700,
redraw: false
}
});
});
});
describe('Test animate API', function() {
'use strict';
var gd, mockCopy;
function verifyQueueEmpty(gd) {
expect(gd._transitionData._frameQueue.length).toEqual(0);
}
function verifyFrameTransitionOrder(gd, expectedFrames) {
var calls = Plots.transition.calls;
var c1 = calls.count();
var c2 = expectedFrames.length;
expect(c1).toEqual(c2);
// Prevent lots of ugly logging when it's already failed:
if(c1 !== c2) return;
for(var i = 0; i < calls.count(); i++) {
expect(calls.argsFor(i)[1]).toEqual(
gd._transitionData._frameHash[expectedFrames[i]].data
);
}
}
beforeEach(function(done) {
gd = createGraphDiv();
mockCopy = Lib.extendDeep({}, mock);
// ------------------------------------------------------------
// NB: TRANSITION IS FAKED
//
// This means that you should not expect `.animate` to actually
// modify the plot in any way in the tests below. For tests
// involving non-faked transitions, see the bottom of this file.
// ------------------------------------------------------------
spyOn(Plots, 'transition').and.callFake(function() {
// Transition's fake behavior is just to delay by the duration
// and resolve:
return Promise.resolve().then(delay(arguments[5].duration));
});
Plotly.newPlot(gd, mockCopy.data, mockCopy.layout).then(function() {
return Plotly.addFrames(gd, mockCopy.frames);
}).then(done);
});
afterEach(function() {
// *must* purge between tests otherwise dangling async events might not get cleaned up properly:
Plotly.purge(gd);
destroyGraphDiv();
});
it('throws an error on addFrames if gd is not a graph', function() {
var gd2 = document.createElement('div');
gd2.id = 'invalidgd';
document.body.appendChild(gd2);
expect(function() {
Plotly.addFrames(gd2, [{}]);
}).toThrow(new Error('This element is not a Plotly plot: [object HTMLDivElement]. It\'s likely that you\'ve failed to create a plot before adding frames. For more details, see https://plotly.com/javascript/animations/'));
document.body.removeChild(gd);
});
it('throws an error on animate if gd is not a graph', function() {
var gd2 = document.createElement('div');
gd2.id = 'invalidgd';
document.body.appendChild(gd2);
expect(function() {
Plotly.animate(gd2, {data: [{}]});
}).toThrow(new Error('This element is not a Plotly plot: [object HTMLDivElement]. It\'s likely that you\'ve failed to create a plot before animating it. For more details, see https://plotly.com/javascript/animations/'));
document.body.removeChild(gd);
});
runTests(0);
runTests(30);
function runTests(duration) {
describe('With duration = ' + duration, function() {
var animOpts;
beforeEach(function() {
animOpts = {frame: {duration: duration}, transition: {duration: duration * 0.5}};
});
it('animates to a frame', function(done) {
Plotly.animate(gd, ['frame0'], {transition: {duration: 1.2345}, frame: {duration: 1.5678}}).then(function() {
expect(Plots.transition).toHaveBeenCalled();
var args = Plots.transition.calls.mostRecent().args;
// was called with gd, data, layout, traceIndices, transitionConfig:
expect(args.length).toEqual(6);
// data has two traces:
expect(args[1].length).toEqual(2);
// Verify frame config has been passed:
expect(args[4].duration).toEqual(1.5678);
// Verify transition config has been passed:
expect(args[5].duration).toEqual(1.2345);
// layout
expect(args[2]).toEqual({
xaxis: {range: [0, 2]},
yaxis: {range: [0, 10]}
});
// traces are [0, 1]:
expect(args[3]).toEqual([0, 1]);
}).then(done, done.fail);
});
it('rejects if a frame is not found', function(done) {
Plotly.animate(gd, ['foobar'], animOpts).then(done, done);
});
it('treats objects as frames', function(done) {
var frame = {data: [{x: [1, 2, 3]}]};
Plotly.animate(gd, frame, animOpts).then(function() {
expect(Plots.transition.calls.count()).toEqual(1);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('treats a list of objects as frames', function(done) {
var frame1 = {data: [{x: [1, 2, 3]}], traces: [0], layout: {foo: 'bar'}};
var frame2 = {data: [{x: [3, 4, 5]}], traces: [1], layout: {foo: 'baz'}};
Plotly.animate(gd, [frame1, frame2], animOpts).then(function() {
expect(Plots.transition.calls.argsFor(0)[1]).toEqual(frame1.data);
expect(Plots.transition.calls.argsFor(0)[2]).toEqual(frame1.layout);
expect(Plots.transition.calls.argsFor(0)[3]).toEqual(frame1.traces);
expect(Plots.transition.calls.argsFor(1)[1]).toEqual(frame2.data);
expect(Plots.transition.calls.argsFor(1)[2]).toEqual(frame2.layout);
expect(Plots.transition.calls.argsFor(1)[3]).toEqual(frame2.traces);
expect(Plots.transition.calls.count()).toEqual(2);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates all frames if list is null', function(done) {
Plotly.animate(gd, null, animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['base', 'frame0', 'frame1', 'frame2', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates all frames if list is undefined', function(done) {
Plotly.animate(gd, undefined, animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['base', 'frame0', 'frame1', 'frame2', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates to a single frame', function(done) {
Plotly.animate(gd, ['frame0'], animOpts).then(function() {
expect(Plots.transition.calls.count()).toEqual(1);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates to an empty list', function(done) {
Plotly.animate(gd, [], animOpts).then(function() {
expect(Plots.transition.calls.count()).toEqual(0);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates to a list of frames', function(done) {
Plotly.animate(gd, ['frame0', 'frame1'], animOpts).then(function() {
expect(Plots.transition.calls.count()).toEqual(2);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates frames by group', function(done) {
Plotly.animate(gd, 'even-frames', animOpts).then(function() {
expect(Plots.transition.calls.count()).toEqual(2);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates frames in the correct order', function(done) {
Plotly.animate(gd, ['frame0', 'frame2', 'frame1', 'frame3'], animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['frame0', 'frame2', 'frame1', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('accepts a single animationOpts', function(done) {
Plotly.animate(gd, ['frame0', 'frame1'], {transition: {duration: 1.12345}}).then(function() {
var calls = Plots.transition.calls;
expect(calls.argsFor(0)[5].duration).toEqual(1.12345);
expect(calls.argsFor(1)[5].duration).toEqual(1.12345);
}).then(done, done.fail);
});
it('accepts an array of animationOpts', function(done) {
Plotly.animate(gd, ['frame0', 'frame1'], {
transition: [{duration: 1.123}, {duration: 1.456}],
frame: [{duration: 8.7654}, {duration: 5.4321}]
}).then(function() {
var calls = Plots.transition.calls;
expect(calls.argsFor(0)[4].duration).toEqual(8.7654);
expect(calls.argsFor(1)[4].duration).toEqual(5.4321);
expect(calls.argsFor(0)[5].duration).toEqual(1.123);
expect(calls.argsFor(1)[5].duration).toEqual(1.456);
}).then(done, done.fail);
});
it('falls back to animationOpts[0] if not enough supplied in array', function(done) {
Plotly.animate(gd, ['frame0', 'frame1'], {
transition: [{duration: 1.123}],
frame: [{duration: 2.345}]
}).then(function() {
var calls = Plots.transition.calls;
expect(calls.argsFor(0)[4].duration).toEqual(2.345);
expect(calls.argsFor(1)[4].duration).toEqual(2.345);
expect(calls.argsFor(0)[5].duration).toEqual(1.123);
expect(calls.argsFor(1)[5].duration).toEqual(1.123);
}).then(done, done.fail);
});
it('chains animations as promises', function(done) {
Plotly.animate(gd, ['frame0', 'frame1'], animOpts).then(function() {
return Plotly.animate(gd, ['frame2', 'frame3'], animOpts);
}).then(function() {
verifyFrameTransitionOrder(gd, ['frame0', 'frame1', 'frame2', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('emits plotly_animated before the promise is resolved', function(done) {
var animated = false;
gd.on('plotly_animated', function() {
animated = true;
});
Plotly.animate(gd, ['frame0'], animOpts).then(function() {
expect(animated).toBe(true);
}).then(done, done.fail);
});
it('emits plotly_animated as each animation in a sequence completes', function(done) {
var completed = 0;
var test1 = 0;
var test2 = 0;
gd.on('plotly_animated', function() {
completed++;
if(completed === 1) {
// Verify that after the first plotly_animated, precisely frame0 and frame1
// have been transitioned to:
verifyFrameTransitionOrder(gd, ['frame0', 'frame1']);
test1++;
} else {
// Verify that after the second plotly_animated, precisely all frames
// have been transitioned to:
verifyFrameTransitionOrder(gd, ['frame0', 'frame1', 'frame2', 'frame3']);
test2++;
}
});
Plotly.animate(gd, ['frame0', 'frame1'], animOpts).then(function() {
return Plotly.animate(gd, ['frame2', 'frame3'], animOpts);
}).then(function() {
expect(test1).toBe(1);
expect(test2).toBe(1);
}).then(done, done.fail);
});
it('resolves at the end of each animation sequence', function(done) {
Plotly.animate(gd, 'even-frames', animOpts).then(function() {
return Plotly.animate(gd, ['frame0', 'frame2', 'frame1', 'frame3'], animOpts);
}).then(function() {
verifyFrameTransitionOrder(gd, ['frame0', 'frame2', 'frame0', 'frame2', 'frame1', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
});
}
describe('Animation direction', function() {
var animOpts;
beforeEach(function() {
animOpts = {
frame: {duration: 0},
transition: {duration: 0}
};
});
it('animates frames by name in reverse', function(done) {
animOpts.direction = 'reverse';
Plotly.animate(gd, ['frame0', 'frame2', 'frame1', 'frame3'], animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['frame3', 'frame1', 'frame2', 'frame0']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates a group in reverse', function(done) {
animOpts.direction = 'reverse';
Plotly.animate(gd, 'even-frames', animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['frame2', 'frame0']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
});
describe('Animation fromcurrent', function() {
var animOpts;
beforeEach(function() {
animOpts = {
frame: {duration: 0},
transition: {duration: 0},
fromcurrent: true
};
});
it('animates starting at the current frame', function(done) {
Plotly.animate(gd, ['frame1'], animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['frame1']);
verifyQueueEmpty(gd);
return Plotly.animate(gd, null, animOpts);
}).then(function() {
verifyFrameTransitionOrder(gd, ['frame1', 'frame2', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('plays from the start when current frame = last frame', function(done) {
Plotly.animate(gd, null, animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['base', 'frame0', 'frame1', 'frame2', 'frame3']);
verifyQueueEmpty(gd);
return Plotly.animate(gd, null, animOpts);
}).then(function() {
verifyFrameTransitionOrder(gd, [
'base', 'frame0', 'frame1', 'frame2', 'frame3',
'base', 'frame0', 'frame1', 'frame2', 'frame3'
]);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates in reverse starting at the current frame', function(done) {
animOpts.direction = 'reverse';
Plotly.animate(gd, ['frame1'], animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['frame1']);
verifyQueueEmpty(gd);
return Plotly.animate(gd, null, animOpts);
}).then(function() {
verifyFrameTransitionOrder(gd, ['frame1', 'frame0', 'base']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('plays in reverse from the end when current frame = first frame', function(done) {
animOpts.direction = 'reverse';
Plotly.animate(gd, ['base'], animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['base']);
verifyQueueEmpty(gd);
return Plotly.animate(gd, null, animOpts);
}).then(function() {
verifyFrameTransitionOrder(gd, [
'base', 'frame3', 'frame2', 'frame1', 'frame0', 'base'
]);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
});
// The tests above use promises to ensure ordering, but the tests below this call Plotly.animate
// without chaining promises which would result in race conditions. This is not invalid behavior,
// but it doesn't ensure proper ordering and completion, so these must be performed with finite
// duration. Stricly speaking, these tests *do* involve race conditions, but the finite duration
// prevents that from causing problems.
describe('Calling Plotly.animate synchronously in series', function() {
var animOpts;
beforeEach(function() {
animOpts = {frame: {duration: 30}};
});
it('emits plotly_animationinterrupted when an animation is interrupted', function(done) {
var interrupted = false;
gd.on('plotly_animationinterrupted', function() {
interrupted = true;
});
Plotly.animate(gd, ['frame0', 'frame1'], animOpts);
Plotly.animate(gd, ['frame2'], Lib.extendFlat(animOpts, {mode: 'immediate'})).then(function() {
expect(interrupted).toBe(true);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('queues successive animations', function(done) {
var starts = 0;
var ends = 0;
gd.on('plotly_animating', function() {
starts++;
}).on('plotly_animated', function() {
ends++;
expect(Plots.transition.calls.count()).toEqual(4);
expect(starts).toEqual(1);
});
Plotly.animate(gd, 'even-frames', {transition: {duration: 16}});
Plotly.animate(gd, 'odd-frames', {transition: {duration: 16}}).then(delay(10)).then(function() {
expect(ends).toEqual(1);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('an empty list with immediate dumps previous frames', function(done) {
Plotly.animate(gd, ['frame0', 'frame1'], {frame: {duration: 50}});
Plotly.animate(gd, [], {mode: 'immediate'}).then(function() {
expect(Plots.transition.calls.count()).toEqual(1);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates groups in the correct order', function(done) {
Plotly.animate(gd, 'even-frames', animOpts);
Plotly.animate(gd, 'odd-frames', animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['frame0', 'frame2', 'frame1', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('drops queued frames when immediate = true', function(done) {
Plotly.animate(gd, 'even-frames', animOpts);
Plotly.animate(gd, 'odd-frames', Lib.extendFlat(animOpts, {mode: 'immediate'})).then(function() {
verifyFrameTransitionOrder(gd, ['frame0', 'frame1', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('animates frames and groups in sequence', function(done) {
Plotly.animate(gd, 'even-frames', animOpts);
Plotly.animate(gd, ['frame0', 'frame2', 'frame1', 'frame3'], animOpts).then(function() {
verifyFrameTransitionOrder(gd, ['frame0', 'frame2', 'frame0', 'frame2', 'frame1', 'frame3']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
it('rejects when an animation is interrupted', function(done) {
var interrupted = false;
Plotly.animate(gd, ['frame0', 'frame1'], animOpts).then(failTest, function() {
interrupted = true;
});
Plotly.animate(gd, ['frame2'], Lib.extendFlat(animOpts, {mode: 'immediate'})).then(function() {
expect(interrupted).toBe(true);
verifyFrameTransitionOrder(gd, ['frame0', 'frame2']);
verifyQueueEmpty(gd);
}).then(done, done.fail);
});
});
describe('frame events', function() {
it('emits an event when a frame is transitioned to', function(done) {
var frames = [];
gd.on('plotly_animatingframe', function(data) {
frames.push(data.name);
expect(data.frame).not.toBe(undefined);
expect(data.animation.frame).not.toBe(undefined);
expect(data.animation.transition).not.toBe(undefined);
});
Plotly.animate(gd, ['frame0', 'frame1', {name: 'test'}, {data: []}], {
transition: {duration: 1},
frame: {duration: 1}
}).then(function() {
expect(frames).toEqual(['frame0', 'frame1', null, null]);
}).then(done, done.fail);
});
});
describe('frame vs. transition timing', function() {
it('limits the transition duration to <= frame duration', function(done) {
Plotly.animate(gd, ['frame0'], {
transition: {duration: 100000},
frame: {duration: 50}
}).then(function() {
// Frame timing:
expect(Plots.transition.calls.argsFor(0)[4].duration).toEqual(50);
// Transition timing:
expect(Plots.transition.calls.argsFor(0)[5].duration).toEqual(50);
}).then(done, done.fail);
});
it('limits the transition duration to <= frame duration (matching per-config)', function(done) {
Plotly.animate(gd, ['frame0', 'frame1'], {
transition: [{duration: 100000}, {duration: 123456}],
frame: [{duration: 50}, {duration: 40}]
}).then(function() {
// Frame timing:
expect(Plots.transition.calls.argsFor(0)[4].duration).toEqual(50);
expect(Plots.transition.calls.argsFor(1)[4].duration).toEqual(40);
// Transition timing:
expect(Plots.transition.calls.argsFor(0)[5].duration).toEqual(50);
expect(Plots.transition.calls.argsFor(1)[5].duration).toEqual(40);
}).then(done, done.fail);
});
});
});
describe('Animate API details', function() {
'use strict';
var gd;
var dur = 30;
var mockCopy;
beforeEach(function(done) {
gd = createGraphDiv();
mockCopy = Lib.extendDeep({}, mock);
Plotly.newPlot(gd, mockCopy.data, mockCopy.layout).then(done);
});
afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});
it('redraws after a layout animation', function(done) {
var redraws = 0;
gd.on('plotly_redraw', function() {redraws++;});
Plotly.animate(gd,
{layout: {'xaxis.range': [0, 1]}},
{frame: {redraw: true, duration: dur}, transition: {duration: dur}}
).then(function() {
expect(redraws).toBe(1);
}).then(done, done.fail);
});
it('forces a relayout after layout animations', function(done) {
var relayouts = 0;
var restyles = 0;
var redraws = 0;
gd.on('plotly_relayout', function() {relayouts++;});
gd.on('plotly_restyle', function() {restyles++;});
gd.on('plotly_redraw', function() {redraws++;});
Plotly.animate(gd,
{layout: {'xaxis.range': [0, 1]}},
{frame: {redraw: false, duration: dur}, transition: {duration: dur}}
).then(function() {
expect(relayouts).toBe(1);
expect(restyles).toBe(0);
expect(redraws).toBe(0);
}).then(done, done.fail);
});
it('triggers plotly_animated after a single layout animation', function(done) {
var animateds = 0;
gd.on('plotly_animated', function() {animateds++;});
Plotly.animate(gd, [
{layout: {'xaxis.range': [0, 1]}},
], {frame: {redraw: false, duration: dur}, transition: {duration: dur}}
).then(function() {
// Wait a bit just to be sure:
setTimeout(function() {
expect(animateds).toBe(1);
done();
}, dur);
});
});
it('triggers plotly_animated after a multi-step layout animation', function(done) {
var animateds = 0;
gd.on('plotly_animated', function() {animateds++;});
Plotly.animate(gd, [
{layout: {'xaxis.range': [0, 1]}},
{layout: {'xaxis.range': [2, 4]}},
], {frame: {redraw: false, duration: dur}, transition: {duration: dur}}
).then(function() {
// Wait a bit just to be sure:
setTimeout(function() {
expect(animateds).toBe(1);
done();
}, dur);
});
});
it('does not fail if strings are not used', function(done) {
Plotly.addFrames(gd, [{name: 8, data: [{x: [8, 7, 6]}]}]).then(function() {
// Verify it was added as a string name:
expect(gd._transitionData._frameHash['8']).not.toBeUndefined();
// Transition using a number:
return Plotly.animate(gd, [8], {transition: {duration: 0}, frame: {duration: 0}});
}).then(function() {
// Confirm the result:
expect(gd.data[0].x).toEqual([8, 7, 6]);
}).then(done, done.fail);
});
it('ignores null and undefined frames', function(done) {
var cnt = 0;
gd.on('plotly_animatingframe', function() {cnt++;});
Plotly.addFrames(gd, mockCopy.frames).then(function() {
return Plotly.animate(gd, ['frame0', null, undefined], {transition: {duration: 0}, frame: {duration: 0}});
}).then(function() {
// Check only one animating was fired:
expect(cnt).toEqual(1);
// Check unused frames did not affect the current frame:
expect(gd._fullLayout._currentFrame).toEqual('frame0');
}).then(done, done.fail);
});
it('null frames should not break everything', function(done) {
gd._transitionData._frames.push(null);
Plotly.animate(gd, null, {
frame: {duration: 0},
transition: {duration: 0}
}).then(done, done.fail);
});
});
describe('Animate expandObjectPaths do not pollute prototype', function() {
'use strict';
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});
it('should not pollute prototype - layout object', function(done) {
Plotly.newPlot(gd, {
data: [{y: [1, 3, 2]}]
}).then(function() {
return Plotly.animate(gd, {
transition: {duration: 10},
data: [{y: [2, 3, 1]}],
traces: [0],
layout: {'__proto__.polluted': true, 'x.__proto__.polluted': true}
});
}).then(delay(100)).then(function() {
var a = {};
expect(a.polluted).toBeUndefined();
}).then(done, done.fail);
});
it('should not pollute prototype - data object', function(done) {
Plotly.newPlot(gd, {
data: [{y: [1, 3, 2]}]
}).then(function() {
return Plotly.animate(gd, {
transition: {duration: 10},
data: [{y: [2, 3, 1], '__proto__.polluted': true}],
traces: [0]
});
}).then(delay(100)).then(function() {
var a = {};
expect(a.polluted).toBeUndefined();
}).then(done, done.fail);
});
});
describe('Animating multiple axes', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});
it('updates ranges of secondary axes', function(done) {
Plotly.newPlot(gd, [
{y: [1, 2, 3]},
{y: [1, 2, 3], yaxis: 'y2'}
], {
yaxis: {range: [0, 5]},
yaxis2: {range: [-1, 4]}
})
.then(function() {
expect(gd._fullLayout.yaxis.range).toEqual([0, 5]);
expect(gd._fullLayout.yaxis2.range).toEqual([-1, 4]);
return Plotly.animate(gd, [
{layout: {'yaxis.range': [2, 3]}},
{layout: {'yaxis2.range': [1, 2]}}
], {
// TODO: if the durations are the same, yaxis.range gets some
// random endpoint, often close to what it's supposed to be but
// sometimes very far away.
frame: {redraw: false, duration: 60},
transition: {duration: 30}
});
})
.then(function() {
expect(gd._fullLayout.yaxis.range).toEqual([2, 3]);
expect(gd._fullLayout.yaxis2.range).toEqual([1, 2]);
})
.then(done, done.fail);
});
it('@flaky updates ranges of secondary axes (date + category case)', function(done) {
Plotly.newPlot(gd, [
{x: ['2018-01-01', '2019-01-01', '2020-01-01'], y: [1, 2, 3]},
{x: ['a', 'b', 'c'], y: [1, 2, 3], xaxis: 'x2', yaxis: 'y2'}
], {
grid: {rows: 1, columns: 2, pattern: 'independent'},
xaxis: {range: ['2018-01-01', '2020-01-01']},
yaxis: {range: [0, 4]},
xaxis2: {range: [0, 2]},
yaxis2: {range: [0, 4]}
})
.then(function() {
expect(gd._fullLayout.xaxis.range).toEqual(['2018-01-01', '2020-01-01']);
expect(gd._fullLayout.xaxis2.range).toEqual([0, 2]);
var promise = Plotly.animate(gd, [{
layout: {
'xaxis.range': ['2018-06-01', '2019-06-01'],
'xaxis2.range': [0.5, 1.5]
}
}], {
frame: {redraw: false, duration: 60},
transition: {duration: 30}
});
setTimeout(function() {
var fullLayout = gd._fullLayout;
var xa = fullLayout.xaxis;
var xr = xa.range.slice();
expect(xa.r2l(xr[0])).toBeGreaterThan(xa.r2l('2018-01-01'));
expect(xa.r2l(xr[1])).toBeLessThan(xa.r2l('2020-01-01'));
var xa2 = fullLayout.xaxis2;
var xr2 = xa2.range.slice();
expect(xr2[0]).toBeGreaterThan(0);
expect(xr2[1]).toBeLessThan(2);
}, 15);
return promise;
})
.then(function() {
expect(gd._fullLayout.xaxis.range).toEqual(['2018-06-01', '2019-06-01']);
expect(gd._fullLayout.xaxis2.range).toEqual([0.5, 1.5]);
})
.then(done, done.fail);
});
it('should not leak axis update from subplot to subplot', function(done) {
function _animate(frameLayout) {
return function() {
return Plotly.animate(gd, {layout: frameLayout}, {
frame: {redraw: false, duration: 10},
transition: {duration: 10}
});
};
}
function _assert(msg, exp) {
return function() {
var fullLayout = gd._fullLayout;
for(var k in exp) {
expect(fullLayout[k].range).toBeCloseToArray(exp[k], 2, msg + '| ' + k);
}
};
}
Plotly.newPlot(gd, [{
x: [0.1, 0.2, 0.3],
y: [0.4, 0.5, 0.6],
}, {
x: [0.2, 0.3, 0.4],
y: [0.5, 0.6, 0.7],
xaxis: 'x2',
yaxis: 'y2',
}, {
x: [0.3, 0.5, 0.7],
y: [0.7, 0.2, 0.2],
xaxis: 'x3',
yaxis: 'y3',
}], {
grid: {rows: 1, columns: 3, pattern: 'independent'},
showlegend: false
})
.then(_assert('base', {
xaxis: [0.0825, 0.3174], xaxis2: [0.1825, 0.417], xaxis3: [0.265, 0.7349],
yaxis: [0.385, 0.614], yaxis2: [0.485, 0.714], yaxis3: [0.163, 0.7366]
}))
.then(_animate({
xaxis: {range: [-10, 10]},
yaxis: {range: [-10, 10]}
}))
.then(_assert('after xy range animate', {
xaxis: [-10, 10], xaxis2: [0.1825, 0.417], xaxis3: [0.265, 0.7349],
yaxis: [-10, 10], yaxis2: [0.485, 0.714], yaxis3: [0.163, 0.7366]
}))
.then(_animate({
xaxis2: {range: [-20, 20]},
yaxis2: {range: [-20, 20]}
}))
.then(_assert('after x2y2 range animate', {
xaxis: [-10, 10], xaxis2: [-20, 20], xaxis3: [0.265, 0.7349],
yaxis: [-10, 10], yaxis2: [-20, 20], yaxis3: [0.163, 0.7366]
}))
.then(_animate({
xaxis3: {range: [-30, 30]},
yaxis3: {range: [-30, 30]}
}))
.then(_assert('after x3y3 range animate', {
xaxis: [-10, 10], xaxis2: [-20, 20], xaxis3: [-30, 30],
yaxis: [-10, 10], yaxis2: [-20, 20], yaxis3: [-30, 30]
}))
.then(done, done.fail);
});
});
describe('non-animatable fallback', function() {
'use strict';
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});
it('falls back to a simple update for bar graphs', function(done) {
Plotly.newPlot(gd, [{
x: [1, 2, 3],
y: [4, 5, 6],
type: 'bar'
}]).then(function() {
expect(gd.data[0].y).toEqual([4, 5, 6]);
return Plotly.animate(gd, [{
data: [{y: [6, 4, 5]}]
}], {frame: {duration: 0}});
}).then(function() {
expect(gd.data[0].y).toEqual([6, 4, 5]);
}).then(done, done.fail);
});
});
describe('animating scatter traces', function() {
'use strict';
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});
it('animates trace opacity', function(done) {
var trace;
Plotly.newPlot(gd, [{
x: [1, 2, 3],
y: [4, 5, 6],
opacity: 1
}]).then(function() {
trace = d3SelectAll('g.scatter.trace');
// d3 style getter is disallowed by strict-d3
expect(trace.node().style.opacity).toEqual('1');
return Plotly.animate(gd, [{
data: [{opacity: 0.1}]
}], {transition: {duration: 0}, frame: {duration: 0, redraw: false}});
}).then(function() {
expect(trace.node().style.opacity).toEqual('0.1');
}).then(done, done.fail);
});
it('computes calcdata correctly when transforms are present', function(done) {
Plotly.newPlot(gd, {
data: [{
x: [1, 2, 3],
y: [1, 2, 3],
mode: 'markers',
transforms: [{
type: 'sort',
target: [1, 3, 2]
}]
}],
frames: [
{name: 'frame1', data: [{y: [1, 2, 3]}]},
{name: 'frame2', data: [{y: [3, 1, 2]}]}
]
}).then(function() {
return Plotly.animate(gd, ['frame2'], {frame: {duration: 200, redraw: false}});
}).then(function() {
expect(gd.calcdata[0][0].y).toEqual(3);
}).then(done, done.fail);
});
it('@flaky should animate axis ranges using the less number of steps', function(done) {
// sanity-check that scatter points and bars are still there