UNPKG

3.64 kBPlain TextView Raw
1/* eslint-disable no-undef -- jest is not defined and cannot be */
2const asyncFn = <T>(response: T) => () =>
3 jest.fn(() => {
4 return Promise.resolve(response);
5 });
6const syncFn = <T>(response: T) => () => jest.fn(() => response);
7const makeFns = <T>(response: T) => [asyncFn(response), syncFn(response)];
8
9const [stringFnAsync, stringFnSync] = makeFns('unknown');
10const [numberFnAsync, numberFnSync] = makeFns(-1);
11const [arrayFnAsync, arrayFnSync] = makeFns([]);
12const [booleanFnAsync, booleanFnSync] = makeFns(false);
13const [objectFnAsync, objectFnSync] = makeFns({});
14
15const RNDeviceInfo: any = {};
16const stringKeys = [
17 'deviceId',
18 'model',
19 'brand',
20 'systemName',
21 'systemVersion',
22 'bundleId',
23 'appName',
24 'buildNumber',
25 'appVersion',
26 'deviceType',
27];
28
29for (const key of stringKeys) {
30 RNDeviceInfo[key] = 'unknown-test';
31}
32
33const booleanKeys = ['isTablet'];
34for (const key of booleanKeys) {
35 RNDeviceInfo[key] = true;
36}
37
38RNDeviceInfo.syncUniqueId = stringFnAsync();
39RNDeviceInfo.getDeviceToken = stringFnSync();
40
41// string getters
42const 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 'getPhoneNumber',
68 'getCarrier',
69 'getInstallReferrer',
70];
71for (const name of stringFnNames) {
72 RNDeviceInfo[name] = stringFnAsync();
73 RNDeviceInfo[`${name}Sync`] = stringFnSync();
74}
75
76// boolean getters
77const booleanFnNames = [
78 'isCameraPresent',
79 'isEmulator',
80 'isPinOrFingerprintSet',
81 'isBatteryCharging',
82 'isAirplaneMode',
83 'hasSystemFeature',
84 'isLocationEnabled',
85 'isHeadphonesConnected',
86];
87for (const name of booleanFnNames) {
88 RNDeviceInfo[name] = booleanFnAsync();
89 RNDeviceInfo[`${name}Sync`] = booleanFnSync();
90}
91
92// number getters
93const numberFnNames = [
94 'getUsedMemory',
95 'getFontScale',
96 'getApiLevel',
97 'getPreviewSdkInt',
98 'getFirstInstallTime',
99 'getLastUpdateTime',
100 'getTotalMemory',
101 'getMaxMemory',
102 'getTotalDiskCapacity',
103 'getTotalDiskCapacityOld',
104 'getFreeDiskStorage',
105 'getFreeDiskStorageOld',
106 'getBatteryLevel',
107];
108for (const name of numberFnNames) {
109 RNDeviceInfo[name] = numberFnAsync();
110 RNDeviceInfo[`${name}Sync`] = numberFnSync();
111}
112
113const objectFnNames = ['getPowerState', 'getAvailableLocationProviders'];
114for (const name of objectFnNames) {
115 RNDeviceInfo[name] = objectFnAsync();
116 RNDeviceInfo[`${name}Sync`] = objectFnSync();
117}
118
119const arrayFnNames = [
120 'getSupportedAbis',
121 'getSupported32BitAbis',
122 'getSupported64BitAbis',
123 'getSystemAvailableFeatures',
124];
125for (const name of arrayFnNames) {
126 RNDeviceInfo[name] = arrayFnAsync();
127 RNDeviceInfo[`${name}Sync`] = arrayFnSync();
128}
129
130jest.mock('react-native', () => {
131 const RN = jest.requireActual('react-native'); // use original implementation, which comes with mocks out of the box
132
133 // mock modules/components created by assigning to NativeModules
134 RN.NativeModules.RNDeviceInfo = RNDeviceInfo;
135
136 type OS = typeof RN.Platform.OS;
137 jest.spyOn(RN.Platform, 'select').mockImplementation((obj: OS) => {
138 return obj[RN.Platform.OS] ?? obj.default ?? obj.android ?? obj.ios ?? obj.windows ?? obj.web;
139 });
140
141 return RN;
142});
143
144jest.mock('./src/internal/nativeInterface', () => ({ default: RNDeviceInfo }));
145
146jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');