UNPKG

27.7 kBPlain TextView Raw
1import { Platform } from 'react-native';
2import * as RNDeviceInfo from '../src';
3import { clearMemo } from '../src/internal/supported-platform-info';
4let mockNativeModule = jest.requireMock('../src/internal/nativeInterface').default;
5
6function makeTable(name: string) {
7 return [
8 name, // name
9 (RNDeviceInfo as any)[name], // asyncGetter
10 (RNDeviceInfo as any)[`${name}Sync`], // syncGetter
11 mockNativeModule[name], // asyncNativeGetter
12 mockNativeModule[`${name}Sync`], // syncNativeGetter
13 ];
14}
15const memoizedStringGetters = [
16 'getUniqueId',
17 'getInstanceId',
18 'getSerialNumber',
19 'getAndroidId',
20 'getBuildId',
21 'getInstallerPackageName',
22 'getBootloader',
23 'getDevice',
24 'getDisplay',
25 'getFingerprint',
26 'getHardware',
27 'getHost',
28 'getProduct',
29 'getTags',
30 'getType',
31 'getBaseOs',
32 'getSecurityPatch',
33 'getCodename',
34 'getIncremental',
35 'getInstallReferrer',
36].map(makeTable);
37memoizedStringGetters.push([
38 'getManufacturer',
39 RNDeviceInfo.getManufacturer,
40 RNDeviceInfo.getManufacturerSync,
41 mockNativeModule.getSystemManufacturer,
42 mockNativeModule.getSystemManufacturerSync,
43]);
44
45const nonMemoizedStringGetters = [
46 'getMacAddress',
47 'getIpAddress',
48 'getDeviceName',
49 'getPhoneNumber',
50 'getCarrier',
51].map(makeTable);
52
53describe('string getters', () => {
54 describe.each(memoizedStringGetters)(
55 '%s*',
56 (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => {
57 beforeEach(() => {
58 clearMemo();
59 Platform.OS = 'android';
60 asyncNativeGetter.mockClear();
61 syncNativeGetter.mockClear();
62 });
63
64 it('should have an async version', () => {
65 expect(typeof asyncGetter).toBe('function');
66 });
67
68 it('should have a sync version', () => {
69 expect(typeof syncGetter).toBe('function');
70 });
71
72 it('should call native async module function', async () => {
73 const resp = await asyncGetter();
74 expect(resp).toEqual('unknown');
75 expect(asyncNativeGetter).toHaveBeenCalled();
76 });
77
78 it('should call native sync module function', () => {
79 const resp = syncGetter();
80 expect(resp).toEqual('unknown');
81 expect(syncNativeGetter).toHaveBeenCalled();
82 });
83
84 it('should not call native sync module function on unsupported OS', () => {
85 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
86 const resp = syncGetter();
87 expect(resp).toEqual('unknown');
88 expect(syncNativeGetter).not.toHaveBeenCalled();
89 });
90
91 it('should not call native async module function on unsupported OS', async () => {
92 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
93 const resp = await asyncGetter();
94 expect(resp).toEqual('unknown');
95 expect(asyncNativeGetter).not.toHaveBeenCalled();
96 });
97
98 it('should use memoized value if there exists one', async () => {
99 const resp = await asyncGetter();
100 const resp2 = syncGetter();
101 expect(resp).toBe(resp2);
102 expect(asyncNativeGetter).toHaveBeenCalled();
103 expect(syncNativeGetter).not.toHaveBeenCalled();
104 });
105 }
106 );
107
108 describe.each(nonMemoizedStringGetters)(
109 '%s*',
110 (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => {
111 beforeEach(() => {
112 clearMemo();
113 Platform.OS = 'android';
114 asyncNativeGetter.mockClear();
115 syncNativeGetter.mockClear();
116 });
117
118 it('should have an async version', () => {
119 expect(typeof asyncGetter).toBe('function');
120 });
121
122 it('should have a sync version', () => {
123 expect(typeof syncGetter).toBe('function');
124 });
125
126 it('should call native async module function', async () => {
127 const resp = await asyncGetter();
128 expect(resp).toEqual('unknown');
129 expect(asyncNativeGetter).toHaveBeenCalled();
130 });
131
132 it('should call native sync module function', () => {
133 const resp = syncGetter();
134 expect(resp).toEqual('unknown');
135 expect(syncNativeGetter).toHaveBeenCalled();
136 });
137
138 it('should not call native sync module function on unsupported OS', () => {
139 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
140 const resp = syncGetter();
141 expect(resp).toEqual('unknown');
142 expect(syncNativeGetter).not.toHaveBeenCalled();
143 });
144
145 it('should not call native async module function on unsupported OS', async () => {
146 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
147 const resp = await asyncGetter();
148 expect(resp).toEqual('unknown');
149 expect(asyncNativeGetter).not.toHaveBeenCalled();
150 });
151 }
152 );
153
154 const iosAndroidWindows = [
155 'getDeviceId',
156 'getModel',
157 'getBrand',
158 'getSystemVersion',
159 'getBundleId',
160 'getApplicationName',
161 'getBuildNumber',
162 'getVersion',
163 ].map((name) => [name, (RNDeviceInfo as any)[name]]);
164
165 describe.each(iosAndroidWindows)('%s*', (_name, fn) => {
166 beforeEach(() => {
167 clearMemo();
168 Platform.OS = 'android';
169 });
170 it('should exists as a function', () => {
171 expect(typeof fn).toEqual('function');
172 });
173
174 it.each(['ios', 'android', 'windows'])('supports %s', (os) => {
175 Platform.OS = os as any;
176 expect(fn()).toEqual('unknown-test');
177 });
178 });
179
180 const iosAndroid = ['getDeviceType', 'getDeviceTypeSync'].map((name) => [
181 name,
182 (RNDeviceInfo as any)[name],
183 ]);
184
185 describe.each(iosAndroid)('%s*', (_name, fn) => {
186 beforeEach(() => {
187 clearMemo();
188 Platform.OS = 'android';
189 });
190 it('should exists as a function', () => {
191 expect(typeof fn).toEqual('function');
192 });
193
194 it.each(['ios', 'android'])('supports %s', (os) => {
195 Platform.OS = os as any;
196 expect(fn()).toEqual('unknown-test');
197 });
198 });
199
200 describe('getMacAddress*', () => {
201 beforeEach(() => {
202 clearMemo();
203 Platform.OS = 'ios';
204 mockNativeModule.getMacAddress.mockClear();
205 mockNativeModule.getMacAddressSync.mockClear();
206 });
207
208 it('should return expected value for iOS (async)', async () => {
209 const resp = await RNDeviceInfo.getMacAddress();
210 expect(resp).toEqual('02:00:00:00:00:00');
211 });
212
213 it('should not use native module for iOS (async)', async () => {
214 await RNDeviceInfo.getMacAddress();
215 expect(mockNativeModule.getMacAddress).not.toHaveBeenCalled();
216 });
217
218 it('should return expected value for iOS (sync)', () => {
219 const resp = RNDeviceInfo.getMacAddressSync();
220 expect(resp).toEqual('02:00:00:00:00:00');
221 });
222
223 it('should not use native module for iOS (sync)', () => {
224 RNDeviceInfo.getMacAddressSync();
225 expect(mockNativeModule.getMacAddressSync).not.toHaveBeenCalled();
226 });
227 });
228
229 describe('getManufacturer*', () => {
230 beforeEach(() => {
231 clearMemo();
232 Platform.OS = 'ios';
233 mockNativeModule.getSystemManufacturer.mockClear();
234 mockNativeModule.getSystemManufacturerSync.mockClear();
235 });
236
237 it('should return expected value for iOS (async)', async () => {
238 const resp = await RNDeviceInfo.getManufacturer();
239 expect(resp).toEqual('Apple');
240 });
241
242 it('should not use native module for iOS (async)', async () => {
243 await RNDeviceInfo.getManufacturer();
244 expect(mockNativeModule.getSystemManufacturer).not.toHaveBeenCalled();
245 });
246
247 it('should return expected value for iOS (sync)', () => {
248 const resp = RNDeviceInfo.getManufacturerSync();
249 expect(resp).toEqual('Apple');
250 });
251
252 it('should not use native module for iOS (sync)', () => {
253 RNDeviceInfo.getManufacturerSync();
254 expect(mockNativeModule.getSystemManufacturerSync).not.toHaveBeenCalled();
255 });
256 });
257
258 describe('getDeviceToken', () => {
259 beforeEach(() => {
260 clearMemo();
261 Platform.OS = 'ios';
262 mockNativeModule.getDeviceToken.mockClear();
263 });
264
265 it('should exist as a function', () => {
266 expect(typeof RNDeviceInfo.getDeviceToken).toBe('function');
267 });
268
269 it.each(['ios'])('should call native module for supported OS, %s', async (os) => {
270 Platform.OS = os as any;
271 await RNDeviceInfo.getDeviceToken();
272 expect(mockNativeModule.getDeviceToken).toHaveBeenCalled();
273 });
274
275 it.each(['android', 'windows', 'web'])(
276 'should return default value for unsupported OS, %s',
277 async (os) => {
278 Platform.OS = os as any;
279 expect(await RNDeviceInfo.getDeviceToken()).toEqual('unknown');
280 expect(mockNativeModule.getDeviceToken).not.toHaveBeenCalled();
281 }
282 );
283 });
284
285 describe('getUserAgent', () => {
286 const getter = RNDeviceInfo.getUserAgent;
287 const nativeGetter = mockNativeModule.getUserAgent;
288
289 const supportedPlatforms = ['android', 'ios', 'web'];
290
291 beforeEach(() => {
292 clearMemo();
293 nativeGetter.mockClear();
294 });
295
296 it('should exist as a function', () => {
297 expect(typeof getter).toBe('function');
298 });
299
300 it.each(supportedPlatforms)(
301 'should call native async module function for supported platform, %s',
302 async (platform) => {
303 Platform.OS = platform as any;
304 const resp = await getter();
305 expect(resp).toEqual('unknown');
306 expect(nativeGetter).toHaveBeenCalled();
307 }
308 );
309
310 it('should not call native module function on unsupported OS', async () => {
311 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
312 const resp = await getter();
313 expect(resp).toEqual('unknown');
314 expect(nativeGetter).not.toHaveBeenCalled();
315 });
316 });
317
318 describe('getUserAgentSync', () => {
319 const getter = RNDeviceInfo.getUserAgentSync;
320 const nativeGetter = mockNativeModule.getUserAgentSync;
321
322 const supportedPlatforms = ['android', 'web'];
323
324 beforeEach(() => {
325 clearMemo();
326 nativeGetter.mockClear();
327 });
328
329 it('should exist as a function', () => {
330 expect(typeof getter).toBe('function');
331 });
332
333 it.each(supportedPlatforms)(
334 'should call native async module function for supported platform, %s',
335 (platform) => {
336 Platform.OS = platform as any;
337 const resp = getter();
338 expect(resp).toEqual('unknown');
339 expect(nativeGetter).toHaveBeenCalled();
340 }
341 );
342
343 it('should not call native module function on unsupported OS', () => {
344 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
345 const resp = getter();
346 expect(resp).toEqual('unknown');
347 expect(nativeGetter).not.toHaveBeenCalled();
348 });
349 });
350
351 describe('getSystemName', () => {
352 const getter = RNDeviceInfo.getSystemName;
353 const supportedPlatforms = [
354 ['ios', mockNativeModule.systemName],
355 ['android', 'Android'],
356 ['windows', 'Windows'],
357 ];
358
359 beforeEach(() => {
360 clearMemo();
361 });
362
363 it('should exist as a function', () => {
364 expect(typeof getter).toBe('function');
365 });
366
367 it.each(supportedPlatforms)(
368 'should call native async module function for supported platform, %s',
369 (platform, value) => {
370 Platform.OS = platform as any;
371 const resp = getter();
372 expect(resp).toEqual(value);
373 }
374 );
375
376 it('should not call native module function on unsupported OS', () => {
377 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
378 const resp = getter();
379 expect(resp).toEqual('unknown');
380 });
381 });
382});
383
384const memoizedNumberGetters = [
385 'getApiLevel',
386 'getPreviewSdkInt',
387 'getFirstInstallTime',
388 'getLastUpdateTime',
389 'getTotalMemory',
390 'getMaxMemory',
391].map(makeTable);
392const nonMemoizedNumberGetters = [
393 'getUsedMemory',
394 'getFontScale',
395 'getFreeDiskStorage',
396 'getBatteryLevel',
397 'getTotalDiskCapacity',
398].map(makeTable);
399
400describe('number getters', () => {
401 describe.each(memoizedNumberGetters)(
402 '%s*',
403 (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => {
404 beforeEach(() => {
405 clearMemo();
406 Platform.OS = 'android';
407 asyncNativeGetter.mockClear();
408 syncNativeGetter.mockClear();
409 });
410
411 it('should have an async version', () => {
412 expect(typeof asyncGetter).toBe('function');
413 });
414
415 it('should have a sync version', () => {
416 expect(typeof syncGetter).toBe('function');
417 });
418
419 it('should call native async module function', async () => {
420 const resp = await asyncGetter();
421 expect(resp).toEqual(-1);
422 expect(asyncNativeGetter).toHaveBeenCalled();
423 });
424
425 it('should call native sync module function', () => {
426 const resp = syncGetter();
427 expect(resp).toEqual(-1);
428 expect(syncNativeGetter).toHaveBeenCalled();
429 });
430
431 it('should not call native sync module function on unsupported OS', () => {
432 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
433 const resp = syncGetter();
434 expect(resp).toEqual(-1);
435 expect(syncNativeGetter).not.toHaveBeenCalled();
436 });
437
438 it('should not call native async module function on unsupported OS', async () => {
439 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
440 const resp = await asyncGetter();
441 expect(resp).toEqual(-1);
442 expect(asyncNativeGetter).not.toHaveBeenCalled();
443 });
444
445 it('should use memoized value if there exists one', async () => {
446 const resp = await asyncGetter();
447 const resp2 = syncGetter();
448 expect(resp).toBe(resp2);
449 expect(asyncNativeGetter).toHaveBeenCalled();
450 expect(syncNativeGetter).not.toHaveBeenCalled();
451 });
452 }
453 );
454
455 describe.each(nonMemoizedNumberGetters)(
456 '%s*',
457 (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => {
458 beforeEach(() => {
459 clearMemo();
460 Platform.OS = 'android';
461 asyncNativeGetter.mockClear();
462 syncNativeGetter.mockClear();
463 });
464
465 it('should have an async version', () => {
466 expect(typeof asyncGetter).toBe('function');
467 });
468
469 it('should have a sync version', () => {
470 expect(typeof syncGetter).toBe('function');
471 });
472
473 it('should call native async module function', async () => {
474 const resp = await asyncGetter();
475 expect(resp).toEqual(-1);
476 expect(asyncNativeGetter).toHaveBeenCalled();
477 });
478
479 it('should call native sync module function', () => {
480 const resp = syncGetter();
481 expect(resp).toEqual(-1);
482 expect(syncNativeGetter).toHaveBeenCalled();
483 });
484
485 it('should not call native sync module function on unsupported OS', () => {
486 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
487 const resp = syncGetter();
488 expect(resp).toEqual(-1);
489 expect(syncNativeGetter).not.toHaveBeenCalled();
490 });
491
492 it('should not call native async module function on unsupported OS', async () => {
493 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
494 const resp = await asyncGetter();
495 expect(resp).toEqual(-1);
496 expect(asyncNativeGetter).not.toHaveBeenCalled();
497 });
498 }
499 );
500});
501
502const memoizedBooleanGetters = ['isEmulator'].map(makeTable);
503const nonMemoizedBooleanGetters = [
504 'isCameraPresent',
505 'isPinOrFingerprintSet',
506 'isBatteryCharging',
507 'isAirplaneMode',
508 'isLocationEnabled',
509 'isHeadphonesConnected',
510].map(makeTable);
511
512describe('boolean getters', () => {
513 describe.each(memoizedBooleanGetters)(
514 '%s*',
515 (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => {
516 beforeEach(() => {
517 clearMemo();
518 Platform.OS = 'android';
519 asyncNativeGetter.mockClear();
520 syncNativeGetter.mockClear();
521 });
522
523 it('should have an async version', () => {
524 expect(typeof asyncGetter).toBe('function');
525 });
526
527 it('should have a sync version', () => {
528 expect(typeof syncGetter).toBe('function');
529 });
530
531 it('should call native async module function', async () => {
532 const resp = await asyncGetter();
533 expect(resp).toEqual(false);
534 expect(asyncNativeGetter).toHaveBeenCalled();
535 });
536
537 it('should call native sync module function', () => {
538 const resp = syncGetter();
539 expect(resp).toEqual(false);
540 expect(syncNativeGetter).toHaveBeenCalled();
541 });
542
543 it('should not call native sync module function on unsupported OS', () => {
544 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
545 const resp = syncGetter();
546 expect(resp).toEqual(false);
547 expect(syncNativeGetter).not.toHaveBeenCalled();
548 });
549
550 it('should not call native async module function on unsupported OS', async () => {
551 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
552 const resp = await asyncGetter();
553 expect(resp).toEqual(false);
554 expect(asyncNativeGetter).not.toHaveBeenCalled();
555 });
556
557 it('should use memoized value if there exists one', async () => {
558 const resp = await asyncGetter();
559 const resp2 = syncGetter();
560 expect(resp).toBe(resp2);
561 expect(asyncNativeGetter).toHaveBeenCalled();
562 expect(syncNativeGetter).not.toHaveBeenCalled();
563 });
564 }
565 );
566
567 describe.each(nonMemoizedBooleanGetters)(
568 '%s*',
569 (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => {
570 beforeEach(() => {
571 clearMemo();
572 Platform.OS = 'android';
573 asyncNativeGetter.mockClear();
574 syncNativeGetter.mockClear();
575 });
576
577 it('should have an async version', () => {
578 expect(typeof asyncGetter).toBe('function');
579 });
580
581 it('should have a sync version', () => {
582 expect(typeof syncGetter).toBe('function');
583 });
584
585 it('should call native async module function', async () => {
586 const resp = await asyncGetter();
587 expect(resp).toEqual(false);
588 expect(asyncNativeGetter).toHaveBeenCalled();
589 });
590
591 it('should call native sync module function', () => {
592 const resp = syncGetter();
593 expect(resp).toEqual(false);
594 expect(syncNativeGetter).toHaveBeenCalled();
595 });
596
597 it('should not call native sync module function on unsupported OS', () => {
598 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
599 const resp = syncGetter();
600 expect(resp).toEqual(false);
601 expect(syncNativeGetter).not.toHaveBeenCalled();
602 });
603
604 it('should not call native async module function on unsupported OS', async () => {
605 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
606 const resp = await asyncGetter();
607 expect(resp).toEqual(false);
608 expect(asyncNativeGetter).not.toHaveBeenCalled();
609 });
610 }
611 );
612});
613
614const memoizedArrayGetters = [
615 [
616 'supported32BitAbis', // name
617 (RNDeviceInfo as any).supported32BitAbis, // asyncGetter
618 (RNDeviceInfo as any).supported32BitAbisSync, // syncGetter
619 mockNativeModule.getSupported32BitAbis, // asyncNativeGetter
620 mockNativeModule.getSupported32BitAbisSync, // syncNativeGetter
621 ],
622 [
623 'supported64BitAbis', // name
624 (RNDeviceInfo as any).supported64BitAbis, // asyncGetter
625 (RNDeviceInfo as any).supported64BitAbisSync, // syncGetter
626 mockNativeModule.getSupported64BitAbis, // asyncNativeGetter
627 mockNativeModule.getSupported64BitAbisSync, // syncNativeGetter
628 ],
629];
630
631const nonMemoizedArrayGetters = ['getSystemAvailableFeatures'].map(makeTable);
632
633describe('array getters', () => {
634 describe.each(memoizedArrayGetters)(
635 '%s*',
636 (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => {
637 beforeEach(() => {
638 clearMemo();
639 Platform.OS = 'android';
640 asyncNativeGetter.mockClear();
641 syncNativeGetter.mockClear();
642 });
643
644 it('should have an async version', () => {
645 expect(typeof asyncGetter).toBe('function');
646 });
647
648 it('should have a sync version', () => {
649 expect(typeof syncGetter).toBe('function');
650 });
651
652 it('should call native async module function', async () => {
653 const resp = await asyncGetter();
654 expect(resp).toEqual([]);
655 expect(asyncNativeGetter).toHaveBeenCalled();
656 });
657
658 it('should call native sync module function', () => {
659 const resp = syncGetter();
660 expect(resp).toEqual([]);
661 expect(syncNativeGetter).toHaveBeenCalled();
662 });
663
664 it('should not call native sync module function on unsupported OS', () => {
665 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
666 const resp = syncGetter();
667 expect(resp).toEqual([]);
668 expect(syncNativeGetter).not.toHaveBeenCalled();
669 });
670
671 it('should not call native async module function on unsupported OS', async () => {
672 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
673 const resp = await asyncGetter();
674 expect(resp).toEqual([]);
675 expect(asyncNativeGetter).not.toHaveBeenCalled();
676 });
677
678 it('should use memoized value if there exists one', async () => {
679 const resp = await asyncGetter();
680 const resp2 = syncGetter();
681 expect(resp).toBe(resp2);
682 expect(asyncNativeGetter).toHaveBeenCalled();
683 expect(syncNativeGetter).not.toHaveBeenCalled();
684 });
685 }
686 );
687
688 describe.each(nonMemoizedArrayGetters)(
689 '%s*',
690 (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => {
691 beforeEach(() => {
692 clearMemo();
693 Platform.OS = 'android';
694 asyncNativeGetter.mockClear();
695 syncNativeGetter.mockClear();
696 });
697
698 it('should have an async version', () => {
699 expect(typeof asyncGetter).toBe('function');
700 });
701
702 it('should have a sync version', () => {
703 expect(typeof syncGetter).toBe('function');
704 });
705
706 it('should call native async module function', async () => {
707 const resp = await asyncGetter();
708 expect(resp).toEqual([]);
709 expect(asyncNativeGetter).toHaveBeenCalled();
710 });
711
712 it('should call native sync module function', () => {
713 const resp = syncGetter();
714 expect(resp).toEqual([]);
715 expect(syncNativeGetter).toHaveBeenCalled();
716 });
717
718 it('should not call native sync module function on unsupported OS', () => {
719 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
720 const resp = syncGetter();
721 expect(resp).toEqual([]);
722 expect(syncNativeGetter).not.toHaveBeenCalled();
723 });
724
725 it('should not call native async module function on unsupported OS', async () => {
726 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
727 const resp = await asyncGetter();
728 expect(resp).toEqual([]);
729 expect(asyncNativeGetter).not.toHaveBeenCalled();
730 });
731 }
732 );
733
734 describe('isTablet', () => {
735 beforeEach(() => {
736 clearMemo();
737 Platform.OS = 'android';
738 });
739
740 it('should exist as function', () => {
741 expect(typeof RNDeviceInfo.isTablet).toBe('function');
742 });
743
744 it.each(['android', 'ios', 'windows'])(
745 'should support OS, %s, by return getter value',
746 (os) => {
747 Platform.OS = os as any;
748 expect(RNDeviceInfo.isTablet()).toEqual(true);
749 }
750 );
751
752 it.each(['web', 'GLaDOS'])('should return default value for unsupported OS, %s', (os) => {
753 Platform.OS = os as any;
754 expect(RNDeviceInfo.isTablet()).toEqual(false);
755 });
756 });
757});
758
759describe('Object Getters', () => {
760 describe('getPowerState*', () => {
761 const [, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter] = makeTable(
762 'getPowerState'
763 );
764 const supportedPlatforms = ['android', 'ios', 'windows', 'web'];
765
766 beforeEach(() => {
767 clearMemo();
768 asyncNativeGetter.mockClear();
769 syncNativeGetter.mockClear();
770 });
771
772 it('should have an async version', () => {
773 expect(typeof asyncGetter).toBe('function');
774 });
775
776 it('should have a sync version', () => {
777 expect(typeof syncGetter).toBe('function');
778 });
779
780 it.each(supportedPlatforms)(
781 'should call native async module function for supported platform, %s',
782 async (platform) => {
783 Platform.OS = platform as any;
784 const resp = await asyncGetter();
785 expect(resp).toEqual({});
786 expect(asyncNativeGetter).toHaveBeenCalled();
787 }
788 );
789
790 it.each(supportedPlatforms)(
791 'should call native sync module function for supported platform, %s',
792 (platform) => {
793 Platform.OS = platform as any;
794 const resp = syncGetter();
795 expect(resp).toEqual({});
796 expect(syncNativeGetter).toHaveBeenCalled();
797 }
798 );
799
800 it('should not call native sync module function on unsupported OS', () => {
801 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
802 const resp = syncGetter();
803 expect(resp).toEqual({});
804 expect(syncNativeGetter).not.toHaveBeenCalled();
805 });
806
807 it('should not call native async module function on unsupported OS', async () => {
808 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
809 const resp = await asyncGetter();
810 expect(resp).toEqual({});
811 expect(asyncNativeGetter).not.toHaveBeenCalled();
812 });
813 });
814
815 describe('getAvailableLocationProviders*', () => {
816 const [, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter] = makeTable(
817 'getAvailableLocationProviders'
818 );
819 const supportedPlatforms = ['android', 'ios'];
820
821 beforeEach(() => {
822 clearMemo();
823 asyncNativeGetter.mockClear();
824 syncNativeGetter.mockClear();
825 });
826
827 it('should have an async version', () => {
828 expect(typeof asyncGetter).toBe('function');
829 });
830
831 it('should have a sync version', () => {
832 expect(typeof syncGetter).toBe('function');
833 });
834
835 it.each(supportedPlatforms)(
836 'should call native async module function for supported platform, %s',
837 async (platform) => {
838 Platform.OS = platform as any;
839 const resp = await asyncGetter();
840 expect(resp).toEqual({});
841 expect(asyncNativeGetter).toHaveBeenCalled();
842 }
843 );
844
845 it.each(supportedPlatforms)(
846 'should call native sync module function for supported platform, %s',
847 (platform) => {
848 Platform.OS = platform as any;
849 const resp = syncGetter();
850 expect(resp).toEqual({});
851 expect(syncNativeGetter).toHaveBeenCalled();
852 }
853 );
854
855 it('should not call native sync module function on unsupported OS', () => {
856 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
857 const resp = syncGetter();
858 expect(resp).toEqual({});
859 expect(syncNativeGetter).not.toHaveBeenCalled();
860 });
861
862 it('should not call native async module function on unsupported OS', async () => {
863 Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything
864 const resp = await asyncGetter();
865 expect(resp).toEqual({});
866 expect(asyncNativeGetter).not.toHaveBeenCalled();
867 });
868 });
869});