-
-
Notifications
You must be signed in to change notification settings - Fork 946
Expand file tree
/
Copy pathAnimatedCoordinatesArray.test.js
More file actions
199 lines (180 loc) · 5.64 KB
/
Copy pathAnimatedCoordinatesArray.test.js
File metadata and controls
199 lines (180 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import FakeTimers from '@sinonjs/fake-timers';
import { Animated, Easing } from 'react-native';
import TestRenderer from 'react-test-renderer';
import { act } from '@testing-library/react-native';
import React from 'react';
import { AnimatedShape, AnimatedCoordinatesArray } from '../../../src/classes';
import { ShapeSource } from '../../../src/components/ShapeSource';
let clock = null;
let oldNodeEnv = null;
beforeAll(() => {
clock = FakeTimers.install();
clock._requestedAnimationFrames = [];
clock.requestAnimationFrame = callback => {
clock._requestedAnimationFrames.push(callback);
};
clock.fireRequestAnimationFrames = () => {
const oldRAF = clock._requestedAnimationFrames;
clock._requestedAnimationFrames = [];
oldRAF.forEach(cb => cb(Date.now()));
};
// animated will not call nativeProps in test mode
// https://github.com/facebook/react-native/blob/34d3373bb0f7ee405292bf993163f29759ba5205/Libraries/Animated/createAnimatedComponent.js#L150-L156
oldNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'dev';
});
afterAll(() => {
process.env.NODE_ENV = oldNodeEnv;
clock.uninstall();
});
const AnimatedShapeSource = Animated.createAnimatedComponent(ShapeSource);
function _nativeRef(ref) {
return ref._nativeRef;
}
describe('AnimatedShapeSource', () => {
test('testSetNativeProps', () => {
AnimatedShapeSource.__skipSetNativeProps_FOR_TESTS_ONLY = false;
const coordinates = new AnimatedCoordinatesArray([
[1, 1],
[10, 10],
]);
let shapeSourceRef;
act(() => {
// eslint-disable-next-line no-unused-vars
const testRenderer = TestRenderer.create(
<AnimatedShapeSource
shape={new AnimatedShape({ type: 'LineString', coordinates })}
ref={ref => (shapeSourceRef = ref)}
/>,
);
});
const setNativeProps = jest.fn();
_nativeRef(shapeSourceRef).setNativeProps = setNativeProps;
coordinates
.timing({
toValue: [
[21, 21],
[30, 30],
],
duration: 20,
easing: Easing.linear,
useNativeDriver: false,
})
.start();
expect(setNativeProps).toHaveBeenCalledTimes(0);
// process.env.NODE_ENV = 'TEST_butDontSkipSetNativeProps' for future RN
for (let i = 0; i < 5; i++) {
clock.tick(4);
clock.fireRequestAnimationFrames();
expect(setNativeProps).toHaveBeenCalledTimes(i + 1);
expect(setNativeProps).toHaveBeenCalledWith({
shape: JSON.stringify({
type: 'LineString',
coordinates: [
[1 + (i + 1) * 4, 1 + (i + 1) * 4],
[10 + (i + 1) * 4, 10 + (i + 1) * 4],
],
}),
});
}
// process.env.NODE_ENV = 'TEST';
});
test('testAddingCoords', () => {
AnimatedShapeSource.__skipSetNativeProps_FOR_TESTS_ONLY = false;
const coordinates = new AnimatedCoordinatesArray([
[1, 1],
[10, 10],
]);
let shapeSourceRef;
act(() => {
// eslint-disable-next-line no-unused-vars
const testRenderer = TestRenderer.create(
<AnimatedShapeSource
shape={new AnimatedShape({ type: 'LineString', coordinates })}
ref={ref => (shapeSourceRef = ref)}
/>,
);
});
const setNativeProps = jest.fn();
_nativeRef(shapeSourceRef).setNativeProps = setNativeProps;
coordinates
.timing({
toValue: [
[21, 21],
[30, 30],
[50, 50],
],
duration: 20,
easing: Easing.linear,
useNativeDriver: false,
})
.start();
expect(setNativeProps).toHaveBeenCalledTimes(0);
// process.env.NODE_ENV = 'TEST_butDontSkipSetNativeProps' for future RN
for (let i = 0; i < 5; i++) {
clock.tick(4);
clock.fireRequestAnimationFrames();
expect(setNativeProps).toHaveBeenCalledTimes(i + 1);
expect(setNativeProps).toHaveBeenCalledWith({
shape: JSON.stringify({
type: 'LineString',
coordinates: [
[1 + (i + 1) * 4, 1 + (i + 1) * 4],
[10 + (i + 1) * 4, 10 + (i + 1) * 4],
[10 + (i + 1) * 8, 10 + (i + 1) * 8],
],
}),
});
}
// process.env.NODE_ENV = 'TEST';
});
test('testRemovingCoords', () => {
AnimatedShapeSource.__skipSetNativeProps_FOR_TESTS_ONLY = false;
const coordinates = new AnimatedCoordinatesArray([
[1, 1],
[10, 10],
[50, 50],
]);
let shapeSourceRef;
act(() => {
// eslint-disable-next-line no-unused-vars
const testRenderer = TestRenderer.create(
<AnimatedShapeSource
shape={new AnimatedShape({ type: 'LineString', coordinates })}
ref={ref => (shapeSourceRef = ref)}
/>,
);
});
const setNativeProps = jest.fn();
_nativeRef(shapeSourceRef).setNativeProps = setNativeProps;
coordinates
.timing({
toValue: [
[21, 21],
[30, 30],
],
duration: 20,
easing: Easing.linear,
useNativeDriver: false,
})
.start();
expect(setNativeProps).toHaveBeenCalledTimes(0);
// process.env.NODE_ENV = 'TEST_butDontSkipSetNativeProps' for future RN
for (let i = 0; i < 5; i++) {
clock.tick(4);
clock.fireRequestAnimationFrames();
expect(setNativeProps).toHaveBeenCalledTimes(i + 1);
expect(setNativeProps).toHaveBeenCalledWith({
shape: JSON.stringify({
type: 'LineString',
coordinates: [
[1 + (i + 1) * 4, 1 + (i + 1) * 4],
[10 + (i + 1) * 4, 10 + (i + 1) * 4],
[50 - (i + 1) * 4, 50 - (i + 1) * 4],
],
}),
});
}
// process.env.NODE_ENV = 'TEST';
});
});