-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathStaggeredMotion-test.js
348 lines (323 loc) · 9.19 KB
/
StaggeredMotion-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
/* eslint-disable class-methods-use-this */
import React from 'react';
import {spring} from '../src/react-motion';
import createMockRaf from './createMockRaf';
import TestUtils from 'react-dom/test-utils';
const injector = require('inject-loader!../src/StaggeredMotion');
describe('StaggeredMotion', () => {
let StaggeredMotion;
let mockRaf;
beforeEach(() => {
mockRaf = createMockRaf();
StaggeredMotion = injector({
raf: mockRaf.raf,
'performance-now': mockRaf.now,
}).default;
});
it('should allow returning null from children function', () => {
class App extends React.Component {
render() {
// shouldn't throw here
return (
<StaggeredMotion styles={() => [{a: 0}]}>
{() => null}
</StaggeredMotion>
);
}
}
TestUtils.renderIntoDocument(<App />);
});
it('should not throw on unmount', () => {
spyOn(console, 'error');
let kill = () => {};
class App extends React.Component {
constructor() {
super();
this.state = {
kill: false,
};
}
componentWillMount() {
kill = () => this.setState({kill: true});
}
render() {
return this.state.kill
? null
: <StaggeredMotion defaultStyles={[{a: 0}]} styles={() => [{a: spring(10)}]}>
{() => null}
</StaggeredMotion>;
}
}
TestUtils.renderIntoDocument(<App />);
mockRaf.step(2);
kill();
mockRaf.step(3);
expect(console.error).not.toHaveBeenCalled();
});
it('should allow a defaultStyles', () => {
let count = [];
class App extends React.Component {
render() {
return (
<StaggeredMotion
defaultStyles={[{a: 0}]}
styles={() => [{a: spring(10)}]}>
{([{a}]) => {
count.push(a);
return null;
}}
</StaggeredMotion>
);
}
}
TestUtils.renderIntoDocument(<App />);
expect(count).toEqual([0]);
mockRaf.step(4);
expect(count).toEqual([
0,
0.4722222222222222,
1.1897376543209877,
2.0123698988340193,
2.8557218143909084,
]);
});
it('should accept different spring configs', () => {
let count = [];
class App extends React.Component {
render() {
return (
<StaggeredMotion
defaultStyles={[{a: 0}]}
styles={() => [{a: spring(10, {stiffness: 100, damping: 50, precision: 16})}]}>
{([{a}]) => {
count.push(a);
return null;
}}
</StaggeredMotion>
);
}
}
TestUtils.renderIntoDocument(<App />);
mockRaf.step(99);
expect(count).toEqual([
0,
0.2777777777777778,
0.5941358024691358,
0.9081361454046639,
1.213021309632678,
1.5079182450697726,
1.7929588941684615,
2.0684390330691236,
10,
]);
});
it('should interpolate many values while staggering', () => {
let count = [];
class App extends React.Component {
render() {
return (
<StaggeredMotion
defaultStyles={[{a: 0, b: 10}, {a: 0, b: 10}]}
styles={prevStyles => {
return prevStyles.map((_, i) => i === 0
? {a: spring(10), b: spring(410)}
: {a: spring(prevStyles[i - 1].a), b: spring(prevStyles[i - 1].b)});
}}>
{([{a, b}, {a: a2, b: b2}]) => {
count.push([a, b, a2, b2]);
return null;
}}
</StaggeredMotion>
);
}
}
TestUtils.renderIntoDocument(<App />);
expect(count).toEqual([[0, 10, 0, 10]]);
mockRaf.step(4);
expect(count).toEqual([
[0, 10, 0, 10],
[0.4722222222222222, 28.888888888888886, 0, 10],
[1.1897376543209877, 57.589506172839506, 0.02229938271604938, 10.891975308641975],
[2.0123698988340193, 90.49479595336075, 0.09006472908093276, 13.602589163237312],
[2.8557218143909084, 124.22887257563633, 0.18039409126848038, 17.215763650739216],
]);
});
it('should work with nested Motions', () => {
let count = [];
class App extends React.Component {
render() {
return (
<StaggeredMotion defaultStyles={[{owner: 0}]} styles={() => [{owner: spring(10)}]}>
{([{owner}]) => {
count.push(owner);
return (
<StaggeredMotion defaultStyles={[{child: 10}]} styles={() => [{child: spring(400)}]}>
{([{child}]) => {
count.push(child);
return null;
}}
</StaggeredMotion>
);
}}
</StaggeredMotion>
);
}
}
TestUtils.renderIntoDocument(<App />);
expect(count).toEqual([0, 10]);
mockRaf.step();
expect(count).toEqual([
0,
10,
28.416666666666668, // child
0.4722222222222222, // owner
28.416666666666668, // child
]);
mockRaf.step(2);
expect(count).toEqual([
0,
10,
28.416666666666668,
0.4722222222222222,
28.416666666666668,
56.39976851851852, // child
1.1897376543209877, // owner
56.39976851851852, // child
88.48242605452674, // child
2.0123698988340193, // owner
88.48242605452674, // child
]);
});
// TODO: should animate one last time to reach true destination
// maybe shouldStopAnimation logic has a flaw
it('should reach destination value', () => {
let count = [];
class App extends React.Component {
render() {
return (
<StaggeredMotion
defaultStyles={[{a: 0, b: 10}, {a: 0, b: 10}]}
styles={prevStyles => {
return prevStyles.map((_, i) => i === 0
? {a: spring(10), b: spring(410)}
: {a: spring(prevStyles[i - 1].a), b: spring(prevStyles[i - 1].b)});
}}>
{([{a, b}, {a: a2, b: b2}]) => {
count.push([a, b, a2, b2]);
return null;
}}
</StaggeredMotion>
);
}
}
TestUtils.renderIntoDocument(<App />);
expect(count).toEqual([[0, 10, 0, 10]]);
mockRaf.step(111);
expect(count.slice(0, 5)).toEqual([
[0, 10, 0, 10],
[0.4722222222222222, 28.888888888888886, 0, 10],
[1.1897376543209877, 57.589506172839506, 0.02229938271604938, 10.891975308641975],
[2.0123698988340193, 90.49479595336075, 0.09006472908093276, 13.602589163237312],
[2.8557218143909084, 124.22887257563633, 0.18039409126848038, 17.215763650739216],
]);
expect(count.length).toBe(111);
expect(count[count.length - 1]).toEqual([10, 410, 10, 410]);
});
it('should support jumping to value', () => {
let count = [];
let setState = () => {};
class App extends React.Component {
constructor() {
super();
this.state = {
p: false,
};
}
componentWillMount() {
setState = this.setState.bind(this);
}
render() {
return (
<StaggeredMotion styles={() => [{a: this.state.p ? 400 : spring(0)}]}>
{([{a}]) => {
count.push(a);
return null;
}}
</StaggeredMotion>
);
}
}
TestUtils.renderIntoDocument(<App />);
expect(count).toEqual([0]);
setState({p: true});
expect(count).toEqual([
0,
0, // this new 0 comes from owner update, causing StaggeredMotion to re-render
]);
mockRaf.step(10);
// jumped to end, will only have two renders no matter how much we step
expect(count).toEqual([
0,
0,
400,
]);
setState({p: false});
mockRaf.step(3);
expect(count).toEqual([
0,
0,
400,
400, // redundant 0 comes from owner update again
381.1111111111111,
352.4104938271605,
319.5052040466392,
]);
});
it('should behave well when many owner updates come in-between rAFs', () => {
let count = [];
let setState = () => {};
class App extends React.Component {
constructor() {
super();
this.state = {
a: spring(0),
};
}
componentWillMount() {
setState = this.setState.bind(this);
}
render() {
return (
<StaggeredMotion styles={() => [this.state]}>
{([a]) => {
count.push(a);
return null;
}}
</StaggeredMotion>
);
}
}
TestUtils.renderIntoDocument(<App />);
expect(count).toEqual([{a: 0}]);
setState({a: 400});
setState({a: spring(100)});
mockRaf.step(2);
setState({a: spring(400)});
mockRaf.step(2);
expect(count).toEqual([
{a: 0},
{a: 0}, // this new 0 comes from owner update, causing StaggeredMotion to re-render
{a: 400},
{a: 385.8333333333333},
{a: 364.3078703703703},
{a: 364.3078703703703},
{a: 353.79556970164606},
{a: 350.02047519790233},
]);
mockRaf.step(999);
expect(count.length).toBe(85);
setState({a: spring(400)});
// make sure we're still updating children even if there's nothing to interp
expect(count.length).toBe(86);
});
});