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