From 45686c86e2b50c98436d511dd63f0e987f565008 Mon Sep 17 00:00:00 2001 From: Peter Argany Date: Mon, 4 Feb 2019 18:25:23 -0800 Subject: [PATCH] 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 --- Libraries/Animated/src/Animated.js | 6 +- .../Animated/src/AnimatedImplementation.js | 2 + Libraries/Animated/src/AnimatedMock.js | 117 ++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 Libraries/Animated/src/AnimatedMock.js diff --git a/Libraries/Animated/src/Animated.js b/Libraries/Animated/src/Animated.js index f04de4a7aff3f1..41b5173f543228 100644 --- a/Libraries/Animated/src/Animated.js +++ b/Libraries/Animated/src/Animated.js @@ -10,7 +10,11 @@ 'use strict'; -const AnimatedImplementation = require('AnimatedImplementation'); +import Platform from 'Platform'; + +const AnimatedImplementation = Platform.isTesting + ? require('AnimatedMock') + : require('AnimatedImplementation'); module.exports = { get FlatList() { diff --git a/Libraries/Animated/src/AnimatedImplementation.js b/Libraries/Animated/src/AnimatedImplementation.js index d14ac612ee7f51..88f7c02c045880 100644 --- a/Libraries/Animated/src/AnimatedImplementation.js +++ b/Libraries/Animated/src/AnimatedImplementation.js @@ -514,6 +514,8 @@ const event = function(argMapping: Array, config?: EventConfig): any { * easy to build and maintain. `Animated` focuses on declarative relationships * between inputs and outputs, with configurable transforms in between, and * simple `start`/`stop` methods to control time-based animation execution. + * If additional transforms are added, be sure to include them in + * AnimatedMock.js as well. * * See http://facebook.github.io/react-native/docs/animated.html */ diff --git a/Libraries/Animated/src/AnimatedMock.js b/Libraries/Animated/src/AnimatedMock.js new file mode 100644 index 00000000000000..fcd57b90db65b5 --- /dev/null +++ b/Libraries/Animated/src/AnimatedMock.js @@ -0,0 +1,117 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ +'use strict'; + +const AnimatedImplementation = require('AnimatedImplementation'); +const AnimatedValueXY = require('./nodes/AnimatedValueXY'); +const AnimatedValue = require('./nodes/AnimatedValue'); + +import type {EndCallback} from './animations/Animation'; +import type {TimingAnimationConfig} from './animations/TimingAnimation'; +import type {DecayAnimationConfig} from './animations/DecayAnimation'; +import type {SpringAnimationConfig} from './animations/SpringAnimation'; +import type {Mapping, EventConfig} from './AnimatedEvent'; + +/** + * Animations are a source of flakiness in snapshot testing. This mock replaces + * animation functions from AnimatedImplementation with empty animations for + * predictability in tests. + */ +type CompositeAnimation = { + start: (callback?: ?EndCallback) => void, + stop: () => void, + reset: () => void, + _startNativeLoop: (iterations?: number) => void, + _isUsingNativeDriver: () => boolean, +}; + +const emptyAnimation = { + start: () => {}, + stop: () => {}, + reset: () => {}, + _startNativeLoop: () => {}, + _isUsingNativeDriver: () => { + return false; + }, +}; + +const spring = function( + value: AnimatedValue | AnimatedValueXY, + config: SpringAnimationConfig, +): CompositeAnimation { + return emptyAnimation; +}; + +const timing = function( + value: AnimatedValue | AnimatedValueXY, + config: TimingAnimationConfig, +): CompositeAnimation { + return emptyAnimation; +}; + +const decay = function( + value: AnimatedValue | AnimatedValueXY, + config: DecayAnimationConfig, +): CompositeAnimation { + return emptyAnimation; +}; + +const sequence = function( + animations: Array, +): CompositeAnimation { + return emptyAnimation; +}; + +type ParallelConfig = { + stopTogether?: boolean, +}; +const parallel = function( + animations: Array, + config?: ?ParallelConfig, +): CompositeAnimation { + return emptyAnimation; +}; + +const delay = function(time: number): CompositeAnimation { + return emptyAnimation; +}; + +const stagger = function( + time: number, + animations: Array, +): CompositeAnimation { + return emptyAnimation; +}; + +type LoopAnimationConfig = {iterations: number}; + +const loop = function( + animation: CompositeAnimation, + {iterations = -1}: LoopAnimationConfig = {}, +): CompositeAnimation { + return emptyAnimation; +}; + +const event = function(argMapping: Array, config?: EventConfig): any { + return null; +}; + +module.exports = { + ...AnimatedImplementation, + decay, + timing, + spring, + delay, + sequence, + parallel, + stagger, + loop, + event, +};