Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from 'vitest'
2
3// Import after mocks are set up
4import { AppState } from 'react-native'
5import NetInfo from '@react-native-community/netinfo'
6import { ReactNativeOnlineDetector } from '../src/connectivity/ReactNativeOnlineDetector'
7
8// Mock the react-native module
9vi.mock(`react-native`, () => {
10 const listeners: Array<(state: string) => void> = []
11 return {
12 AppState: {
13 addEventListener: vi.fn(
14 (event: string, callback: (state: string) => void) => {
15 if (event === `change`) {
16 listeners.push(callback)
17 }
18 return {
19 remove: vi.fn(() => {
20 const index = listeners.indexOf(callback)
21 if (index > -1) {
22 listeners.splice(index, 1)
23 }
24 }),
25 }
26 },
27 ),
28 // Expose for testing
29 __listeners: listeners,
30 __triggerChange: (state: string) => {
31 for (const listener of listeners) {
32 listener(state)
33 }
34 },
35 },
36 }
37})
38
39// Mock the @react-native-community/netinfo module
40vi.mock(`@react-native-community/netinfo`, () => {
41 const listeners: Array<
42 (state: {
43 isConnected: boolean
44 isInternetReachable: boolean | null
45 }) => void
46 > = []
47 return {
48 default: {
49 addEventListener: vi.fn(
50 (
51 callback: (state: {
52 isConnected: boolean
53 isInternetReachable: boolean | null
54 }) => void,
55 ) => {
56 listeners.push(callback)
57 return () => {
58 const index = listeners.indexOf(callback)
59 if (index > -1) {
60 listeners.splice(index, 1)
61 }
62 }
63 },
64 ),
65 // Expose for testing
66 __listeners: listeners,
67 __triggerState: (state: {
68 isConnected: boolean
69 isInternetReachable: boolean | null
70 }) => {
71 for (const listener of listeners) {
72 listener(state)
73 }
74 },
75 },
76 }
77})
78
79describe(`ReactNativeOnlineDetector`, () => {
80 beforeEach(() => {
81 vi.clearAllMocks()
82 // Clear internal listener arrays
83 ;(AppState as any).__listeners.length = 0
84 ;(NetInfo as any).__listeners.length = 0
85 })
86
87 describe(`initialization`, () => {
88 it(`should subscribe to NetInfo on construction`, () => {
89 const detector = new ReactNativeOnlineDetector()
90
91 expect(NetInfo.addEventListener).toHaveBeenCalledTimes(1)
92 expect(NetInfo.addEventListener).toHaveBeenCalledWith(
93 expect.any(Function),
94 )
95
96 detector.dispose()
97 })
98
99 it(`should subscribe to AppState on construction`, () => {
100 const detector = new ReactNativeOnlineDetector()
101
102 expect(AppState.addEventListener).toHaveBeenCalledTimes(1)
103 expect(AppState.addEventListener).toHaveBeenCalledWith(
104 `change`,
105 expect.any(Function),
106 )
107
108 detector.dispose()
109 })
110 })
111
112 describe(`network connectivity changes`, () => {
113 it(`should notify subscribers when transitioning from offline to online`, () => {
114 const detector = new ReactNativeOnlineDetector()
115 const callback = vi.fn()
116
117 detector.subscribe(callback)
118
119 // First, simulate offline state
120 ;(NetInfo as any).__triggerState({
121 isConnected: false,
122 isInternetReachable: false,
123 })
124
125 expect(callback).not.toHaveBeenCalled()
126
127 // Now simulate coming online
128 ;(NetInfo as any).__triggerState({
129 isConnected: true,
130 isInternetReachable: true,
131 })
132
133 expect(callback).toHaveBeenCalledTimes(1)
134
135 detector.dispose()
136 })
137
138 it(`should not notify when already online and staying online`, () => {
139 const detector = new ReactNativeOnlineDetector()
140 const callback = vi.fn()
141
142 detector.subscribe(callback)
143
144 // Simulate online state (detector starts assuming online)
145 ;(NetInfo as any).__triggerState({
146 isConnected: true,
147 isInternetReachable: true,
148 })
149
150 // Should not trigger since we were already considered online
151 expect(callback).not.toHaveBeenCalled()
152
153 detector.dispose()
154 })
155
156 it(`should treat isInternetReachable null as potentially reachable`, () => {
157 const detector = new ReactNativeOnlineDetector()
158 const callback = vi.fn()
159
160 detector.subscribe(callback)
161
162 // First go offline
163 ;(NetInfo as any).__triggerState({
164 isConnected: false,
165 isInternetReachable: false,
166 })
167
168 // Now come online with null isInternetReachable (unknown)
169 ;(NetInfo as any).__triggerState({
170 isConnected: true,
171 isInternetReachable: null,
172 })
173
174 expect(callback).toHaveBeenCalledTimes(1)
175
176 detector.dispose()
177 })
178 })
179
180 describe(`app state changes`, () => {
181 it(`should notify subscribers when app becomes active`, () => {
182 const detector = new ReactNativeOnlineDetector()
183 const callback = vi.fn()
184
185 detector.subscribe(callback)
186
187 // Simulate app becoming active (foregrounded)
188 ;(AppState as any).__triggerChange(`active`)
189
190 expect(callback).toHaveBeenCalledTimes(1)
191
192 detector.dispose()
193 })
194
195 it(`should not notify when app goes to background`, () => {
196 const detector = new ReactNativeOnlineDetector()
197 const callback = vi.fn()
198
199 detector.subscribe(callback)
200
201 // Simulate app going to background
202 ;(AppState as any).__triggerChange(`background`)
203
204 expect(callback).not.toHaveBeenCalled()
205
206 detector.dispose()
207 })
208
209 it(`should not notify when app becomes inactive`, () => {
210 const detector = new ReactNativeOnlineDetector()
211 const callback = vi.fn()
212
213 detector.subscribe(callback)
214
215 // Simulate app becoming inactive
216 ;(AppState as any).__triggerChange(`inactive`)
217
218 expect(callback).not.toHaveBeenCalled()
219
220 detector.dispose()
221 })
222 })
223
224 describe(`subscription management`, () => {
225 it(`should support multiple subscribers`, () => {
226 const detector = new ReactNativeOnlineDetector()
227 const callback1 = vi.fn()
228 const callback2 = vi.fn()
229
230 detector.subscribe(callback1)
231 detector.subscribe(callback2)
232
233 detector.notifyOnline()
234
235 expect(callback1).toHaveBeenCalledTimes(1)
236 expect(callback2).toHaveBeenCalledTimes(1)
237
238 detector.dispose()
239 })
240
241 it(`should allow unsubscribing`, () => {
242 const detector = new ReactNativeOnlineDetector()
243 const callback = vi.fn()
244
245 const unsubscribe = detector.subscribe(callback)
246 unsubscribe()
247
248 detector.notifyOnline()
249
250 expect(callback).not.toHaveBeenCalled()
251
252 detector.dispose()
253 })
254
255 it(`should handle notifyOnline() manual trigger`, () => {
256 const detector = new ReactNativeOnlineDetector()
257 const callback = vi.fn()
258
259 detector.subscribe(callback)
260
261 detector.notifyOnline()
262 detector.notifyOnline()
263
264 expect(callback).toHaveBeenCalledTimes(2)
265
266 detector.dispose()
267 })
268 })
269
270 describe(`disposal`, () => {
271 it(`should unsubscribe from all native events on dispose`, () => {
272 const detector = new ReactNativeOnlineDetector()
273 const callback = vi.fn()
274
275 detector.subscribe(callback)
276 detector.dispose()
277
278 // NetInfo and AppState listeners should be cleared
279 expect((NetInfo as any).__listeners.length).toBe(0)
280 expect((AppState as any).__listeners.length).toBe(0)
281 })
282
283 it(`should not notify after dispose`, () => {
284 const detector = new ReactNativeOnlineDetector()
285 const callback = vi.fn()
286
287 detector.subscribe(callback)
288 detector.dispose()
289
290 // Try to trigger events - should not notify
291 detector.notifyOnline()
292
293 expect(callback).not.toHaveBeenCalled()
294 })
295 })
296
297 describe(`error handling`, () => {
298 it(`should catch and warn on listener errors without stopping other listeners`, () => {
299 const detector = new ReactNativeOnlineDetector()
300 const consoleWarnSpy = vi
301 .spyOn(console, `warn`)
302 .mockImplementation(() => {})
303 const errorCallback = vi.fn(() => {
304 throw new Error(`Test error`)
305 })
306 const successCallback = vi.fn()
307
308 detector.subscribe(errorCallback)
309 detector.subscribe(successCallback)
310
311 detector.notifyOnline()
312
313 expect(errorCallback).toHaveBeenCalledTimes(1)
314 expect(successCallback).toHaveBeenCalledTimes(1)
315 expect(consoleWarnSpy).toHaveBeenCalledWith(
316 `ReactNativeOnlineDetector listener error:`,
317 expect.any(Error),
318 )
319
320 consoleWarnSpy.mockRestore()
321 detector.dispose()
322 })
323 })
324})