Skip to content

Commit cf8b7f8

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

File tree

2 files changed

+60
-49
lines changed

2 files changed

+60
-49
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

+58-47
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,7 +50,7 @@ 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
);
@@ -66,12 +66,14 @@ describe('useNetworkStatus', () => {
6666
...ectStatusListeners,
6767
effectiveType: '4g'
6868
};
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+
});
7577
});
7678

7779
test('should not return initialEffectiveConnectionType for supported case', () => {
@@ -81,23 +83,27 @@ describe('useNetworkStatus', () => {
8183
effectiveType: '4g'
8284
};
8385

84-
const { result } = renderHook(() =>
86+
const { result, waitForNextUpdate } = renderHook(() =>
8587
useNetworkStatus(initialEffectiveConnectionType)
8688
);
8789

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+
})
9195
});
9296

9397
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+
})
101107
});
102108

103109
test('should update the effectiveConnectionType state when navigator.connection change event', () => {
@@ -106,23 +112,28 @@ describe('useNetworkStatus', () => {
106112
effectiveType: '2g'
107113
};
108114

109-
const { result } = renderHook(() => useNetworkStatus());
115+
const { result, waitForNextUpdate } = renderHook(() => useNetworkStatus());
110116
global.navigator.connection.effectiveType = '4g';
111-
act(() => map.change());
112117

113-
expect(result.current.effectiveConnectionType).toEqual('4g');
118+
waitForNextUpdate().then(() => {
119+
act(() => map.change());
120+
121+
expect(result.current.effectiveConnectionType).toEqual('4g');
122+
})
114123
});
115124

116125
test('should remove the listener for the navigator.connection change event on unmount', () => {
117126
global.navigator.connection = {
118127
...ectStatusListeners,
119128
effectiveType: '2g'
120129
};
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+
})
127138
});
128139
});

0 commit comments

Comments
 (0)