Repository navigation
Expand file tree
/
Copy pathjest.setup.js
More file actions
57 lines (51 loc) · 1.82 KB
/
Copy pathjest.setup.js
File metadata and controls
57 lines (51 loc) · 1.82 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
/**
* Registers the native-module stubs the app's native dependencies look up at
* import time. Without these the libraries throw while their JS is still being
* evaluated, so no component test can even mount the app.
*
* Only the native bridge is stubbed here — the libraries' own JavaScript still
* runs, so tests exercise the real code paths above the bridge.
*/
const { NativeModules } = require('react-native');
const eventEmitterStub = {
addListener: jest.fn(),
removeListeners: jest.fn(),
};
NativeModules.RNFSManager = {
...eventEmitterStub,
RNFSFileTypeRegular: 0,
RNFSFileTypeDirectory: 1,
RNFSMainBundlePath: '/main-bundle',
RNFSCachesDirectoryPath: '/caches',
RNFSDocumentDirectoryPath: '/documents',
RNFSTemporaryDirectoryPath: '/tmp',
RNFSExternalDirectoryPath: '/external',
RNFSExternalStorageDirectoryPath: '/external-storage',
RNFSLibraryDirectoryPath: '/library',
RNFSDownloadDirectoryPath: '/downloads',
RNFSPicturesDirectoryPath: '/pictures',
};
/**
* Bridges whose surface is large enough that listing every method by hand
* would rot: any method the library reaches for resolves to a jest.fn().
*/
const nativeBridgeStub = () =>
new Proxy(
{ ...eventEmitterStub },
{
get(target, prop) {
if (!(prop in target) && typeof prop === 'string') {
target[prop] = jest.fn();
}
return target[prop];
},
},
);
NativeModules.BlePlx = nativeBridgeStub();
NativeModules.RNZipArchive = nativeBridgeStub();
NativeModules.RNDocumentPicker = nativeBridgeStub();
// AsyncStorage ships its own functional in-memory jest mock; use that rather
// than a hand-rolled stub so storage reads and writes still behave.
jest.mock('@react-native-async-storage/async-storage', () =>
require('@react-native-async-storage/async-storage/jest/async-storage-mock'),
);