Skip to content

Commit ea17bf3

Browse files
authored
Merge pull request #124 from myty/bugfix/undefined-window-in-network-information-utils
Add check for undefined window and navigator as well as tests
2 parents a273bcd + 2320685 commit ea17bf3

2 files changed

Lines changed: 68 additions & 11 deletions

File tree

src/utilities/network-information-utils.test.ts

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ import buildNetworkInformationUtils, {
44
} from "./network-information-utils";
55

66
const setupSut = (options?: {
7+
buildNavigator?: boolean;
8+
buildWindow?: boolean;
79
connectionProperty?: string;
810
onLine?: boolean;
911
}): typeof NetworkInformationUtils => {
10-
const { connectionProperty, onLine = false } = options ?? {};
12+
const {
13+
buildNavigator = true,
14+
buildWindow = true,
15+
connectionProperty,
16+
onLine = false,
17+
} = options ?? {};
1118

1219
const buildMockNavigator = (): Navigator => {
1320
const isOnline = onLine ?? navigator.onLine;
@@ -23,9 +30,10 @@ const setupSut = (options?: {
2330
return { ...navigator, onLine: isOnline };
2431
};
2532

26-
const navigator = buildMockNavigator();
33+
const navigator = buildNavigator ? buildMockNavigator() : undefined;
34+
const window = buildWindow ? ({ navigator } as Window) : undefined;
2735

28-
return buildNetworkInformationUtils(navigator);
36+
return buildNetworkInformationUtils(window);
2937
};
3038

3139
describe("NetworkInformationUtils", () => {
@@ -53,6 +61,28 @@ describe("NetworkInformationUtils", () => {
5361
}
5462
);
5563

64+
test("when window.navigator is undefined, it returns undefined", () => {
65+
// Arrange
66+
const sut = setupSut({ buildNavigator: false });
67+
68+
// Act
69+
const connection = sut.getNavigatorConnection();
70+
71+
// Assert
72+
expect(connection).toBeUndefined();
73+
});
74+
75+
test("when window is undefined, it returns undefined", () => {
76+
// Arrange
77+
const sut = setupSut({ buildWindow: false });
78+
79+
// Act
80+
const connection = sut.getNavigatorConnection();
81+
82+
// Assert
83+
expect(connection).toBeUndefined();
84+
});
85+
5686
test("when window.navigator does not have a known connection property, it returns undefined", () => {
5787
// Arrange
5888
const sut = setupSut();
@@ -113,6 +143,28 @@ describe("NetworkInformationUtils", () => {
113143
expect(networkConnection.isOnline).toEqual(onLine);
114144
}
115145
);
146+
147+
test("when navigator is undefined, it returns undefined", () => {
148+
// Arrange
149+
const sut = setupSut({ buildNavigator: false });
150+
151+
// Act
152+
const networkConnection = sut.getNetworkConnection();
153+
154+
// Assert
155+
expect(networkConnection).toBeUndefined();
156+
});
157+
158+
describe("when window is undefined, it returns undefined", () => {
159+
// Arrange
160+
const sut = setupSut({ buildWindow: false });
161+
162+
// Act
163+
const networkConnection = sut.getNetworkConnection();
164+
165+
// Assert
166+
expect(networkConnection).toBeUndefined();
167+
});
116168
});
117169

118170
// #endregion getNetworkConnection

src/utilities/network-information-utils.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1+
import { NetworkConnection } from "../interfaces/network-connection";
12
import { NetworkInformation } from "../interfaces/network-information";
23
import { Navigator } from "../types/navigator";
34

45
// -----------------------------------------------------------------------------------------
56
// #region Functions
67
// -----------------------------------------------------------------------------------------
78

8-
export default function buildNetworkInformationUtils(navigator: Navigator) {
9+
export default function buildNetworkInformationUtils(window?: Window) {
10+
const navigator: Navigator | undefined = window?.navigator;
11+
912
return {
1013
/**
1114
* Returns a NavigatorConnection object if one exists
1215
*/
1316
getNavigatorConnection(): NetworkInformation | undefined {
1417
return (
15-
navigator.connection ??
16-
navigator.mozConnection ??
17-
navigator.webkitConnection ??
18+
navigator?.connection ??
19+
navigator?.mozConnection ??
20+
navigator?.webkitConnection ??
1821
undefined
1922
);
2023
},
2124

2225
/**
2326
* Returns a `NetworkConnection` object which is an aggregate of `navigator.connection` and `navigator.onLine`
2427
*/
25-
getNetworkConnection() {
28+
getNetworkConnection(): NetworkConnection | undefined {
29+
if (navigator == null) {
30+
return undefined;
31+
}
32+
2633
const { onLine: isOnline } = navigator;
2734
const navigatorConnection = this.getNavigatorConnection() ?? {};
2835

@@ -40,8 +47,6 @@ export default function buildNetworkInformationUtils(navigator: Navigator) {
4047
// #region Exports
4148
// -----------------------------------------------------------------------------------------
4249

43-
export const NetworkInformationUtils = buildNetworkInformationUtils(
44-
window.navigator
45-
);
50+
export const NetworkInformationUtils = buildNetworkInformationUtils(window);
4651

4752
// #endregion Exports

0 commit comments

Comments
 (0)