|
1 |
| -import * as React from 'react'; |
2 |
| - |
3 | 1 | import {
|
4 | 2 | focusFirstDescendant,
|
5 | 3 | focusNextFocusableElement,
|
6 | 4 | focusPreviousFocusableElement,
|
7 | 5 | getFocusableDescendants,
|
8 |
| - trapFocus, |
| 6 | + getFocusableDescendantsV2, |
9 | 7 | } from '../focusHandlers';
|
10 | 8 |
|
11 | 9 | test('focusFirstDescendant - It focuses a button', () => {
|
@@ -87,61 +85,6 @@ test('focusFirstDescendant - It does not focus on an element with negative tabin
|
87 | 85 | expect(button).not.toHaveFocus();
|
88 | 86 | });
|
89 | 87 |
|
90 |
| -test('trapFocus - will focus on the first element when try to scape from forward or backward', () => { |
91 |
| - const focusableSection = document.createElement('section'); |
92 |
| - focusableSection.id = 'test-section'; |
93 |
| - |
94 |
| - const button1Focusable = document.createElement('button'); |
95 |
| - button1Focusable.id = 'button1Focusable'; |
96 |
| - |
97 |
| - const button2Focusable = document.createElement('button'); |
98 |
| - button2Focusable.id = 'button2Focusable'; |
99 |
| - |
100 |
| - const button3Focusable = document.createElement('button'); |
101 |
| - button3Focusable.id = 'button3Focusable'; |
102 |
| - |
103 |
| - focusableSection.appendChild(button1Focusable); |
104 |
| - focusableSection.appendChild(button2Focusable); |
105 |
| - focusableSection.appendChild(button3Focusable); |
106 |
| - |
107 |
| - document.body.appendChild(focusableSection); |
108 |
| - |
109 |
| - // Focus last element |
110 |
| - button3Focusable.focus(); |
111 |
| - |
112 |
| - // Trying to forward scape redirect the focus to the first one |
113 |
| - const mockEventForwardScape = { |
114 |
| - shiftKey: false, |
115 |
| - preventDefault: jest.fn(), |
116 |
| - } as unknown as React.KeyboardEvent<HTMLElement>; |
117 |
| - trapFocus(focusableSection, mockEventForwardScape); |
118 |
| - expect(document.activeElement).toBe(button1Focusable); |
119 |
| - |
120 |
| - // Focus in the first element |
121 |
| - button1Focusable.focus(); |
122 |
| - |
123 |
| - // Trying to backward scape redirect the focus to the first one |
124 |
| - const mockEventBackwardScape = { |
125 |
| - shiftKey: true, |
126 |
| - preventDefault: jest.fn(), |
127 |
| - } as unknown as React.KeyboardEvent<HTMLElement>; |
128 |
| - trapFocus(focusableSection, mockEventBackwardScape); |
129 |
| - expect(document.activeElement).toBe(button3Focusable); |
130 |
| -}); |
131 |
| - |
132 |
| -test('trapFocus - It executes preventDefault if there are nothing to get the focus', () => { |
133 |
| - const element = document.createElement('div'); |
134 |
| - |
135 |
| - const mockPreventDefault = jest.fn(); |
136 |
| - const event = { |
137 |
| - key: 'ArrowDown', |
138 |
| - preventDefault: mockPreventDefault, |
139 |
| - } as unknown as React.KeyboardEvent<HTMLInputElement>; |
140 |
| - |
141 |
| - trapFocus(element, event); |
142 |
| - expect(mockPreventDefault).toHaveBeenCalled(); |
143 |
| -}); |
144 |
| - |
145 | 88 | test('focusNextFocusableElement - The next element focusable is the brother', () => {
|
146 | 89 | const element = document.createElement('div');
|
147 | 90 | const button1 = document.createElement('button');
|
@@ -208,3 +151,66 @@ test('focusPreviousFocusableElement - The previous element focusable is the brot
|
208 | 151 | const result = focusPreviousFocusableElement(button2);
|
209 | 152 | expect(result).toBeTruthy();
|
210 | 153 | });
|
| 154 | + |
| 155 | +describe('getFocusableDescendantsV2', () => { |
| 156 | + it('Filter disabled', () => { |
| 157 | + const element = document.createElement('div'); |
| 158 | + const button = document.createElement('button'); |
| 159 | + button.disabled = true; |
| 160 | + element.appendChild(button); |
| 161 | + |
| 162 | + const result = getFocusableDescendantsV2({ element }); |
| 163 | + expect(result).toHaveLength(0); |
| 164 | + }); |
| 165 | + |
| 166 | + it('Filter aria-disabled elements', () => { |
| 167 | + const element = document.createElement('div'); |
| 168 | + const button = document.createElement('button'); |
| 169 | + button.setAttribute('aria-disabled', 'true'); |
| 170 | + element.appendChild(button); |
| 171 | + |
| 172 | + const result = getFocusableDescendantsV2({ element }); |
| 173 | + expect(result).toHaveLength(0); |
| 174 | + }); |
| 175 | + |
| 176 | + it('Filter aria-hidden attributes', () => { |
| 177 | + const element = document.createElement('div'); |
| 178 | + const button = document.createElement('button'); |
| 179 | + button.setAttribute('aria-hidden', 'true'); |
| 180 | + element.appendChild(button); |
| 181 | + |
| 182 | + const result = getFocusableDescendantsV2({ element }); |
| 183 | + expect(result).toHaveLength(0); |
| 184 | + }); |
| 185 | + |
| 186 | + it('Filter elements to omit', () => { |
| 187 | + const element = document.createElement('div'); |
| 188 | + const button = document.createElement('button'); |
| 189 | + const omit = document.createElement('div'); |
| 190 | + omit.appendChild(button); |
| 191 | + element.appendChild(omit); |
| 192 | + |
| 193 | + const result = getFocusableDescendantsV2({ element, elementsToOmit: [omit] }); |
| 194 | + expect(result).toHaveLength(0); |
| 195 | + }); |
| 196 | + |
| 197 | + it('Filter elements that are not summary and are inside a closed details', () => { |
| 198 | + const element = document.createElement('div'); |
| 199 | + const button = document.createElement('button'); |
| 200 | + const details = document.createElement('details'); |
| 201 | + details.appendChild(button); |
| 202 | + element.appendChild(details); |
| 203 | + |
| 204 | + const result = getFocusableDescendantsV2({ element }); |
| 205 | + expect(result).toHaveLength(0); |
| 206 | + }); |
| 207 | + |
| 208 | + it('Return focusable elements', () => { |
| 209 | + const element = document.createElement('div'); |
| 210 | + const button = document.createElement('button'); |
| 211 | + element.appendChild(button); |
| 212 | + |
| 213 | + const result = getFocusableDescendantsV2({ element }); |
| 214 | + expect(result).toHaveLength(1); |
| 215 | + }); |
| 216 | +}); |
0 commit comments