-
-
Notifications
You must be signed in to change notification settings - Fork 638
/
jestSetup.js
90 lines (82 loc) · 3.14 KB
/
jestSetup.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
import mockAsyncStorage from '@react-native-community/async-storage/jest/async-storage-mock';
import * as ReactNative from 'react-native';
// Following upstream advice [1], mock `react-native` ourselves.
//
// Helpful in particular for adding to NativeModules, to simulate
// modules being available after the bridge initializes.
//
// The 'react-native' we import here is already mocked to a great
// extent by `node_modules/react-native-jest/setup.js`, thanks to our
// having declared `preset: 'react-native'` in our Jest config.
//
// For how we fall back on these already-mocked properties:
//
// Following the suggested implementation, we don't do something like
// an object spread, but instead set the returned object's prototype
// to be ReactNative. They don't explain why, but at least one reason
// seems to be to avoid triggering deprecation warnings on accessing,
// e.g., ReactNative.CameraRoll, which would would be done during a
// spread as each property is enumerated.
//
// [1] https://github.com/facebook/react-native/issues/26579#issuecomment-535244001
jest.mock('react-native', () =>
// Extend ReactNative
Object.setPrototypeOf(
{
// Boring RN mocks. Adding something directly from RN here, as
// in `Foo: ReactNative.Foo`, is not done because we want to
// mock Foo ourselves.
//
// One reason to have to do this is because of an odd
// indirection in `react-native-vector-icons`. They re-export
// everything from RN, in their own `lib/react-native.js`:
//
// `export * from 'react-native';`
//
// When some code in `react-native-vector-icons` tries to access
// Foo (when Foo isn't included in the object literal here, even
// when it's in ReactNative), it fails. We suppose the
// re-export prevents the prototype from being accessed. So,
// include Foo here.
Platform: ReactNative.Platform,
StyleSheet: ReactNative.StyleSheet,
NativeModules: ReactNative.NativeModules,
},
ReactNative,
),
);
/**
* Boring mocks
*
* We aren't interested in any specific data in these mocks; they just
* make things not break. Usually it's because these JS modules depend
* on something being available on NativeModules, and we'd rather not
* mock that precisely, as it's an implementation detail that may
* change.
*
* If Jest complains about syntax errors in a module in node_modules,
* it's likely because the only code it finds for the module is modern
* JavaScript that needs to be transformed by Babel. In that case, try
* adding the module to `transformModulesWhitelist` in our Jest config
* before mocking it here.
*/
jest.mock('@react-native-community/async-storage', () => mockAsyncStorage);
jest.mock('react-native-sound', () => () => ({
play: jest.fn(),
}));
jest.mock('rn-fetch-blob', () => ({
DocumentDir: () => {},
}));
jest.mock('react-native-simple-toast', () => ({
SHORT: 2.0,
LONG: 3.5,
TOP: 3,
BOTTOM: 1,
CENTER: 2,
show: jest.fn(),
showWithGravity: jest.fn(),
}));
jest.mock('react-native-device-info', () => ({
getSystemName: jest.fn().mockReturnValue('ios'),
getSystemVersion: jest.fn().mockReturnValue('13.3.1'),
}));