@@ -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,28 +50,32 @@ 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
) ;
57
57
58
- expect ( result . current . supported ) . toBe ( false ) ;
59
- expect ( result . current . effectiveConnectionType ) . toBe (
60
- initialEffectiveConnectionType
61
- ) ;
58
+ setTimeout ( ( ) => {
59
+ expect ( result . current . supported ) . toBe ( false ) ;
60
+ expect ( result . current . effectiveConnectionType ) . toBe (
61
+ initialEffectiveConnectionType
62
+ ) ;
63
+ } )
62
64
} ) ;
63
65
64
66
test ( 'should return 4g of effectiveConnectionType' , ( ) => {
65
67
global . navigator . connection = {
66
68
...ectStatusListeners ,
67
69
effectiveType : '4g'
68
70
} ;
69
-
71
+
70
72
const { result } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
71
73
72
- testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
73
- expect ( result . current . supported ) . toBe ( true ) ;
74
- expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
74
+ setTimeout ( ( ) => {
75
+ testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
76
+ expect ( result . current . supported ) . toBe ( true ) ;
77
+ expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
78
+ } )
75
79
} ) ;
76
80
77
81
test ( 'should not return initialEffectiveConnectionType for supported case' , ( ) => {
@@ -85,19 +89,23 @@ describe('useNetworkStatus', () => {
85
89
useNetworkStatus ( initialEffectiveConnectionType )
86
90
) ;
87
91
88
- testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
89
- expect ( result . current . supported ) . toBe ( true ) ;
90
- expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
92
+ setTimeout ( ( ) => {
93
+ testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
94
+ expect ( result . current . supported ) . toBe ( true ) ;
95
+ expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
96
+ } )
91
97
} ) ;
92
98
93
99
test ( 'should update the effectiveConnectionType state' , ( ) => {
94
100
const { result } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
95
101
96
- act ( ( ) =>
97
- result . current . setNetworkStatus ( { effectiveConnectionType : '2g' } )
98
- ) ;
99
-
100
- expect ( result . current . effectiveConnectionType ) . toEqual ( '2g' ) ;
102
+ setTimeout ( ( ) => {
103
+ act ( ( ) =>
104
+ result . current . setNetworkStatus ( { effectiveConnectionType : '2g' } )
105
+ ) ;
106
+
107
+ expect ( result . current . effectiveConnectionType ) . toEqual ( '2g' ) ;
108
+ } )
101
109
} ) ;
102
110
103
111
test ( 'should update the effectiveConnectionType state when navigator.connection change event' , ( ) => {
@@ -108,21 +116,26 @@ describe('useNetworkStatus', () => {
108
116
109
117
const { result } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
110
118
global . navigator . connection . effectiveType = '4g' ;
111
- act ( ( ) => map . change ( ) ) ;
112
119
113
- expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
120
+ setTimeout ( ( ) => {
121
+ act ( ( ) => map . change ( ) ) ;
122
+
123
+ expect ( result . current . effectiveConnectionType ) . toEqual ( '4g' ) ;
124
+ } )
114
125
} ) ;
115
126
116
127
test ( 'should remove the listener for the navigator.connection change event on unmount' , ( ) => {
117
128
global . navigator . connection = {
118
129
...ectStatusListeners ,
119
130
effectiveType : '2g'
120
131
} ;
121
-
132
+
122
133
const { unmount } = renderHook ( ( ) => useNetworkStatus ( ) ) ;
123
134
124
- testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
125
- unmount ( ) ;
126
- testEctStatusEventListenerMethod ( ectStatusListeners . removeEventListener ) ;
135
+ setTimeout ( ( ) => {
136
+ testEctStatusEventListenerMethod ( ectStatusListeners . addEventListener ) ;
137
+ unmount ( ) ;
138
+ testEctStatusEventListenerMethod ( ectStatusListeners . removeEventListener ) ;
139
+ } )
127
140
} ) ;
128
141
} ) ;
0 commit comments