Skip to content

Commit 4b4731d

Browse files
committed
move support initialization outside of useNetworkStatus hook
1 parent 1bd3a53 commit 4b4731d

File tree

2 files changed

+58
-45
lines changed

2 files changed

+58
-45
lines changed

network/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import { useState, useEffect } from 'react';
1818

19-
const useNetworkStatus = (initialEffectiveConnectionType = null) => {
20-
const supported = (typeof navigator !== 'undefined' && 'connection' in navigator && 'effectiveType' in navigator.connection)
19+
const supported = (typeof navigator !== 'undefined' && 'connection' in navigator && 'effectiveType' in navigator.connection)
2120

21+
const useNetworkStatus = (initialEffectiveConnectionType = null) => {
2222
const initialNetworkStatus = {
2323
supported,
2424
effectiveConnectionType: supported

network/network.test.js

+56-43
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ import { renderHook, act } from '@testing-library/react-hooks';
1818

1919
import { useNetworkStatus } from './';
2020

21-
describe('useNetworkStatus', () => {
22-
const map = {};
21+
const map = {};
2322

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+
};
3029

31-
afterEach(() => {
32-
Object.values(ectStatusListeners).forEach(listener => listener.mockClear());
33-
});
30+
afterEach(() => {
31+
Object.values(ectStatusListeners).forEach(listener => listener.mockClear());
32+
});
3433

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+
};
4443

44+
describe('useNetworkStatus', () => {
4545
test(`should return "false" for unsupported case`, () => {
4646
const { result } = renderHook(() => useNetworkStatus());
4747

@@ -50,28 +50,32 @@ describe('useNetworkStatus', () => {
5050

5151
test('should return initialEffectiveConnectionType for unsupported case', () => {
5252
const initialEffectiveConnectionType = '4g';
53-
53+
5454
const { result } = renderHook(() =>
5555
useNetworkStatus(initialEffectiveConnectionType)
5656
);
5757

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+
})
6264
});
6365

6466
test('should return 4g of effectiveConnectionType', () => {
6567
global.navigator.connection = {
6668
...ectStatusListeners,
6769
effectiveType: '4g'
6870
};
69-
71+
7072
const { result } = renderHook(() => useNetworkStatus());
7173

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+
})
7579
});
7680

7781
test('should not return initialEffectiveConnectionType for supported case', () => {
@@ -85,19 +89,23 @@ describe('useNetworkStatus', () => {
8589
useNetworkStatus(initialEffectiveConnectionType)
8690
);
8791

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+
})
9197
});
9298

9399
test('should update the effectiveConnectionType state', () => {
94100
const { result } = renderHook(() => useNetworkStatus());
95101

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+
})
101109
});
102110

103111
test('should update the effectiveConnectionType state when navigator.connection change event', () => {
@@ -108,21 +116,26 @@ describe('useNetworkStatus', () => {
108116

109117
const { result } = renderHook(() => useNetworkStatus());
110118
global.navigator.connection.effectiveType = '4g';
111-
act(() => map.change());
112119

113-
expect(result.current.effectiveConnectionType).toEqual('4g');
120+
setTimeout(() => {
121+
act(() => map.change());
122+
123+
expect(result.current.effectiveConnectionType).toEqual('4g');
124+
})
114125
});
115126

116127
test('should remove the listener for the navigator.connection change event on unmount', () => {
117128
global.navigator.connection = {
118129
...ectStatusListeners,
119130
effectiveType: '2g'
120131
};
121-
132+
122133
const { unmount } = renderHook(() => useNetworkStatus());
123134

124-
testEctStatusEventListenerMethod(ectStatusListeners.addEventListener);
125-
unmount();
126-
testEctStatusEventListenerMethod(ectStatusListeners.removeEventListener);
135+
setTimeout(() => {
136+
testEctStatusEventListenerMethod(ectStatusListeners.addEventListener);
137+
unmount();
138+
testEctStatusEventListenerMethod(ectStatusListeners.removeEventListener);
139+
})
127140
});
128141
});

0 commit comments

Comments
 (0)