-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjest.setup.js
More file actions
136 lines (121 loc) · 3.87 KB
/
Copy pathjest.setup.js
File metadata and controls
136 lines (121 loc) · 3.87 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
import {jest, afterAll} from '@jest/globals';
import 'react-native-gesture-handler/jestSetup';
import mockNetInfo from '@react-native-community/netinfo/jest/netinfo-mock.js';
import mockSafeAreaContext from 'react-native-safe-area-context/jest/mock';
import mockRNDeviceInfo from 'react-native-device-info/jest/react-native-device-info-mock';
import {randomBytes} from 'node:crypto';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import * as url from 'node:url';
import {NativeModules} from 'react-native';
import {setUpTests} from 'react-native-reanimated';
setUpTests();
// Module resolution error when using expo/winter. See: https://github.com/expo/expo/issues/36831#issuecomment-3107047371
jest.mock('expo/src/winter/ImportMetaRegistry', () => ({
ImportMetaRegistry: {
get url() {
return null;
},
},
}));
jest.mock('react-native-nitro-modules', () => {});
jest.mock('expo-audio', () => {
const grantedPermission = {
status: 'granted',
expires: 'never',
granted: true,
canAskAgain: true,
};
const mockRecorder = {
prepareToRecordAsync: jest.fn(() => Promise.resolve()),
record: jest.fn(),
stop: jest.fn(() => Promise.resolve()),
uri: null,
};
return {
getRecordingPermissionsAsync: jest.fn(() =>
Promise.resolve(grantedPermission),
),
requestRecordingPermissionsAsync: jest.fn(() =>
Promise.resolve(grantedPermission),
),
useAudioRecorder: jest.fn(() => mockRecorder),
useAudioRecorderState: jest.fn(() => ({
isRecording: false,
durationMillis: 0,
})),
useAudioPlayer: jest.fn(() => ({
play: jest.fn(),
pause: jest.fn(),
seekTo: jest.fn(),
})),
useAudioPlayerStatus: jest.fn(() => ({
isLoaded: false,
isPlaying: false,
currentTime: 0,
duration: 0,
})),
RecordingPresets: {
HIGH_QUALITY: {ios: {}, android: {}, web: {}},
LOW_QUALITY: {ios: {}, android: {}, web: {}},
},
};
});
jest.mock('@lodev09/react-native-exify', () => ({
read: jest.fn(() => Promise.resolve(null)),
}));
jest.mock('@react-native-community/netinfo', () => mockNetInfo);
jest.mock('react-native-safe-area-context', () => mockSafeAreaContext);
jest.mock('react-native-device-info', () => mockRNDeviceInfo);
// https://github.com/callstack/react-native-testing-library/issues/1712
jest.mock('expo-font', () => {
/** @type {import('expo-font')} */
const module = {
...jest.requireActual('expo-font'),
isLoaded: jest.fn(() => true),
};
return module;
});
// Native modules won't work in tests.
NativeModules.FlagSecureModule = {
activate: () => {},
deactivate: () => {},
};
// MLRNModule crashes at load time when NativeModules.MLRNModule is absent.
jest.mock('@maplibre/maplibre-react-native', () => ({
MapView: 'MapView',
Camera: 'Camera',
MarkerView: 'MarkerView',
UserLocation: 'UserLocation',
ShapeSource: 'ShapeSource',
LineLayer: 'LineLayer',
setAccessToken: jest.fn(),
setTelemetryEnabled: jest.fn(),
LineJoin: {Round: 'round', Bevel: 'bevel', Miter: 'miter'},
LineCap: {Round: 'round', Butt: 'butt', Square: 'square'},
}));
jest.mock('react-native-vision-camera', () => ({
Camera: 'Camera',
useCameraDevice: jest.fn(() => undefined),
useCameraPermission: jest.fn(() => ({
hasPermission: true,
requestPermission: jest.fn(() => Promise.resolve(true)),
})),
}));
function temporaryDirectory() {
const result = path.join(
os.tmpdir(),
'comapeo-mobile-tests-' + randomBytes(16).toString('hex'),
);
fs.mkdirSync(result);
afterAll(async () => {
await fs.promises.rm(result, {force: true, recursive: true, maxRetries: 3});
});
return result;
}
// `expo-file-system` is already mocked, but we need to extend it.
const FileSystem = jest.requireMock('expo-file-system');
FileSystem.documentDirectory = url
.pathToFileURL(temporaryDirectory())
.toString();