@@ -18,30 +18,30 @@ import { renderHook, act } from '@testing-library/react-hooks';
18
18
19
19
import { useNetworkStatus } from './' ;
20
20
21
- describe ( 'useNetworkStatus' , ( ) => {
22
- const map = { } ;
21
+ const map = { } ;
23
22
24
- const ectStatusListeners = {
25
- addEventListener : jest . fn ( ) . mockImplementation ( ( event , callback ) => {
26
- map [ event ] = callback ;
27
- } ) ,
28
- removeEventListener : jest . fn ( )
29
- } ;
23
+ const ectStatusListeners = {
24
+ addEventListener : jest . fn ( ) . mockImplementation ( ( event , callback ) => {
25
+ map [ event ] = callback ;
26
+ } ) ,
27
+ removeEventListener : jest . fn ( )
28
+ } ;
30
29
31
- afterEach ( ( ) => {
32
- Object . values ( ectStatusListeners ) . forEach ( listener => listener . mockClear ( ) ) ;
33
- } ) ;
30
+ afterEach ( ( ) => {
31
+ Object . values ( ectStatusListeners ) . forEach ( listener => listener . mockClear ( ) ) ;
32
+ } ) ;
34
33
35
- /**
36
- * Tests that addEventListener or removeEventListener was called during the
37
- * lifecycle of the useEffect hook within useNetworkStatus
38
- */
39
- const testEctStatusEventListenerMethod = method => {
40
- expect ( method ) . toBeCalledTimes ( 1 ) ;
41
- expect ( method . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'change' ) ;
42
- expect ( method . mock . calls [ 0 ] [ 1 ] . constructor ) . toEqual ( Function ) ;
43
- } ;
34
+ /**
35
+ * Tests that addEventListener or removeEventListener was called during the
36
+ * lifecycle of the useEffect hook within useNetworkStatus
37
+ */
38
+ const testEctStatusEventListenerMethod = method => {
39
+ expect ( method ) . toBeCalledTimes ( 1 ) ;
40
+ expect ( method . mock . calls [ 0 ] [ 0 ] ) . toEqual ( 'change' ) ;
41
+ expect ( method . mock . calls [ 0 ] [ 1 ] . constructor ) . toEqual ( Function ) ;
42
+ } ;
44
43
44
+ describe ( 'useNetworkStatus' , ( ) => {
45
45
test ( `should return "false" for unsupported case` , ( ) => {
46
46
const { result } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
47
47
@@ -50,7 +50,7 @@ describe('useNetworkStatus', () => {
50
50
51
51
test ( 'should return initialEffectiveConnectionType for unsupported case' , ( ) => {
52
52
const initialEffectiveConnectionType = '4g' ;
53
-
53
+
54
54
const { result } = renderHook ( ( ) =>
55
55
useNetworkStatus ( initialEffectiveConnectionType )
56
56
) ;
@@ -66,12 +66,14 @@ describe('useNetworkStatus', () => {
66
66
...ectStatusListeners ,
67
67
effectiveType : '4g'
68
68
} ;
69
-
70
- const { result } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
71
-
72
- testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
73
- expect ( result . current . supported ) . toBe ( true ) ;
74
- expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
69
+
70
+ const { result, waitForNextUpdate } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
71
+
72
+ waitForNextUpdate ( ) . then ( ( ) => {
73
+ testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
74
+ expect ( result . current . supported ) . toBe ( true ) ;
75
+ expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
76
+ } ) ;
75
77
} ) ;
76
78
77
79
test ( 'should not return initialEffectiveConnectionType for supported case' , ( ) => {
@@ -81,23 +83,27 @@ describe('useNetworkStatus', () => {
81
83
effectiveType : '4g'
82
84
} ;
83
85
84
- const { result } = renderHook ( ( ) =>
86
+ const { result, waitForNextUpdate } = renderHook ( ( ) =>
85
87
useNetworkStatus ( initialEffectiveConnectionType )
86
88
) ;
87
89
88
- testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
89
- expect ( result . current . supported ) . toBe ( true ) ;
90
- expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
90
+ waitForNextUpdate ( ) . then ( ( ) => {
91
+ testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
92
+ expect ( result . current . supported ) . toBe ( true ) ;
93
+ expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
94
+ } )
91
95
} ) ;
92
96
93
97
test ( 'should update the effectiveConnectionType state' , ( ) => {
94
- const { result } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
95
-
96
- act ( ( ) =>
97
- result . current . setNetworkStatus ( { effectiveConnectionType : '2g' } )
98
- ) ;
99
-
100
- expect ( result . current . effectiveConnectionType ) . toEqual ( '2g' ) ;
98
+ const { result, waitForNextUpdate } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
99
+
100
+ waitForNextUpdate ( ) . then ( ( ) => {
101
+ act ( ( ) =>
102
+ result . current . setNetworkStatus ( { effectiveConnectionType : '2g' } )
103
+ ) ;
104
+
105
+ expect ( result . current . effectiveConnectionType ) . toEqual ( '2g' ) ;
106
+ } )
101
107
} ) ;
102
108
103
109
test ( 'should update the effectiveConnectionType state when navigator.connection change event' , ( ) => {
@@ -106,23 +112,28 @@ describe('useNetworkStatus', () => {
106
112
effectiveType : '2g'
107
113
} ;
108
114
109
- const { result } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
115
+ const { result, waitForNextUpdate } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
110
116
global . navigator . connection . effectiveType = '4g' ;
111
- act ( ( ) => map . change ( ) ) ;
112
117
113
- expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
118
+ waitForNextUpdate ( ) . then ( ( ) => {
119
+ act ( ( ) => map . change ( ) ) ;
120
+
121
+ expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
122
+ } )
114
123
} ) ;
115
124
116
125
test ( 'should remove the listener for the navigator.connection change event on unmount' , ( ) => {
117
126
global . navigator . connection = {
118
127
...ectStatusListeners ,
119
128
effectiveType : '2g'
120
129
} ;
121
-
122
- const { unmount } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
123
-
124
- testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
125
- unmount ( ) ;
126
- testEctStatusEventListenerMethod ( ectStatusListeners . removeEventListener ) ;
130
+
131
+ const { unmount, waitForNextUpdate } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
132
+
133
+ waitForNextUpdate ( ) . then ( ( ) => {
134
+ testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
135
+ unmount ( ) ;
136
+ testEctStatusEventListenerMethod ( ectStatusListeners . removeEventListener ) ;
137
+ } )
127
138
} ) ;
128
139
} ) ;
0 commit comments