UNPKG

3.69 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 'getCarrier',
68 'getInstallReferrer',
69];
70for (const name of stringFnNames) {
71 RNDeviceInfo[name] = stringFnAsync();
72 RNDeviceInfo[`${name}Sync`] = stringFnSync();
73}
74
75// boolean getters
76const booleanFnNames = [
77 'isCameraPresent',
78 'isEmulator',
79 'isPinOrFingerprintSet',
80 'isBatteryCharging',
81 'isAirplaneMode',
82 'hasSystemFeature',
83 'isLocationEnabled',
84 'isHeadphonesConnected',
85 'isWiredHeadphonesConnected',
86 'isBluetoothHeadphonesConnected',
87];
88for (const name of booleanFnNames) {
89 RNDeviceInfo[name] = booleanFnAsync();
90 RNDeviceInfo[`${name}Sync`] = booleanFnSync();
91}
92
93// number getters
94const 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];
109for (const name of numberFnNames) {
110 RNDeviceInfo[name] = numberFnAsync();
111 RNDeviceInfo[`${name}Sync`] = numberFnSync();
112}
113
114const objectFnNames = ['getPowerState', 'getAvailableLocationProviders'];
115for (const name of objectFnNames) {
116 RNDeviceInfo[name] = objectFnAsync();
117 RNDeviceInfo[`${name}Sync`] = objectFnSync();
118}
119
120const arrayFnNames = [
121 'getSupportedAbis',
122 'getSupported32BitAbis',
123 'getSupported64BitAbis',
124 'getSystemAvailableFeatures',
125];
126for (const name of arrayFnNames) {
127 RNDeviceInfo[name] = arrayFnAsync();
128 RNDeviceInfo[`${name}Sync`] = arrayFnSync();
129}
130
131jest.mock('react-native', () => {
132 const RN = jest.requireActual('react-native'); // use original implementation, which comes with mocks out of the box
133
134 // mock modules/components created by assigning to NativeModules
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
145jest.mock('./src/internal/nativeInterface', () => ({ default: RNDeviceInfo }));
146
147jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');