Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { FocusManager } from '../focusManager'
3β
4describe('focusManager', () => {
5 let focusManager: FocusManager
6β
7 beforeEach(() => {
8 vi.useFakeTimers()
9 vi.resetModules()
10 focusManager = new FocusManager()
11 })
12β
13 afterEach(() => {
14 vi.useRealTimers()
15 })
16β
17 it('should call previous remove handler when replacing an event listener', () => {
18 const remove1Spy = vi.fn<() => void>()
19 const remove2Spy = vi.fn<() => void>()
20β
21 focusManager.setEventListener(() => remove1Spy)
22 focusManager.setEventListener(() => remove2Spy)
23β
24 expect(remove1Spy).toHaveBeenCalledTimes(1)
25 expect(remove2Spy).not.toHaveBeenCalled()
26 })
27β
28 it('should use focused boolean arg', () => {
29 let count = 0
30β
31 const setup = (setFocused: (focused?: boolean) => void) => {
32 setTimeout(() => {
33 count++
34 setFocused(true)
35 }, 20)
36 return () => void 0
37 }
38β
39 focusManager.setEventListener(setup)
40β
41 vi.advanceTimersByTime(20)
42 expect(count).toBe(1)
43 expect(focusManager.isFocused()).toBe(true)
44 })
45β
46 it('should return true for isFocused if document is undefined', () => {
47 const { document } = globalThis
48β
49 // @ts-expect-error
50 delete globalThis.document
51β
52 focusManager.setFocused()
53 expect(focusManager.isFocused()).toBe(true)
54 globalThis.document = document
55 })
56β
57 it('should not call cleanup (removeEventListener) if window is not defined', () => {
58 const windowSpy = vi.spyOn(globalThis, 'window', 'get')
59 windowSpy.mockReturnValue(
60 undefined as unknown as Window & typeof globalThis,
61 )
62 const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
63β
64 const subscribe = () => focusManager.subscribe(() => undefined)
65 let firstUnsubscribe: (() => void) | undefined
66β
67 expect(() => {
68 firstUnsubscribe = subscribe()
69 }).not.toThrow()
70 const secondUnsubscribe = subscribe()
71β
72 firstUnsubscribe?.()
73 secondUnsubscribe()
74β
75 expect(removeEventListenerSpy).not.toHaveBeenCalled()
76 })
77β
78 it('should not call cleanup (removeEventListener) if window.addEventListener is not defined', () => {
79 const { addEventListener } = globalThis.window
80β
81 // @ts-expect-error
82 globalThis.window.addEventListener = undefined
83β
84 const removeEventListenerSpy = vi.spyOn(globalThis, 'removeEventListener')
85β
86 const unsubscribe = focusManager.subscribe(() => undefined)
87β
88 unsubscribe()
89 expect(removeEventListenerSpy).not.toHaveBeenCalled()
90β
91 globalThis.window.addEventListener = addEventListener
92 })
93β
94 it('should replace default window listener when a new event listener is set', () => {
95 const unsubscribeSpy = vi.fn<() => undefined>().mockReturnValue(undefined)
96 const handlerSpy = vi
97 .fn<() => () => undefined>()
98 .mockReturnValue(unsubscribeSpy)
99β
100 focusManager.setEventListener(() => handlerSpy())
101β
102 const unsubscribe = focusManager.subscribe(() => undefined)
103β
104 // Should call the custom event once
105 expect(handlerSpy).toHaveBeenCalledTimes(1)
106β
107 unsubscribe()
108β
109 // Should unsubscribe our event listener once
110 expect(unsubscribeSpy).toHaveBeenCalledTimes(1)
111 })
112β
113 it('should call removeEventListener when last listener unsubscribes', () => {
114 const addEventListenerSpy = vi.spyOn(globalThis.window, 'addEventListener')
115β
116 const removeEventListenerSpy = vi.spyOn(
117 globalThis.window,
118 'removeEventListener',
119 )
120β
121 const unsubscribe1 = focusManager.subscribe(() => undefined)
122 const unsubscribe2 = focusManager.subscribe(() => undefined)
123 expect(addEventListenerSpy).toHaveBeenCalledTimes(1) // visibilitychange event
124β
125 unsubscribe1()
126 expect(removeEventListenerSpy).toHaveBeenCalledTimes(0)
127β
128 unsubscribe2()
129 expect(removeEventListenerSpy).toHaveBeenCalledTimes(1) // visibilitychange event
130 })
131β
132 it('should keep setup function even if last listener unsubscribes', () => {
133 const setupSpy = vi
134 .fn<() => () => undefined>()
135 .mockReturnValue(() => undefined)
136β
137 focusManager.setEventListener(setupSpy)
138β
139 const unsubscribe1 = focusManager.subscribe(() => undefined)
140 expect(setupSpy).toHaveBeenCalledTimes(1)
141β
142 unsubscribe1()
143β
144 const unsubscribe2 = focusManager.subscribe(() => undefined)
145 expect(setupSpy).toHaveBeenCalledTimes(2)
146β
147 unsubscribe2()
148 })
149β
150 it('should call listeners when setFocused is called', () => {
151 const listener = vi.fn<(focused: boolean) => void>()
152β
153 focusManager.subscribe(listener)
154β
155 focusManager.setFocused(true)
156 focusManager.setFocused(true)
157 expect(listener).toHaveBeenCalledTimes(1)
158 expect(listener).toHaveBeenNthCalledWith(1, true)
159β
160 focusManager.setFocused(false)
161 focusManager.setFocused(false)
162 expect(listener).toHaveBeenCalledTimes(2)
163 expect(listener).toHaveBeenNthCalledWith(2, false)
164β
165 focusManager.setFocused(undefined)
166 focusManager.setFocused(undefined)
167 expect(listener).toHaveBeenCalledTimes(3)
168 expect(listener).toHaveBeenNthCalledWith(3, true)
169 })
170})