Skip to content

Commit 98b55c8

Browse files
committed
add check for undefined window and navigator as well as tests
1 parent a273bcd commit 98b55c8

2 files changed

Lines changed: 69 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: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,32 @@ import { Navigator } from "../types/navigator";
55
// #region Functions
66
// -----------------------------------------------------------------------------------------
77

8-
export default function buildNetworkInformationUtils(navigator: Navigator) {
8+
export default function buildNetworkInformationUtils(window?: Window) {
9+
const navigator: Navigator | undefined = window?.navigator;
10+
911
return {
1012
/**
1113
* Returns a NavigatorConnection object if one exists
1214
*/
1315
getNavigatorConnection(): NetworkInformation | undefined {
1416
return (
15-
navigator.connection ??
16-
navigator.mozConnection ??
17-
navigator.webkitConnection ??
17+
navigator?.connection ??
18+
navigator?.mozConnection ??
19+
navigator?.webkitConnection ??
1820
undefined
1921
);
2022
},
2123

2224
/**
2325
* Returns a `NetworkConnection` object which is an aggregate of `navigator.connection` and `navigator.onLine`
2426
*/
25-
getNetworkConnection() {
27+
getNetworkConnection():
28+
| (NetworkInformation & { isOnline: boolean })
29+
| undefined {
30+
if (navigator == null) {
31+
return undefined;
32+
}
33+
2634
const { onLine: isOnline } = navigator;
2735
const navigatorConnection = this.getNavigatorConnection() ?? {};
2836

@@ -40,8 +48,6 @@ export default function buildNetworkInformationUtils(navigator: Navigator) {
4048
// #region Exports
4149
// -----------------------------------------------------------------------------------------
4250

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

4753
// #endregion Exports

0 commit comments

Comments
 (0)