Open History to see the last commit
1import { OnlineManager } from '../onlineManager'
2import { sleep } from '../utils'
3import { setIsServer } from '../../reactjs/tests/utils'
4β
5describe('onlineManager', () => {
6 let onlineManager: OnlineManager
7 beforeEach(() => {
8 onlineManager = new OnlineManager()
9 })
10β
11 test('isOnline should return true if navigator is undefined', () => {
12 const navigatorSpy = jest.spyOn(globalThis, 'navigator', 'get')
13β
14 // Force navigator to be undefined
15 //@ts-expect-error
16 navigatorSpy.mockImplementation(() => undefined)
17 expect(onlineManager.isOnline()).toBeTruthy()
18β
19 navigatorSpy.mockRestore()
20 })
21β
22 test('isOnline should return true if navigator.onLine is true', () => {
23 const navigatorSpy = jest.spyOn(navigator, 'onLine', 'get')
24 navigatorSpy.mockImplementation(() => true)
25β
26 expect(onlineManager.isOnline()).toBeTruthy()
27β
28 navigatorSpy.mockRestore()
29 })
30β
31 test('setEventListener should use online boolean arg', async () => {
32 let count = 0
33β
34 const setup = (setOnline: (online?: boolean) => void) => {
35 setTimeout(() => {
36 count++
37 setOnline(false)
38 }, 20)
39 return () => void 0
40 }
41β
42 onlineManager.setEventListener(setup)
43β
44 await sleep(30)
45 expect(count).toEqual(1)
46 expect(onlineManager.isOnline()).toBeFalsy()
47 })
48β
49 test('setEventListener should call previous remove handler when replacing an event listener', () => {
50 const remove1Spy = jest.fn()
51 const remove2Spy = jest.fn()
52β
53 onlineManager.setEventListener(() => remove1Spy)
54 onlineManager.setEventListener(() => remove2Spy)
55β
56 expect(remove1Spy).toHaveBeenCalledTimes(1)
57 expect(remove2Spy).not.toHaveBeenCalled()
58 })
59β
60 test('cleanup should still be undefined if window is not defined', async () => {
61 const restoreIsServer = setIsServer(true)
62β
63 const unsubscribe = onlineManager.subscribe(() => undefined)
64 expect(onlineManager['cleanup']).toBeUndefined()
65β
66 unsubscribe()
67 restoreIsServer()
68 })
69β
70 test('cleanup should still be undefined if window.addEventListener is not defined', async () => {
71 const { addEventListener } = globalThis.window
72β
73 // @ts-expect-error
74 globalThis.window.addEventListener = undefined
75β
76 const unsubscribe = onlineManager.subscribe(() => undefined)
77 expect(onlineManager['cleanup']).toBeUndefined()
78β
79 unsubscribe()
80 globalThis.window.addEventListener = addEventListener
81 })
82β
83 test('it should replace default window listener when a new event listener is set', async () => {
84 const addEventListenerSpy = jest.spyOn(
85 globalThis.window,
86 'addEventListener'
87 )
88β
89 const removeEventListenerSpy = jest.spyOn(
90 globalThis.window,
91 'removeEventListener'
92 )
93β
94 // Should set the default event listener with window event listeners
95 const unsubscribe = onlineManager.subscribe(() => undefined)
96 expect(addEventListenerSpy).toHaveBeenCalledTimes(2)
97β
98 // Should replace the window default event listener by a new one
99 // and it should call window.removeEventListener twice
100 onlineManager.setEventListener(() => {
101 return () => void 0
102 })
103β
104 expect(removeEventListenerSpy).toHaveBeenCalledTimes(2)
105β
106 unsubscribe()
107 addEventListenerSpy.mockRestore()
108 removeEventListenerSpy.mockRestore()
109 })
110β
111 test('should call removeEventListener when last listener unsubscribes', () => {
112 const addEventListenerSpy = jest.spyOn(
113 globalThis.window,
114 'addEventListener'
115 )
116β
117 const removeEventListenerSpy = jest.spyOn(
118 globalThis.window,
119 'removeEventListener'
120 )
121β
122 const unsubscribe1 = onlineManager.subscribe(() => undefined)
123 const unsubscribe2 = onlineManager.subscribe(() => undefined)
124 expect(addEventListenerSpy).toHaveBeenCalledTimes(2) // online + offline
125β
126 unsubscribe1()
127 expect(removeEventListenerSpy).toHaveBeenCalledTimes(0)
128 unsubscribe2()
129 expect(removeEventListenerSpy).toHaveBeenCalledTimes(2) // online + offline
130 })
131β
132 test('should keep setup function even if last listener unsubscribes', () => {
133 const setupSpy = jest.fn().mockImplementation(() => () => undefined)
134β
135 onlineManager.setEventListener(setupSpy)
136β
137 const unsubscribe1 = onlineManager.subscribe(() => undefined)
138β
139 expect(setupSpy).toHaveBeenCalledTimes(1)
140β
141 unsubscribe1()
142β
143 const unsubscribe2 = onlineManager.subscribe(() => undefined)
144β
145 expect(setupSpy).toHaveBeenCalledTimes(2)
146β
147 unsubscribe2()
148 })
149})