-
Notifications
You must be signed in to change notification settings - Fork 725
/
Copy pathjest-setup.js
154 lines (141 loc) · 5.04 KB
/
jest-setup.js
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
import {AccessibilityInfo, Animated} from 'react-native';
// ========= Mock Object.defineProperty to always allow overriding =========
const originalDefineProperty = Object.defineProperty;
Object.defineProperty = (obj, prop, desc) => {
try {
return originalDefineProperty(obj, prop, {...desc, configurable: true});
} catch (e) {
return originalDefineProperty(obj, prop, desc);
}
};
Object.defineProperties = (obj, props) => {
Object.keys(props).forEach(key => {
Object.defineProperty(obj, key, props[key]);
});
return obj;
};
// =========================================================================
global._UILIB_TESTING = true;
jest.spyOn(AccessibilityInfo, 'isScreenReaderEnabled').mockImplementation(() => Promise.resolve(false));
// mock native modules
jest.mock('@react-native-community/blur', () => {});
jest.mock('@react-native-community/netinfo', () => {});
// TODO: Adding here a todo for package.json: need to remove moduleNameMapper, see this: https://github.com/software-mansion/react-native-reanimated/issues/1196
jest.mock('react-native-reanimated', () => {
const reactNativeReanimated = require('react-native-reanimated/mock');
reactNativeReanimated.interpolateColor = jest.fn(v => v); // TODO: See this https://github.com/software-mansion/react-native-reanimated/issues/2749
reactNativeReanimated.FadeIn = {
duration: jest.fn()
};
return reactNativeReanimated;
});
global.__reanimatedWorkletInit = jest.fn();
jest.mock('react-native-gesture-handler',
() => {
jest.requireActual('react-native-gesture-handler/jestSetup');
const GestureHandler = jest.requireActual('react-native-gesture-handler');
GestureHandler.Gesture.Tap = () => {
const TapMock = {
_handlers: {}
};
const getDefaultMockedHandler = handlerName => handler => {
if (typeof handler === 'function') {
TapMock._handlers[handlerName] = handler;
}
return TapMock;
};
TapMock.type = 'tap';
TapMock.maxDuration = getDefaultMockedHandler('maxDuration');
TapMock.onEnd = getDefaultMockedHandler('onEnd');
TapMock.onFinalize = getDefaultMockedHandler('onFinalize');
TapMock.onTouchesDown = getDefaultMockedHandler('onTouchesDown');
return TapMock;
};
GestureHandler.Gesture.Pan = () => {
const PanMock = {
_handlers: {}
};
const getDefaultMockedHandler = handlerName => handler => {
if (typeof handler === 'function') {
PanMock._handlers[handlerName] = handler;
}
return PanMock;
};
PanMock.type = 'pan';
PanMock.onStart = getDefaultMockedHandler('onStart');
PanMock.onBegin = getDefaultMockedHandler('onBegin');
PanMock.onUpdate = getDefaultMockedHandler('onUpdate');
PanMock.onEnd = getDefaultMockedHandler('onEnd');
PanMock.onFinalize = getDefaultMockedHandler('onFinalize');
PanMock.activateAfterLongPress = getDefaultMockedHandler('activateAfterLongPress');
PanMock.enabled = getDefaultMockedHandler('enabled');
PanMock.onTouchesMove = getDefaultMockedHandler('onTouchesMove');
PanMock.prepare = jest.fn();
PanMock.initialize = jest.fn();
PanMock.toGestureArray = jest.fn(() => {
return [PanMock];
});
return PanMock;
};
try {
Object.defineProperty(GestureHandler, 'GestureDetector', {
value: require('./GestureDetectorMock').GestureDetectorMock
});
} catch {}
return GestureHandler;
},
{virtual: true});
jest.mock('react-native', () => {
const reactNative = jest.requireActual('react-native');
reactNative.NativeModules.KeyboardTrackingViewTempManager = {};
reactNative.NativeModules.StatusBarManager = {getHeight: jest.fn()};
const OriginalModal = reactNative.Modal;
const React = jest.requireActual('react');
const useDidUpdate = require('./useDidUpdate').default;
Object.defineProperty(reactNative, 'Modal', {
value: props => {
// eslint-disable-next-line react-hooks/rules-of-hooks
useDidUpdate(() => {
if (!props.visible) {
props.onDismiss?.();
}
}, [props.visible]);
return <OriginalModal {...props}/>;
}
});
return reactNative;
});
jest.mock('react-native-fs',
() => {
return {
exists: jest.fn(() => true),
readFile: jest.fn(),
downloadFile: jest.fn(),
mkdir: jest.fn()
};
},
{virtual: true});
Animated.timing = (value, config) => ({
start: callback => {
value.setValue(config.toValue);
callback?.();
}
});
Animated.parallel = () => ({
start: () => {}
});
Animated.event = () => {};
if (typeof String.prototype.replaceAll === 'undefined') {
// eslint-disable-next-line no-extend-native
String.prototype.replaceAll = function (match, replace) {
return this.split(match).join(replace);
};
}
jest.mock('../src/optionalDependencies', () => {
const actualOptional = jest.requireActual('../src/optionalDependencies');
const view = jest.requireActual('../src/components/view').default;
return {
...actualOptional,
DateTimePickerPackage: view
};
});