1 |
|
2 | const asyncFn = <T>(response: T) => () =>
|
3 | jest.fn(() => {
|
4 | return Promise.resolve(response);
|
5 | });
|
6 | const syncFn = <T>(response: T) => () => jest.fn(() => response);
|
7 | const makeFns = <T>(response: T) => [asyncFn(response), syncFn(response)];
|
8 |
|
9 | const [stringFnAsync, stringFnSync] = makeFns('unknown');
|
10 | const [numberFnAsync, numberFnSync] = makeFns(-1);
|
11 | const [arrayFnAsync, arrayFnSync] = makeFns([]);
|
12 | const [booleanFnAsync, booleanFnSync] = makeFns(false);
|
13 | const [objectFnAsync, objectFnSync] = makeFns({});
|
14 |
|
15 | const RNDeviceInfo: any = {};
|
16 | const stringKeys = [
|
17 | 'deviceId',
|
18 | 'model',
|
19 | 'brand',
|
20 | 'systemName',
|
21 | 'systemVersion',
|
22 | 'bundleId',
|
23 | 'appName',
|
24 | 'buildNumber',
|
25 | 'appVersion',
|
26 | 'deviceType',
|
27 | ];
|
28 |
|
29 | for (const key of stringKeys) {
|
30 | RNDeviceInfo[key] = 'unknown-test';
|
31 | }
|
32 |
|
33 | const booleanKeys = ['isTablet'];
|
34 | for (const key of booleanKeys) {
|
35 | RNDeviceInfo[key] = true;
|
36 | }
|
37 |
|
38 | RNDeviceInfo.syncUniqueId = stringFnAsync();
|
39 | RNDeviceInfo.getDeviceToken = stringFnSync();
|
40 |
|
41 |
|
42 | const stringFnNames = [
|
43 | 'getUniqueId',
|
44 | 'getInstanceId',
|
45 | 'getSerialNumber',
|
46 | 'getAndroidId',
|
47 | 'getIpAddress',
|
48 | 'getMacAddress',
|
49 | 'getSystemManufacturer',
|
50 | 'getBuildId',
|
51 | 'getInstallerPackageName',
|
52 | 'getDeviceName',
|
53 | 'getUserAgent',
|
54 | 'getBootloader',
|
55 | 'getDevice',
|
56 | 'getDisplay',
|
57 | 'getFingerprint',
|
58 | 'getHardware',
|
59 | 'getHost',
|
60 | 'getProduct',
|
61 | 'getTags',
|
62 | 'getType',
|
63 | 'getBaseOs',
|
64 | 'getSecurityPatch',
|
65 | 'getCodename',
|
66 | 'getIncremental',
|
67 | 'getCarrier',
|
68 | 'getInstallReferrer',
|
69 | ];
|
70 | for (const name of stringFnNames) {
|
71 | RNDeviceInfo[name] = stringFnAsync();
|
72 | RNDeviceInfo[`${name}Sync`] = stringFnSync();
|
73 | }
|
74 |
|
75 |
|
76 | const booleanFnNames = [
|
77 | 'isCameraPresent',
|
78 | 'isEmulator',
|
79 | 'isPinOrFingerprintSet',
|
80 | 'isBatteryCharging',
|
81 | 'isAirplaneMode',
|
82 | 'hasSystemFeature',
|
83 | 'isLocationEnabled',
|
84 | 'isHeadphonesConnected',
|
85 | 'isWiredHeadphonesConnected',
|
86 | 'isBluetoothHeadphonesConnected',
|
87 | ];
|
88 | for (const name of booleanFnNames) {
|
89 | RNDeviceInfo[name] = booleanFnAsync();
|
90 | RNDeviceInfo[`${name}Sync`] = booleanFnSync();
|
91 | }
|
92 |
|
93 |
|
94 | const numberFnNames = [
|
95 | 'getUsedMemory',
|
96 | 'getFontScale',
|
97 | 'getApiLevel',
|
98 | 'getPreviewSdkInt',
|
99 | 'getFirstInstallTime',
|
100 | 'getLastUpdateTime',
|
101 | 'getTotalMemory',
|
102 | 'getMaxMemory',
|
103 | 'getTotalDiskCapacity',
|
104 | 'getTotalDiskCapacityOld',
|
105 | 'getFreeDiskStorage',
|
106 | 'getFreeDiskStorageOld',
|
107 | 'getBatteryLevel',
|
108 | ];
|
109 | for (const name of numberFnNames) {
|
110 | RNDeviceInfo[name] = numberFnAsync();
|
111 | RNDeviceInfo[`${name}Sync`] = numberFnSync();
|
112 | }
|
113 |
|
114 | const objectFnNames = ['getPowerState', 'getAvailableLocationProviders'];
|
115 | for (const name of objectFnNames) {
|
116 | RNDeviceInfo[name] = objectFnAsync();
|
117 | RNDeviceInfo[`${name}Sync`] = objectFnSync();
|
118 | }
|
119 |
|
120 | const arrayFnNames = [
|
121 | 'getSupportedAbis',
|
122 | 'getSupported32BitAbis',
|
123 | 'getSupported64BitAbis',
|
124 | 'getSystemAvailableFeatures',
|
125 | ];
|
126 | for (const name of arrayFnNames) {
|
127 | RNDeviceInfo[name] = arrayFnAsync();
|
128 | RNDeviceInfo[`${name}Sync`] = arrayFnSync();
|
129 | }
|
130 |
|
131 | jest.mock('react-native', () => {
|
132 | const RN = jest.requireActual('react-native');
|
133 |
|
134 |
|
135 | RN.NativeModules.RNDeviceInfo = RNDeviceInfo;
|
136 |
|
137 | type OS = typeof RN.Platform.OS;
|
138 | jest.spyOn(RN.Platform, 'select').mockImplementation((obj: OS) => {
|
139 | return obj[RN.Platform.OS] ?? obj.default ?? obj.android ?? obj.ios ?? obj.windows ?? obj.web;
|
140 | });
|
141 |
|
142 | return RN;
|
143 | });
|
144 |
|
145 | jest.mock('./src/internal/nativeInterface', () => ({ default: RNDeviceInfo }));
|
146 |
|
147 | jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');
|