UNPKG

5.92 kBJavaScriptView Raw
1/**
2 * Mock implementation for test runners.
3 *
4 * Example:
5 *
6 * ```js
7 * jest.mock('react-native-reanimated', () => require('react-native-reanimated/mock'));
8 * ```
9 */
10
11const React = require('react');
12const { View, Text, Image, Animated, Platform } = require('react-native');
13
14function NOOP() {}
15
16function simulateCallbackFactory(...params) {
17 return (callback) => {
18 callback &&
19 setTimeout(() => {
20 callback(...params);
21 }, 0);
22 };
23}
24
25class Code extends React.Component {
26 render() {
27 return null;
28 }
29}
30
31const getValue = node => {
32 if (typeof node === "number") {
33 return node;
34 }
35 return node && node[" __value"] || 0;
36};
37
38class AnimatedValue {
39 constructor(val) {
40 this[" __value"] = val;
41 }
42
43 setValue(val) {
44 this[" __value"] = val;
45 }
46
47 interpolate() {
48 return this;
49 }
50}
51
52function createMockComponent(name) {
53 return class extends React.Component {
54 static displayName = name;
55
56 render() {
57 return this.props.children;
58 }
59 };
60}
61
62function createTransitioningComponent(Component) {
63 return class extends React.Component {
64 static displayName = `Transitioning.${Component.displayName || Component.name || 'Component'}`;
65
66 setNativeProps() {}
67
68 animateNextTransition() {}
69
70 render() {
71 return <Component {...this.props} />;
72 }
73 };
74}
75
76const Reanimated = {
77 SpringUtils: {
78 makeDefaultConfig: NOOP,
79 makeConfigFromBouncinessAndSpeed: NOOP,
80 makeConfigFromOrigamiTensionAndFriction: NOOP,
81 },
82
83 View,
84 Text,
85 Image,
86 ScrollView: Animated.ScrollView,
87 Code,
88
89 Clock: NOOP,
90 Node: NOOP,
91 Value: AnimatedValue,
92
93 Extrapolate: {
94 EXTEND: 'extend',
95 CLAMP: 'clamp',
96 IDENTITY: 'identity',
97 },
98
99
100 add: (...vals) =>
101 new AnimatedValue(vals.map(v => getValue(v)).reduce((acc, v) => acc + v)),
102 sub: (...vals) =>
103 new AnimatedValue(vals.map(v => getValue(v)).reduce((acc, v) => acc - v)),
104 divide: (...vals) =>
105 new AnimatedValue(vals.map(v => getValue(v)).reduce((acc, v) => acc / v)),
106 multiply: (...vals) =>
107 new AnimatedValue(vals.map(v => getValue(v)).reduce((acc, v) => acc * v)),
108 pow: (...vals) =>
109 new AnimatedValue(vals.map(v => getValue(v)).reduce((acc, v) => acc ** v)),
110 modulo: (a, b) => new AnimatedValue(getValue(a) % getValue(b)),
111 sqrt: a => new AnimatedValue(Math.sqrt(getValue(a))),
112 log: a => new AnimatedValue(Math.log(getValue(a))),
113 sin: a => new AnimatedValue(Math.sin(getValue(a))),
114 cos: a => new AnimatedValue(Math.cos(getValue(a))),
115 tan: a => new AnimatedValue(Math.tan(getValue(a))),
116 acos: a => new AnimatedValue(Math.acos(getValue(a))),
117 asin: a => new AnimatedValue(Math.asin(getValue(a))),
118 atan: a => new AnimatedValue(Math.atan(getValue(a))),
119 exp: a => new AnimatedValue(Math.exp(getValue(a))),
120 round: a => new AnimatedValue(Math.round(getValue(a))),
121 floor: a => new AnimatedValue(Math.floor(getValue(a))),
122 ceil: a => new AnimatedValue(Math.ceil(getValue(a))),
123 lessThan: (a, b) => new AnimatedValue(getValue(a) < getValue(b)),
124 eq: (a, b) => new AnimatedValue(getValue(a) === getValue(b)),
125 greaterThan: (a, b) => new AnimatedValue(getValue(a) > getValue(b)),
126 lessOrEq: (a, b) => new AnimatedValue(getValue(a) <= getValue(b)),
127 greaterOrEq: (a, b) => new AnimatedValue(getValue(a) >= getValue(b)),
128 neq: (a, b) => new AnimatedValue(getValue(a) !== getValue(b)),
129 and: (a, b) => new AnimatedValue(getValue(a) && getValue(b)),
130 or: (a, b) => new AnimatedValue(getValue(a) || getValue(b)),
131 defined: (a) => new AnimatedValue(getValue(a) !== null && getValue(a) !== undefined),
132 not: (a) => new AnimatedValue(!getValue(a)),
133 set: (a, b) => {
134 a.setValue(getValue(b));
135 return a;
136 },
137 concat: (a, b) => `${a}${b}`,
138 cond: (a, b, c) => {
139 if (getValue(a)) {
140 return b;
141 } else {
142 return c;
143 }
144 },
145 block: (a) => a[a.length - 1],
146 call: (a, b) => b(a.map(getValue)),
147 debug: NOOP,
148 onChange: NOOP,
149 startClock: NOOP,
150 stopClock: NOOP,
151 clockRunning: NOOP,
152 event: NOOP,
153 abs: (a) => Math.abs(getValue(a)),
154 acc: NOOP,
155 color: (r, g, b, a = 1) => {
156 const color = 16777216 * Math.round(getValue(a) * 255) + 65536 * getValue(r) + 256 * getValue(g) + getValue(b);
157 if (Platform.OS === 'android') {
158 // on Android color is represented as signed 32 bit int
159 if (color < (1 << 31) >>> 0) {
160 return new AnimatedValue(color);
161 }
162 return new AnimatedValue(color - 2 ** 32);
163 }
164 return new AnimatedValue(color);
165 },
166 diff: NOOP,
167 diffClamp: NOOP,
168 interpolate: NOOP,
169 max: (a, b) => Math.max(getValue(a), getValue(b)),
170 min: (a, b) => Math.min(getValue(a), getValue(b)),
171
172 decay: () => ({
173 start: simulateCallbackFactory({ finished: true }),
174 stop: simulateCallbackFactory({ finished: true }),
175 }),
176 timing: () => ({
177 start: simulateCallbackFactory({ finished: true }),
178 stop: simulateCallbackFactory({ finished: true }),
179 }),
180 spring: () => ({
181 start: simulateCallbackFactory({ finished: true }),
182 stop: simulateCallbackFactory({ finished: true }),
183 }),
184
185 proc: cb => cb,
186
187 useCode: NOOP,
188 createAnimatedComponent: Component => Component,
189}
190
191module.exports = {
192 __esModule: true,
193
194 ...Reanimated,
195
196 default: Reanimated,
197
198 Easing: {
199 linear: NOOP,
200 ease: NOOP,
201 quad: NOOP,
202 cubic: NOOP,
203 poly: () => NOOP,
204 sin: NOOP,
205 circle: NOOP,
206 exp: NOOP,
207 elastic: () => NOOP,
208 back: () => NOOP,
209 bounce: () => NOOP,
210 bezier: () => NOOP,
211 in: () => NOOP,
212 out: () => NOOP,
213 inOut: () => NOOP,
214 },
215
216 Transitioning: {
217 View: createTransitioningComponent(View)
218 },
219
220 Transition: {
221 Sequence: createMockComponent('Transition.Sequence'),
222 Together: createMockComponent('Transition.Together'),
223 In: createMockComponent('Transition.In'),
224 Out: createMockComponent('Transition.Out'),
225 Change: createMockComponent('Transition.Change')
226 },
227
228 createTransitioningComponent,
229};
230
\No newline at end of file