Skip to content

Commit b03e824

Browse files
javachefacebook-github-bot
authored andcommitted
Mock composite animations when testing
Summary: Single and composite animations were handled inconsistently in AnimatedMock. Also added a guard to prevent callbacks from triggering additional animations, since we had a test-scenario that did exactly that. Changelog: [General][Fixed] - Composite animations will now be ran immediately when the app is in testing mode Reviewed By: yungsters Differential Revision: D31826967 fbshipit-source-id: a6416b42e227fe79f5c3a55a9c51beb8451874f8
1 parent e3a71b0 commit b03e824

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

Libraries/Animated/AnimatedMock.js

+50-10
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,38 @@ import type {SpringAnimationConfig} from './animations/SpringAnimation';
2727
/**
2828
* Animations are a source of flakiness in snapshot testing. This mock replaces
2929
* animation functions from AnimatedImplementation with empty animations for
30-
* predictability in tests.
30+
* predictability in tests. When possible the animation will run immediately
31+
* to the final state.
3132
*/
33+
34+
// Prevent any callback invocation from recursively triggering another
35+
// callback, which may trigger another animation
36+
let inAnimationCallback = false;
37+
function mockAnimationStart(
38+
start: (callback?: ?EndCallback) => void,
39+
): (callback?: ?EndCallback) => void {
40+
return callback => {
41+
const guardedCallback =
42+
callback == null
43+
? callback
44+
: (...args) => {
45+
if (inAnimationCallback) {
46+
console.warn(
47+
'Ignoring recursive animation callback when running mock animations',
48+
);
49+
return;
50+
}
51+
inAnimationCallback = true;
52+
try {
53+
callback(...args);
54+
} finally {
55+
inAnimationCallback = false;
56+
}
57+
};
58+
start(guardedCallback);
59+
};
60+
}
61+
3262
export type CompositeAnimation = {
3363
start: (callback?: ?EndCallback) => void,
3464
stop: () => void,
@@ -48,17 +78,27 @@ const emptyAnimation = {
4878
},
4979
};
5080

81+
const mockCompositeAnimation = (
82+
animations: Array<CompositeAnimation>,
83+
): CompositeAnimation => ({
84+
...emptyAnimation,
85+
start: mockAnimationStart((callback?: ?EndCallback): void => {
86+
animations.forEach(animation => animation.start());
87+
callback?.({finished: true});
88+
}),
89+
});
90+
5191
const spring = function(
5292
value: AnimatedValue | AnimatedValueXY,
5393
config: SpringAnimationConfig,
5494
): CompositeAnimation {
5595
const anyValue: any = value;
5696
return {
5797
...emptyAnimation,
58-
start: (callback?: ?EndCallback): void => {
98+
start: mockAnimationStart((callback?: ?EndCallback): void => {
5999
anyValue.setValue(config.toValue);
60-
callback && callback({finished: true});
61-
},
100+
callback?.({finished: true});
101+
}),
62102
};
63103
};
64104

@@ -69,10 +109,10 @@ const timing = function(
69109
const anyValue: any = value;
70110
return {
71111
...emptyAnimation,
72-
start: (callback?: ?EndCallback): void => {
112+
start: mockAnimationStart((callback?: ?EndCallback): void => {
73113
anyValue.setValue(config.toValue);
74-
callback && callback({finished: true});
75-
},
114+
callback?.({finished: true});
115+
}),
76116
};
77117
};
78118

@@ -86,15 +126,15 @@ const decay = function(
86126
const sequence = function(
87127
animations: Array<CompositeAnimation>,
88128
): CompositeAnimation {
89-
return emptyAnimation;
129+
return mockCompositeAnimation(animations);
90130
};
91131

92132
type ParallelConfig = {stopTogether?: boolean, ...};
93133
const parallel = function(
94134
animations: Array<CompositeAnimation>,
95135
config?: ?ParallelConfig,
96136
): CompositeAnimation {
97-
return emptyAnimation;
137+
return mockCompositeAnimation(animations);
98138
};
99139

100140
const delay = function(time: number): CompositeAnimation {
@@ -105,7 +145,7 @@ const stagger = function(
105145
time: number,
106146
animations: Array<CompositeAnimation>,
107147
): CompositeAnimation {
108-
return emptyAnimation;
148+
return mockCompositeAnimation(animations);
109149
};
110150

111151
type LoopAnimationConfig = {

0 commit comments

Comments
 (0)