-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.setup.ts
147 lines (131 loc) · 3.6 KB
/
jest.setup.ts
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
/* eslint-disable no-undef -- jest is not defined and cannot be */
const asyncFn = <T>(response: T) => () =>
jest.fn(() => {
return Promise.resolve(response);
});
const syncFn = <T>(response: T) => () => jest.fn(() => response);
const makeFns = <T>(response: T) => [asyncFn(response), syncFn(response)];
const [stringFnAsync, stringFnSync] = makeFns('unknown');
const [numberFnAsync, numberFnSync] = makeFns(-1);
const [arrayFnAsync, arrayFnSync] = makeFns([]);
const [booleanFnAsync, booleanFnSync] = makeFns(false);
const [objectFnAsync, objectFnSync] = makeFns({});
const RNDeviceInfo: any = {};
const stringKeys = [
'deviceId',
'model',
'brand',
'systemName',
'systemVersion',
'bundleId',
'appName',
'buildNumber',
'appVersion',
'deviceType',
];
for (const key of stringKeys) {
RNDeviceInfo[key] = 'unknown-test';
}
const booleanKeys = ['isTablet'];
for (const key of booleanKeys) {
RNDeviceInfo[key] = true;
}
RNDeviceInfo.syncUniqueId = stringFnAsync();
RNDeviceInfo.getDeviceToken = stringFnSync();
// string getters
const stringFnNames = [
'getUniqueId',
'getInstanceId',
'getSerialNumber',
'getAndroidId',
'getIpAddress',
'getMacAddress',
'getSystemManufacturer',
'getBuildId',
'getInstallerPackageName',
'getDeviceName',
'getUserAgent',
'getBootloader',
'getDevice',
'getDisplay',
'getFingerprint',
'getHardware',
'getHost',
'getProduct',
'getTags',
'getType',
'getBaseOs',
'getSecurityPatch',
'getCodename',
'getIncremental',
'getCarrier',
'getInstallReferrer',
];
for (const name of stringFnNames) {
RNDeviceInfo[name] = stringFnAsync();
RNDeviceInfo[`${name}Sync`] = stringFnSync();
}
// boolean getters
const booleanFnNames = [
'isCameraPresent',
'isEmulator',
'isPinOrFingerprintSet',
'isBatteryCharging',
'isAirplaneMode',
'hasSystemFeature',
'isLocationEnabled',
'isHeadphonesConnected',
'isWiredHeadphonesConnected',
'isBluetoothHeadphonesConnected',
];
for (const name of booleanFnNames) {
RNDeviceInfo[name] = booleanFnAsync();
RNDeviceInfo[`${name}Sync`] = booleanFnSync();
}
// number getters
const numberFnNames = [
'getUsedMemory',
'getFontScale',
'getApiLevel',
'getPreviewSdkInt',
'getFirstInstallTime',
'getLastUpdateTime',
'getTotalMemory',
'getMaxMemory',
'getTotalDiskCapacity',
'getTotalDiskCapacityOld',
'getFreeDiskStorage',
'getFreeDiskStorageOld',
'getBatteryLevel',
];
for (const name of numberFnNames) {
RNDeviceInfo[name] = numberFnAsync();
RNDeviceInfo[`${name}Sync`] = numberFnSync();
}
const objectFnNames = ['getPowerState', 'getAvailableLocationProviders'];
for (const name of objectFnNames) {
RNDeviceInfo[name] = objectFnAsync();
RNDeviceInfo[`${name}Sync`] = objectFnSync();
}
const arrayFnNames = [
'getSupportedAbis',
'getSupported32BitAbis',
'getSupported64BitAbis',
'getSystemAvailableFeatures',
];
for (const name of arrayFnNames) {
RNDeviceInfo[name] = arrayFnAsync();
RNDeviceInfo[`${name}Sync`] = arrayFnSync();
}
jest.mock('react-native', () => {
const RN = jest.requireActual('react-native'); // use original implementation, which comes with mocks out of the box
// mock modules/components created by assigning to NativeModules
RN.NativeModules.RNDeviceInfo = RNDeviceInfo;
type OS = typeof RN.Platform.OS;
jest.spyOn(RN.Platform, 'select').mockImplementation((obj: OS) => {
return obj[RN.Platform.OS] ?? obj.default ?? obj.android ?? obj.ios ?? obj.windows ?? obj.web;
});
return RN;
});
jest.mock('./src/internal/nativeInterface', () => ({ default: RNDeviceInfo }));
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');