-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathjestUtils.ts
217 lines (191 loc) · 5.13 KB
/
jestUtils.ts
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { jestResetJsReanimatedModule } from './core';
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Matchers<R> {
toHaveAnimatedStyle(
style: Record<string, unknown>[] | Record<string, unknown>
): R;
}
}
}
let config = {
fps: 60,
};
const isAnimatedStyle = (style) => {
return !!style.animatedStyle;
};
const getAnimatedStyleFromObject = (style) => {
return style.animatedStyle.current.value;
};
const getCurrentStyle = (received) => {
const styleObject = received.props.style;
let currentStyle = {};
if (Array.isArray(styleObject)) {
received.props.style.forEach((style) => {
if (isAnimatedStyle(style)) {
currentStyle = {
...currentStyle,
...getAnimatedStyleFromObject(style),
};
} else {
currentStyle = {
...currentStyle,
...style,
};
}
});
} else {
if (isAnimatedStyle(styleObject)) {
currentStyle = getAnimatedStyleFromObject(styleObject);
} else {
currentStyle = {
...styleObject,
...received.props.animatedStyle.value,
};
}
}
return currentStyle;
};
const checkEqual = (currentStyle, expectStyle) => {
if (Array.isArray(expectStyle)) {
if (expectStyle.length !== currentStyle.length) return false;
for (let i = 0; i < currentStyle.length; i++) {
if (!checkEqual(currentStyle[i], expectStyle[i])) {
return false;
}
}
} else if (typeof currentStyle === 'object' && currentStyle) {
for (const property in expectStyle) {
if (!checkEqual(currentStyle[property], expectStyle[property])) {
return false;
}
}
} else {
return currentStyle === expectStyle;
}
return true;
};
const findStyleDiff = (current, expect, requireAllMatch) => {
const diffs = [];
let isEqual = true;
for (const property in expect) {
if (!checkEqual(current[property], expect[property])) {
isEqual = false;
diffs.push({
property: property,
current: current[property],
expect: expect[property],
});
}
}
if (
requireAllMatch &&
Object.keys(current).length !== Object.keys(expect).length
) {
isEqual = false;
for (const property in current) {
if (expect[property] === undefined) {
diffs.push({
property: property,
current: current[property],
expect: expect[property],
});
}
}
}
return { isEqual, diffs };
};
const compareStyle = (received, expectedStyle, config) => {
if (!received.props.style) {
return { message: () => message, pass: false };
}
const { exact } = config;
const currentStyle = getCurrentStyle(received);
const { isEqual, diffs } = findStyleDiff(currentStyle, expectedStyle, exact);
if (isEqual) {
return { message: () => 'ok', pass: true };
}
const currentStyleStr = JSON.stringify(currentStyle);
const expectedStyleStr = JSON.stringify(expectedStyle);
const differences = diffs
.map(
(diff) =>
`- '${diff.property}' should be ${JSON.stringify(
diff.expect
)}, but is ${JSON.stringify(diff.current)}`
)
.join('\n');
return {
message: () =>
`Expected: ${expectedStyleStr}\nReceived: ${currentStyleStr}\n\nDifferences:\n${differences}`,
pass: false,
};
};
let frameTime = 1000 / config.fps;
let requestAnimationFrameCopy;
let currentTimestamp = 0;
const requestAnimationFrame = (callback) => {
setTimeout(callback, frameTime);
};
const beforeTest = () => {
jestResetJsReanimatedModule();
requestAnimationFrameCopy = global.requestAnimationFrame;
global.requestAnimationFrame = requestAnimationFrame;
global.ReanimatedDataMock = {
now: () => currentTimestamp,
};
currentTimestamp = 0;
jest.useFakeTimers();
};
const afterTest = () => {
jest.useRealTimers();
global.requestAnimationFrame = requestAnimationFrameCopy;
};
const tickTravel = () => {
currentTimestamp += frameTime;
jest.advanceTimersByTime(frameTime);
};
export const withReanimatedTimer = (animationTest) => {
beforeTest();
animationTest();
afterTest();
};
export const advanceAnimationByTime = (time = frameTime) => {
for (let i = 0; i <= Math.ceil(time / frameTime); i++) {
tickTravel();
}
jest.advanceTimersByTime(frameTime);
};
export const advanceAnimationByFrame = (count) => {
for (let i = 0; i <= count; i++) {
tickTravel();
}
jest.advanceTimersByTime(frameTime);
};
export const setUpTests = (userConfig = {}) => {
let expect;
try {
expect = require('expect');
} catch (_) {
// for Jest in version 28+
const { expect: expectModule } = require('@jest/globals');
expect = expectModule;
}
require('setimmediate');
frameTime = Math.round(1000 / config.fps);
config = {
...config,
...userConfig,
};
expect.extend({
toHaveAnimatedStyle(received, expectedStyle, config = {}) {
return compareStyle(received, expectedStyle, config);
},
});
};
export const getAnimatedStyle = (received) => {
return getCurrentStyle(received);
};