1
1
// eslint-disable-next-line no-restricted-imports
2
2
import * as ReactNative from 'react-native' ;
3
- import _ from 'underscore' ;
3
+ import type StartupTimer from '@libs/StartupTimer/types' ;
4
+
5
+ const { BootSplash} = ReactNative . NativeModules ;
4
6
5
7
jest . doMock ( 'react-native' , ( ) => {
6
8
let url = 'https://new.expensify.com/' ;
7
9
const getInitialURL = ( ) => Promise . resolve ( url ) ;
8
10
9
- let appState = 'active' ;
11
+ let appState : ReactNative . AppStateStatus = 'active' ;
10
12
let count = 0 ;
11
- const changeListeners = { } ;
13
+ const changeListeners : Record < number , ( state : ReactNative . AppStateStatus ) => void > = { } ;
12
14
13
15
// Tests will run with the app in a typical small screen size by default. We do this since the react-native test renderer
14
16
// runs against index.native.js source and so anything that is testing a component reliant on withWindowDimensions()
15
17
// would be most commonly assumed to be on a mobile phone vs. a tablet or desktop style view. This behavior can be
16
18
// overridden by explicitly setting the dimensions inside a test via Dimensions.set()
17
- let dimensions = {
19
+ let dimensions : Record < string , number > = {
18
20
width : 300 ,
19
21
height : 700 ,
20
22
scale : 1 ,
21
23
fontScale : 1 ,
22
24
} ;
23
25
24
- return Object . setPrototypeOf (
26
+ type ReactNativeMock = typeof ReactNative & {
27
+ NativeModules : typeof ReactNative . NativeModules & {
28
+ BootSplash : {
29
+ getVisibilityStatus : typeof BootSplash . getVisibilityStatus ;
30
+ hide : typeof BootSplash . hide ;
31
+ logoSizeRatio : number ;
32
+ navigationBarHeight : number ;
33
+ } ;
34
+ StartupTimer : StartupTimer ;
35
+ } ;
36
+ Linking : typeof ReactNative . Linking & {
37
+ setInitialURL : ( newUrl : string ) => void ;
38
+ } ;
39
+ AppState : typeof ReactNative . AppState & {
40
+ emitCurrentTestState : ( state : ReactNative . AppStateStatus ) => void ;
41
+ } ;
42
+ } ;
43
+
44
+ const reactNativeMock : ReactNativeMock = Object . setPrototypeOf (
25
45
{
26
46
NativeModules : {
27
47
...ReactNative . NativeModules ,
@@ -36,7 +56,7 @@ jest.doMock('react-native', () => {
36
56
Linking : {
37
57
...ReactNative . Linking ,
38
58
getInitialURL,
39
- setInitialURL ( newUrl ) {
59
+ setInitialURL ( newUrl : string ) {
40
60
url = newUrl ;
41
61
} ,
42
62
} ,
@@ -45,11 +65,11 @@ jest.doMock('react-native', () => {
45
65
get currentState ( ) {
46
66
return appState ;
47
67
} ,
48
- emitCurrentTestState ( state ) {
68
+ emitCurrentTestState ( state : ReactNative . AppStateStatus ) {
49
69
appState = state ;
50
- _ . each ( changeListeners , ( listener ) => listener ( appState ) ) ;
70
+ Object . entries ( changeListeners ) . forEach ( ( [ , listener ] ) => listener ( appState ) ) ;
51
71
} ,
52
- addEventListener ( type , listener ) {
72
+ addEventListener ( type : ReactNative . AppStateEvent , listener : ( state : ReactNative . AppStateStatus ) => void ) {
53
73
if ( type === 'change' ) {
54
74
const originalCount = count ;
55
75
changeListeners [ originalCount ] = listener ;
@@ -68,7 +88,7 @@ jest.doMock('react-native', () => {
68
88
...ReactNative . Dimensions ,
69
89
addEventListener : jest . fn ( ) ,
70
90
get : ( ) => dimensions ,
71
- set : ( newDimensions ) => {
91
+ set : ( newDimensions : Record < string , number > ) => {
72
92
dimensions = newDimensions ;
73
93
} ,
74
94
} ,
@@ -78,9 +98,11 @@ jest.doMock('react-native', () => {
78
98
// so it seems easier to just run the callback immediately in tests.
79
99
InteractionManager : {
80
100
...ReactNative . InteractionManager ,
81
- runAfterInteractions : ( callback ) => callback ( ) ,
101
+ runAfterInteractions : ( callback : ( ) => void ) => callback ( ) ,
82
102
} ,
83
103
} ,
84
104
ReactNative ,
85
105
) ;
106
+
107
+ return reactNativeMock ;
86
108
} ) ;
0 commit comments