|
| 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