UNPKG

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