Skip to content

Instantly share code, notes, and snippets.

@Grohden
Created January 30, 2020 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grohden/0abd03cc0e7f50266d82f88a9feba9f7 to your computer and use it in GitHub Desktop.
Save Grohden/0abd03cc0e7f50266d82f88a9feba9f7 to your computer and use it in GitHub Desktop.
Fixed file for react native AnimatedMock.js
/**
* 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 {AnimatedEvent, attachNativeEvent} = require('./AnimatedEvent');
const AnimatedImplementation = require('./AnimatedImplementation');
const AnimatedInterpolation = require('./nodes/AnimatedInterpolation');
const AnimatedNode = require('./nodes/AnimatedNode');
const AnimatedProps = require('./nodes/AnimatedProps');
const AnimatedValue = require('./nodes/AnimatedValue');
const AnimatedValueXY = require('./nodes/AnimatedValueXY');
const createAnimatedComponent = require('./createAnimatedComponent');
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: (callback) => { callback && callback({finished: true}) },
stop: () => {},
reset: () => {},
_startNativeLoop: () => {},
_isUsingNativeDriver: () => {
return false;
},
};
const spring = function(
value: AnimatedValue | AnimatedValueXY,
config: SpringAnimationConfig,
): CompositeAnimation {
const anyValue: any = value;
return {
...emptyAnimation,
start: (callback?: ?EndCallback): void => {
anyValue.setValue(config.toValue);
callback && callback({finished: true});
},
};
};
const timing = function(
value: AnimatedValue | AnimatedValueXY,
config: TimingAnimationConfig,
): CompositeAnimation {
const anyValue: any = value;
return {
...emptyAnimation,
start: (callback?: ?EndCallback): void => {
anyValue.setValue(config.toValue);
callback && callback({finished: true});
},
};
};
const decay = function(
value: AnimatedValue | AnimatedValueXY,
config: DecayAnimationConfig,
): CompositeAnimation {
const anyValue: any = value;
return {
...emptyAnimation,
start: (callback?: ?EndCallback): void => {
anyValue.setValue(config.toValue);
callback && callback({finished: true});
}
};
};
const sequence = function(
animations: Array<CompositeAnimation>,
): CompositeAnimation {
return {
...emptyAnimation,
start: (callback) => {
animations.forEach(it => it.start())
callback && callback({finished: true});
}
};
};
type ParallelConfig = {
stopTogether?: boolean,
};
const parallel = function(
animations: Array<CompositeAnimation>,
config?: ?ParallelConfig,
): CompositeAnimation {
return {
...emptyAnimation,
start: (callback) => {
animations.forEach(it => it.start())
callback && callback({finished: true});
}
};
};
const delay = function(time: number): CompositeAnimation {
return emptyAnimation;
};
const stagger = function(
time: number,
animations: Array<CompositeAnimation>,
): CompositeAnimation {
return {
...emptyAnimation,
start: (callback) => {
animations.forEach(it => it.start())
callback && callback({finished: true});
}
};
};
type LoopAnimationConfig = {iterations: number, resetBeforeIteration?: boolean};
const loop = function(
animation: CompositeAnimation,
{iterations = -1}: LoopAnimationConfig = {},
): CompositeAnimation {
return emptyAnimation;
};
module.exports = {
Value: AnimatedValue,
ValueXY: AnimatedValueXY,
Interpolation: AnimatedInterpolation,
Node: AnimatedNode,
decay,
timing,
spring,
add: AnimatedImplementation.add,
subtract: AnimatedImplementation.subtract,
divide: AnimatedImplementation.divide,
multiply: AnimatedImplementation.multiply,
modulo: AnimatedImplementation.modulo,
diffClamp: AnimatedImplementation.diffClamp,
delay,
sequence,
parallel,
stagger,
loop,
event: AnimatedImplementation.event,
createAnimatedComponent,
attachNativeEvent,
forkEvent: AnimatedImplementation.forkEvent,
unforkEvent: AnimatedImplementation.unforkEvent,
Event: AnimatedEvent,
__PropsOnlyForTests: AnimatedProps,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment