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