@@ -4,10 +4,17 @@ import buildNetworkInformationUtils, {
44} from "./network-information-utils" ;
55
66const 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
3139describe ( "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
0 commit comments