-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtransition_test.js
147 lines (124 loc) · 5.49 KB
/
transition_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
var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var Plots = Plotly.Plots;
var plotApiHelpers = require('@src/plot_api/helpers');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var fail = require('../assets/fail_test');
var delay = require('../assets/delay');
var mock = require('@mocks/animation');
function runTests(transitionDuration) {
describe('Plots.transition (duration = ' + transitionDuration + ')', function() {
'use strict';
var gd;
beforeEach(function(done) {
gd = createGraphDiv();
var mockCopy = Lib.extendDeep({}, mock);
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
});
afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});
it('resolves only once the transition has completed', function(done) {
var t1 = Date.now();
var traces = plotApiHelpers.coerceTraceIndices(gd, null);
Plots.transition(gd, null, {'xaxis.range': [0.2, 0.3]}, traces, {redraw: true}, {duration: transitionDuration, easing: 'cubic-in-out'})
.then(delay(20))
.then(function() {
expect(Date.now() - t1).toBeGreaterThan(transitionDuration);
}).catch(fail).then(done);
});
it('emits plotly_transitioning on transition start', function(done) {
var beginTransitionCnt = 0;
var traces = plotApiHelpers.coerceTraceIndices(gd, null);
gd.on('plotly_transitioning', function() { beginTransitionCnt++; });
Plots.transition(gd, null, {'xaxis.range': [0.2, 0.3]}, traces, {redraw: true}, {duration: transitionDuration, easing: 'cubic-in-out'})
.then(delay(20))
.then(function() {
expect(beginTransitionCnt).toBe(1);
}).catch(fail).then(done);
});
it('emits plotly_transitioned on transition end', function(done) {
var trEndCnt = 0;
var traces = plotApiHelpers.coerceTraceIndices(gd, null);
gd.on('plotly_transitioned', function() { trEndCnt++; });
Plots.transition(gd, null, {'xaxis.range': [0.2, 0.3]}, traces, {redraw: true}, {duration: transitionDuration, easing: 'cubic-in-out'})
.then(delay(20))
.then(function() {
expect(trEndCnt).toEqual(1);
}).catch(fail).then(done);
});
it('transitions a transform', function(done) {
Plotly.restyle(gd, {
'transforms[0]': {
enabled: true,
type: 'filter',
operation: '<',
filtersrc: 'x',
value: 10
}
}, [0]).then(function() {
expect(gd._fullData[0].transforms).toEqual([{
enabled: true,
type: 'filter',
operation: '<',
filtersrc: 'x',
value: 10
}]);
return Plots.transition(gd, [{
'transforms[0].operation': '>'
}], null, [0],
{redraw: true, duration: transitionDuration},
{duration: transitionDuration, easing: 'cubic-in-out'}
);
}).then(function() {
expect(gd._fullData[0].transforms).toEqual([{
enabled: true,
type: 'filter',
operation: '>',
filtersrc: 'x',
value: 10
}]);
}).catch(fail).then(done);
});
// This doesn't really test anything that the above tests don't cover, but it combines
// the behavior and attempts to ensure chaining and events happen in the correct order.
it('transitions may be chained', function(done) {
var currentlyRunning = 0;
var beginCnt = 0;
var endCnt = 0;
gd.on('plotly_transitioning', function() { currentlyRunning++; beginCnt++; });
gd.on('plotly_transitioned', function() { currentlyRunning--; endCnt++; });
function doTransition() {
var traces = plotApiHelpers.coerceTraceIndices(gd, null);
return Plots.transition(gd, [{x: [1, 2]}], null, traces, {redraw: true}, {duration: transitionDuration, easing: 'cubic-in-out'});
}
function checkNoneRunning() {
expect(currentlyRunning).toEqual(0);
}
doTransition()
.then(checkNoneRunning)
.then(doTransition)
.then(checkNoneRunning)
.then(doTransition)
.then(checkNoneRunning)
.then(delay(10))
.then(function() {
expect(beginCnt).toEqual(3);
expect(endCnt).toEqual(3);
})
.then(checkNoneRunning)
.catch(fail).then(done);
});
});
}
for(var i = 0; i < 2; i++) {
var duration = i * 20;
// Run the whole set of tests twice: once with zero duration and once with
// nonzero duration since the behavior should be identical, but there's a
// very real possibility of race conditions or other timing issues.
//
// And of course, remember to put the async loop in a closure:
runTests(duration);
}