-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.ts
More file actions
269 lines (249 loc) · 9.63 KB
/
Copy pathjest.setup.ts
File metadata and controls
269 lines (249 loc) · 9.63 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* eslint-disable @typescript-eslint/no-require-imports */
/* global jest */
jest.mock('react-native-reanimated', () => {
const RN = require('react-native')
const passthrough = (value: unknown) => value
const passthroughLast = (...values: unknown[]) => values[values.length - 1]
return {
View: RN.View,
Text: RN.Text,
Image: RN.Image,
ScrollView: RN.ScrollView,
FlatList: RN.FlatList,
createAnimatedComponent: (Component: unknown) => Component,
useSharedValue: <T>(value: T) => ({ value }),
useAnimatedStyle: (updater?: () => unknown) => (typeof updater === 'function' ? updater() : {}),
useAnimatedProps: (updater?: () => unknown) => (typeof updater === 'function' ? updater() : {}),
useAnimatedScrollHandler: (handler: (...args: unknown[]) => unknown) => handler,
useDerivedValue: (updater?: () => unknown) => ({ value: typeof updater === 'function' ? updater() : undefined }),
withTiming: passthrough,
withSpring: passthrough,
withDecay: passthrough,
withDelay: (_ms: number, value: unknown) => value,
withRepeat: (value: unknown) => value,
withSequence: passthroughLast,
cancelAnimation: jest.fn(),
interpolate: passthrough,
interpolateColor: () => '#000000',
runOnJS: (fn: (...args: unknown[]) => unknown) => fn,
runOnUI: (fn: (...args: unknown[]) => unknown) => fn,
useAnimatedRef: () => ({ current: null }),
measure: () => ({ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }),
scrollTo: jest.fn(),
Easing: {
linear: passthrough,
ease: passthrough,
in: passthrough,
out: passthrough,
inOut: passthrough,
bezier: () => passthrough
},
Extrapolation: {
CLAMP: 'clamp',
EXTEND: 'extend',
IDENTITY: 'identity'
},
FadeIn: { duration: () => ({}) },
FadeOut: { duration: () => ({}) },
Layout: { duration: () => ({}) },
default: {
View: RN.View,
Text: RN.Text,
Image: RN.Image,
ScrollView: RN.ScrollView,
FlatList: RN.FlatList,
createAnimatedComponent: (Component: unknown) => Component,
call: jest.fn()
}
}
})
jest.mock('react-native-worklets', () => ({
createSerializable: (value: unknown) => value,
isWorkletFunction: () => false,
runOnJS: (fn: (...args: unknown[]) => unknown) => fn,
runOnUI: (fn: (...args: unknown[]) => unknown) => fn
}))
;(globalThis as Record<string, unknown>).IS_REACT_ACT_ENVIRONMENT = true
// @rific/auto-paper
jest.mock('@rific/auto-paper', () => {
const { createSlice } = jest.requireActual('@reduxjs/toolkit')
const defaultThemeSettings = { appearance: 'system', blur: true, color: '#6750a4', harmony: 'split-complementary' }
const slice = createSlice({
name: 'theme',
initialState: defaultThemeSettings,
reducers: {
initialize: (state: object, action: { payload: object }) => ({ ...state, ...action.payload }),
setAppearance: (state: object, action: { payload: string }) => ({ ...state, appearance: action.payload }),
setBlur: (state: object, action: { payload: boolean }) => ({ ...state, blur: action.payload }),
setColor: (state: object, action: { payload: string }) => ({ ...state, color: action.payload }),
setHarmony: (state: object, action: { payload: string }) => ({ ...state, harmony: action.payload })
}
})
return {
createThemeReducer: (init: object = {}) =>
createSlice({ name: 'theme', initialState: { ...slice.getInitialState(), ...init }, reducers: slice.caseReducers as never }).reducer,
themeActions: slice.actions,
themeReducer: slice.reducer,
defaultThemeSettings,
Provider: ({ children }: { children?: React.ReactNode }) => children,
useThemeSettings: () => ({ settings: slice.getInitialState(), set: jest.fn() })
}
})
// @rific/haptic-press
jest.mock('@rific/haptic-press', () => {
const { createSlice } = jest.requireActual('@reduxjs/toolkit')
const defaultHapticSettings = { vibrate: true }
const slice = createSlice({
name: 'haptic',
initialState: defaultHapticSettings,
reducers: {
initialize: (_state: object, action: { payload: object }) => action.payload,
setVibrate: (state: object, action: { payload: boolean }) => ({ ...state, vibrate: action.payload })
}
})
return {
defaultHapticSettings,
hapticReducer: slice.reducer,
hapticActions: slice.actions,
HapticPressProvider: ({ children }: { children?: React.ReactNode }) => children,
useHapticSettings: () => ({ settings: defaultHapticSettings, set: jest.fn() })
}
})
// @rific/scroll-view
jest.mock('@rific/scroll-view', () => {
const { createSlice } = jest.requireActual('@reduxjs/toolkit')
const defaultScrollViewSettings = { backActionFixed: true, footerFixed: false, headerFixed: false, snapBack: false }
const slice = createSlice({
name: 'scrollView',
initialState: defaultScrollViewSettings,
reducers: {
initialize: (_state: object, action: { payload: object }) => action.payload
}
})
return {
defaultScrollViewSettings,
scrollViewReducer: slice.reducer,
scrollViewActions: slice.actions,
ScrollViewSettingsProvider: ({ children }: { children?: React.ReactNode }) => children,
useScrollViewSettings: () => ({ settings: defaultScrollViewSettings, set: jest.fn() }),
ScrollViewProvider: ({ children }: { children?: React.ReactNode }) => children,
ScrollView: ({ children }: { children?: React.ReactNode }) => children,
ScrollViewHeader: () => null,
ScrollViewFooter: () => null
}
})
// @expo/vector-icons
jest.mock('@expo/vector-icons', () => {
const Icon = ({ children }: { children?: React.ReactNode }) => children || null
return {
Ionicons: Icon,
MaterialCommunityIcons: Icon,
MaterialIcons: Icon,
FontAwesome: Icon,
default: { Ionicons: Icon }
}
})
// @react-native-async-storage/async-storage
jest.mock('@react-native-async-storage/async-storage', () => ({
clear: () => Promise.resolve(null),
getAllKeys: () => Promise.resolve([]),
getItem: () => Promise.resolve(null),
removeItem: () => Promise.resolve(null),
setItem: () => Promise.resolve(null)
}))
// expo-blur
jest.mock('expo-blur', () => ({
BlurView: ({ children }: { children?: React.ReactNode }) => children
}))
// expo-linking
jest.mock('expo-linking', () => ({
useLinkingURL: () => null
}))
// expo-splash-screen
jest.mock('expo-splash-screen', () => ({
preventAutoHideAsync: jest.fn().mockResolvedValue(undefined),
setOptions: jest.fn(),
hideAsync: jest.fn().mockResolvedValue(undefined)
}))
// react-native-gesture-handler
jest.mock('react-native-gesture-handler', () => {
const makeGesture = () => {
const g: Record<string, () => typeof g> = {}
g.onUpdate = () => g
g.onEnd = () => g
g.onStart = () => g
g.onChange = () => g
return g
}
return {
PanGestureHandler: ({ children }: { children?: React.ReactNode }) => children,
PinchGestureHandler: ({ children }: { children?: React.ReactNode }) => children,
GestureHandlerRootView: ({ children }: { children?: React.ReactNode }) => children,
State: { ACTIVE: 'ACTIVE', END: 'END' },
Gesture: {
Pinch: () => makeGesture(),
Pan: () => makeGesture(),
Simultaneous: (..._gs: unknown[]) => makeGesture(),
Race: (..._gs: unknown[]) => makeGesture(),
Sequence: (..._gs: unknown[]) => makeGesture()
},
GestureDetector: ({ children }: { children?: React.ReactNode }) => children
}
})
// react-native-keyboard-controller
jest.mock('react-native-keyboard-controller', () => ({
KeyboardController: {
addListener: jest.fn(),
dismiss: jest.fn(),
removeListener: jest.fn(),
setInputMode: jest.fn()
},
KeyboardProvider: ({ children }: { children?: React.ReactNode }) => children,
KeyboardAwareScrollView: ({ children }: { children?: React.ReactNode }) => children,
useKeyboardHandler: jest.fn()
}))
// react-native-safe-area-context
jest.mock('react-native-safe-area-context', () => ({
SafeAreaProvider: ({ children }: { children?: React.ReactNode }) => children,
SafeAreaInsetsContext: {
Consumer: ({ children }: { children?: (insets: object) => React.ReactNode }) => children?.({ top: 0, right: 0, bottom: 0, left: 0 })
},
useSafeAreaInsets: () => ({ top: 0, bottom: 0, left: 0, right: 0 })
}))
// redux-persist
jest.mock('redux-persist', () => {
const actual = jest.requireActual('redux-persist')
return {
...actual,
persistStore: jest.fn(() => ({ purge: jest.fn(), flush: jest.fn() })),
persistReducer: (_config: unknown, reducer: unknown) => reducer,
persistCombineReducers: (_config: unknown, reducers: unknown) => reducers,
createMigrate: jest.fn(),
createTransform: jest.fn(),
getStoredState: jest.fn().mockResolvedValue(undefined)
}
})
jest.mock('redux-persist/integration/react', () => ({
PersistGate: ({ children }: { children?: React.ReactNode }) => children
}))
const handleUnhandledRejection = (reason: unknown) => {
console.error('UnhandledRejection in tests:', reason) // eslint-disable-line no-console
}
const handleUncaughtException = (err: unknown) => {
console.error('UncaughtException in tests:', err) // eslint-disable-line no-console
}
if (typeof process !== 'undefined' && process?.on) {
process.on('unhandledRejection', handleUnhandledRejection)
process.on('uncaughtException', handleUncaughtException)
}
try {
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper')
} catch {
// ignore
}
if (typeof globalThis.requestAnimationFrame === 'undefined') {
globalThis.requestAnimationFrame = (cb: FrameRequestCallback) => setTimeout(cb, 0)
}
if (typeof globalThis.cancelAnimationFrame === 'undefined') {
globalThis.cancelAnimationFrame = (id: number | null | undefined) => clearTimeout(id ?? undefined)
}