|
5 | 5 | * LICENSE file in the root directory of this source tree.
|
6 | 6 | */
|
7 | 7 |
|
8 |
| -import { wrapFocus } from '../wrapFocus'; |
| 8 | +import { wrapFocus, wrapFocusWithoutSentinels } from '../wrapFocus'; |
9 | 9 |
|
10 | 10 | describe('wrapFocus', () => {
|
11 | 11 | let node;
|
@@ -146,3 +146,54 @@ describe('wrapFocus', () => {
|
146 | 146 | expect(spyInnerModal).toHaveBeenCalled();
|
147 | 147 | });
|
148 | 148 | });
|
| 149 | + |
| 150 | +describe('wrapFocusWithoutSentinels', () => { |
| 151 | + let container; |
| 152 | + let button1; |
| 153 | + |
| 154 | + beforeEach(() => { |
| 155 | + container = document.createElement('div'); |
| 156 | + container.innerHTML = ` |
| 157 | + <button id="button-0">Button 0</button> |
| 158 | + <button id="button-1">Button 1</button> |
| 159 | + `; |
| 160 | + document.body.appendChild(container); |
| 161 | + button1 = container.querySelector('#button-1'); |
| 162 | + }); |
| 163 | + |
| 164 | + afterEach(() => { |
| 165 | + document.body.removeChild(container); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should not throw an error when called during keydown event', () => { |
| 169 | + const event = new KeyboardEvent('keydown', { key: 'Tab' }); |
| 170 | + Object.defineProperty(event, 'shiftKey', { value: false }); |
| 171 | + |
| 172 | + expect(() => |
| 173 | + wrapFocusWithoutSentinels({ |
| 174 | + containerNode: container, |
| 175 | + currentActiveNode: button1, |
| 176 | + event, |
| 177 | + }) |
| 178 | + ).not.toThrow(); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should throw an error when called during unsupported event', () => { |
| 182 | + const event = new FocusEvent('focusin'); |
| 183 | + const originalEnv = process.env.NODE_ENV; |
| 184 | + |
| 185 | + process.env.NODE_ENV = 'development'; |
| 186 | + |
| 187 | + expect(() => |
| 188 | + wrapFocusWithoutSentinels({ |
| 189 | + containerNode: container, |
| 190 | + currentActiveNode: button1, |
| 191 | + event, |
| 192 | + }) |
| 193 | + ).toThrow( |
| 194 | + /^Error: wrapFocusWithoutSentinels\(\.\.\.\) called in unsupported focusin event\.\s+Call wrapFocusWithoutSentinels\(\.\.\.\) from onKeyDown instead\.$/ |
| 195 | + ); |
| 196 | + |
| 197 | + process.env.NODE_ENV = originalEnv; |
| 198 | + }); |
| 199 | +}); |
0 commit comments