Skip to content

Commit d40ad2b

Browse files
#101 Implement network information utilities
Add network information utils
2 parents 9f15628 + dc2707c commit d40ad2b

11 files changed

Lines changed: 299 additions & 0 deletions

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
1010

11+
[*.ts]
12+
indent_size = 4
13+
1114
[*.md]
1215
trim_trailing_whitespace = false
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* ConnectionType - Network Information API
3+
* - W3C Draft Community Group Report 11 May 2020
4+
* - https://wicg.github.io/netinfo/#connectiontype-enum
5+
*/
6+
enum ConnectionType {
7+
Bluetooth = "bluetooth",
8+
Cellular = "cellular",
9+
Ethernet = "ethernet",
10+
None = "none",
11+
Other = "other",
12+
Unknown = "unknown",
13+
Wifi = "wifi",
14+
Wimax = "wimax",
15+
}
16+
17+
export { ConnectionType };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* EffectiveConnectionType - Network Information API
3+
* - Draft Community Group Report 11 May 2020
4+
* - https://wicg.github.io/netinfo/#effectiveconnectiontype-enum
5+
*/
6+
enum EffectiveConnectionType {
7+
Cellular2g = "2g",
8+
Cellular3g = "3g",
9+
Cellular4g = "4g",
10+
Slow2g = "slow-2g",
11+
}
12+
13+
export { EffectiveConnectionType };
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum NavigatorConnectionVariant {
2+
Standard = "connection",
3+
Mozilla = "mozConnection",
4+
Webkit = "webkitConnection",
5+
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export { BaseSpanishSpain } from "./cultures/base-spanish-spain";
2222
// -----------------------------------------------------------------------------------------
2323

2424
export { AnchorTargetTypes } from "./enumerations/anchor-target-types";
25+
export { ConnectionType } from "./enumerations/connection-type";
2526
export { ContentType } from "./enumerations/content-type";
27+
export { EffectiveConnectionType } from "./enumerations/effective-connection-type";
2628
export { ErrorType } from "./enumerations/error-type";
2729
export { HttpHeader } from "./enumerations/http-header";
2830
export { HttpVerb } from "./enumerations/http-verb";
@@ -40,6 +42,7 @@ export { CultureParams } from "./interfaces/culture-params";
4042
export { Entity } from "./interfaces/entity";
4143
export { KeyValuePair } from "./interfaces/key-value-pair";
4244
export { LocalizationInitOptions } from "./interfaces/localization-init-options";
45+
export { NetworkConnection } from "./interfaces/network-connection";
4346
export { PagedQuery } from "./interfaces/paged-query";
4447
export { PagedResult } from "./interfaces/paged-result";
4548
export { Result } from "./interfaces/result";
@@ -76,6 +79,7 @@ export { Do } from "./utilities/do-try";
7679
export { DoSync } from "./utilities/do-try";
7780
export { EnvironmentUtils } from "./utilities/environment-utils";
7881
export { LocalizationUtils } from "./utilities/localization-utils";
82+
export { NetworkInformationUtils } from "./utilities/network-information-utils";
7983
export { PolyfillUtils } from "./utilities/polyfill-utils";
8084
export { PromiseFactory } from "./utilities/promise-factory";
8185
export { RecordUtils } from "./utilities/record-utils";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { NetworkInformation } from "./network-information";
2+
3+
// -----------------------------------------------------------------------------------------
4+
// #region Interfaces
5+
// -----------------------------------------------------------------------------------------
6+
7+
interface NetworkConnection extends NetworkInformation {
8+
/**
9+
* Returns a true or false indicating whether the browser is working online.
10+
*/
11+
isOnline: boolean;
12+
}
13+
14+
// #endregion Interfaces
15+
16+
// -----------------------------------------------------------------------------------------
17+
// #region Export
18+
// -----------------------------------------------------------------------------------------
19+
20+
export { NetworkConnection };
21+
22+
// #endregion Export
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { ConnectionType } from "../enumerations/connection-type";
2+
import { EffectiveConnectionType } from "../enumerations/effective-connection-type";
3+
4+
// -----------------------------------------------------------------------------------------
5+
// #region Interfaces
6+
// -----------------------------------------------------------------------------------------
7+
8+
/**
9+
* NetworkInformation - Network Information API
10+
* - Draft Community Group Report 11 May 2020
11+
* - https://wicg.github.io/netinfo/#networkinformation-interface
12+
*/
13+
interface NetworkInformation extends Partial<EventTarget> {
14+
/**
15+
* Returns the effective bandwidth estimate in megabits per second, rounded to the nearest multiple of 25 kilobits per seconds.
16+
*/
17+
downlink?: number;
18+
19+
/**
20+
* Returns the maximum downlink speed, in megabits per second (Mbps), for the underlying connection technology.
21+
*/
22+
downlinkMax?: number;
23+
24+
/**
25+
* Returns the effective type of the connection meaning one of 'slow-2g', '2g', '3g', or '4g'.
26+
* This value is determined using a combination of recently observed round-trip time and downlink values.
27+
*/
28+
effectiveType?: EffectiveConnectionType;
29+
30+
/**
31+
* Returns the estimated effective round-trip time of the current connection, rounded to the
32+
* nearest multiple of 25 milliseconds.
33+
*/
34+
rtt?: number;
35+
36+
/**
37+
* Returns true if the user has set a reduced data usage option on the user agent.
38+
*/
39+
saveData?: boolean;
40+
41+
/**
42+
* Returns the type of connection a device is using to communicate with the network
43+
*/
44+
type?: ConnectionType;
45+
}
46+
47+
// #endregion Interfaces
48+
49+
// -----------------------------------------------------------------------------------------
50+
// #region Export
51+
// -----------------------------------------------------------------------------------------
52+
53+
export { NetworkInformation };
54+
55+
// #endregion Export

src/types/navigator.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NavigatorConnectionVariant } from "../enumerations/navigator-connection-variant";
2+
import { NetworkInformation } from "../interfaces/network-information";
3+
4+
export declare interface Navigator {
5+
[NavigatorConnectionVariant.Standard]?: NetworkInformation;
6+
[NavigatorConnectionVariant.Mozilla]?: NetworkInformation;
7+
[NavigatorConnectionVariant.Webkit]?: NetworkInformation;
8+
onLine: boolean;
9+
}

src/types/window.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Navigator } from "../types/navigator";
2+
3+
export declare interface Window {
4+
navigator: Navigator;
5+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { NavigatorConnectionVariant } from "../enumerations/navigator-connection-variant";
2+
import buildNetworkInformationUtils, {
3+
NetworkInformationUtils,
4+
} from "./network-information-utils";
5+
6+
const setupSut = (options?: {
7+
connectionProperty?: string;
8+
onLine?: boolean;
9+
}): typeof NetworkInformationUtils => {
10+
const { connectionProperty, onLine = false } = options ?? {};
11+
12+
const buildMockNavigator = (): Navigator => {
13+
const isOnline = onLine ?? navigator.onLine;
14+
15+
if (connectionProperty != null) {
16+
return {
17+
...navigator,
18+
onLine: isOnline,
19+
[connectionProperty]: {},
20+
};
21+
}
22+
23+
return { ...navigator, onLine: isOnline };
24+
};
25+
26+
const navigator = buildMockNavigator();
27+
28+
return buildNetworkInformationUtils(navigator);
29+
};
30+
31+
describe("NetworkInformationUtils", () => {
32+
// -----------------------------------------------------------------------------------------
33+
// #region getNavigatorConnection
34+
// -----------------------------------------------------------------------------------------
35+
36+
describe("getNavigatorConnection()", () => {
37+
test.each`
38+
connectionProperty
39+
${NavigatorConnectionVariant.Standard}
40+
${NavigatorConnectionVariant.Mozilla}
41+
${NavigatorConnectionVariant.Webkit}
42+
`(
43+
"when window.navigator has $connectionProperty, it returns connection",
44+
({ connectionProperty }) => {
45+
// Arrange
46+
const sut = setupSut({ connectionProperty });
47+
48+
// Act
49+
const navigatorConnection = sut.getNavigatorConnection();
50+
51+
// Assert
52+
expect(navigatorConnection).not.toBeUndefined();
53+
}
54+
);
55+
56+
test("when window.navigator does not have a known connection property, it returns undefined", () => {
57+
// Arrange
58+
const sut = setupSut();
59+
60+
// Act
61+
const connection = sut.getNavigatorConnection();
62+
63+
// Assert
64+
expect(connection).toBeUndefined();
65+
});
66+
});
67+
68+
// #endregion getNavigatorConnection
69+
70+
// -----------------------------------------------------------------------------------------
71+
// #region getNetworkConnection
72+
// -----------------------------------------------------------------------------------------
73+
74+
describe("getNetworkConnection()", () => {
75+
test("when navigator has no connection, it returns a value", () => {
76+
// Arrange
77+
const sut = setupSut();
78+
79+
// Act
80+
const networkConnection = sut.getNetworkConnection();
81+
82+
// Assert
83+
expect(networkConnection).toBeDefined();
84+
});
85+
86+
test("when navigator has a connection, it returns a value", () => {
87+
// Arrange
88+
const sut = setupSut({
89+
connectionProperty: NavigatorConnectionVariant.Standard,
90+
});
91+
92+
// Act
93+
const networkConnection = sut.getNetworkConnection();
94+
95+
// Assert
96+
expect(networkConnection).toBeDefined();
97+
});
98+
99+
test.each`
100+
onLine
101+
${false}
102+
${true}
103+
`(
104+
"when window.navigator onLine is $onLine, isOnline is $onLine",
105+
({ onLine }) => {
106+
// Arrange
107+
const sut = setupSut({ onLine });
108+
109+
// Act
110+
const networkConnection = sut.getNetworkConnection();
111+
112+
// Assert
113+
expect(networkConnection.isOnline).toEqual(onLine);
114+
}
115+
);
116+
});
117+
118+
// #endregion getNetworkConnection
119+
});

0 commit comments

Comments
 (0)