Skip to content

Commit 45686c8

Browse files
Peter Arganyfacebook-github-bot
Peter Argany
authored andcommitted
Mock Animated for testing
Summary: Animated views can cause flakiness in snapshot tests. This mock replaces all provided Animated transforms with a blank animation. This could potentially break some tests which animate in elements and then verify their existence. I can deal with that fallout in follow up diffs. One option is making all animations take 0 seconds when testing. Reviewed By: cpojer Differential Revision: D13811035 fbshipit-source-id: cc6b13c7d6bad29b125d35ef759a269bb0372e67
1 parent f370933 commit 45686c8

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

Libraries/Animated/src/Animated.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
'use strict';
1212

13-
const AnimatedImplementation = require('AnimatedImplementation');
13+
import Platform from 'Platform';
14+
15+
const AnimatedImplementation = Platform.isTesting
16+
? require('AnimatedMock')
17+
: require('AnimatedImplementation');
1418

1519
module.exports = {
1620
get FlatList() {

Libraries/Animated/src/AnimatedImplementation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@ const event = function(argMapping: Array<?Mapping>, config?: EventConfig): any {
514514
* easy to build and maintain. `Animated` focuses on declarative relationships
515515
* between inputs and outputs, with configurable transforms in between, and
516516
* simple `start`/`stop` methods to control time-based animation execution.
517+
* If additional transforms are added, be sure to include them in
518+
* AnimatedMock.js as well.
517519
*
518520
* See http://facebook.github.io/react-native/docs/animated.html
519521
*/
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
'use strict';
11+
12+
const AnimatedImplementation = require('AnimatedImplementation');
13+
const AnimatedValueXY = require('./nodes/AnimatedValueXY');
14+
const AnimatedValue = require('./nodes/AnimatedValue');
15+
16+
import type {EndCallback} from './animations/Animation';
17+
import type {TimingAnimationConfig} from './animations/TimingAnimation';
18+
import type {DecayAnimationConfig} from './animations/DecayAnimation';
19+
import type {SpringAnimationConfig} from './animations/SpringAnimation';
20+
import type {Mapping, EventConfig} from './AnimatedEvent';
21+
22+
/**
23+
* Animations are a source of flakiness in snapshot testing. This mock replaces
24+
* animation functions from AnimatedImplementation with empty animations for
25+
* predictability in tests.
26+
*/
27+
type CompositeAnimation = {
28+
start: (callback?: ?EndCallback) => void,
29+
stop: () => void,
30+
reset: () => void,
31+
_startNativeLoop: (iterations?: number) => void,
32+
_isUsingNativeDriver: () => boolean,
33+
};
34+
35+
const emptyAnimation = {
36+
start: () => {},
37+
stop: () => {},
38+
reset: () => {},
39+
_startNativeLoop: () => {},
40+
_isUsingNativeDriver: () => {
41+
return false;
42+
},
43+
};
44+
45+
const spring = function(
46+
value: AnimatedValue | AnimatedValueXY,
47+
config: SpringAnimationConfig,
48+
): CompositeAnimation {
49+
return emptyAnimation;
50+
};
51+
52+
const timing = function(
53+
value: AnimatedValue | AnimatedValueXY,
54+
config: TimingAnimationConfig,
55+
): CompositeAnimation {
56+
return emptyAnimation;
57+
};
58+
59+
const decay = function(
60+
value: AnimatedValue | AnimatedValueXY,
61+
config: DecayAnimationConfig,
62+
): CompositeAnimation {
63+
return emptyAnimation;
64+
};
65+
66+
const sequence = function(
67+
animations: Array<CompositeAnimation>,
68+
): CompositeAnimation {
69+
return emptyAnimation;
70+
};
71+
72+
type ParallelConfig = {
73+
stopTogether?: boolean,
74+
};
75+
const parallel = function(
76+
animations: Array<CompositeAnimation>,
77+
config?: ?ParallelConfig,
78+
): CompositeAnimation {
79+
return emptyAnimation;
80+
};
81+
82+
const delay = function(time: number): CompositeAnimation {
83+
return emptyAnimation;
84+
};
85+
86+
const stagger = function(
87+
time: number,
88+
animations: Array<CompositeAnimation>,
89+
): CompositeAnimation {
90+
return emptyAnimation;
91+
};
92+
93+
type LoopAnimationConfig = {iterations: number};
94+
95+
const loop = function(
96+
animation: CompositeAnimation,
97+
{iterations = -1}: LoopAnimationConfig = {},
98+
): CompositeAnimation {
99+
return emptyAnimation;
100+
};
101+
102+
const event = function(argMapping: Array<?Mapping>, config?: EventConfig): any {
103+
return null;
104+
};
105+
106+
module.exports = {
107+
...AnimatedImplementation,
108+
decay,
109+
timing,
110+
spring,
111+
delay,
112+
sequence,
113+
parallel,
114+
stagger,
115+
loop,
116+
event,
117+
};

0 commit comments

Comments
 (0)