| 1 |
import { describe, expect, it, vi } from 'vitest'; |
| 2 |
import { focusAutofocusElement, focusElement, getAutofocusElement } from '../../src/focus.js'; |
| 3 |
|
| 4 |
function create(html: string): HTMLElement[] { |
| 5 |
document.body.innerHTML = html; |
| 6 |
return Array.from(document.body.children) as HTMLElement[]; |
| 7 |
} |
| 8 |
|
| 9 |
describe('focus', () => { |
| 10 |
describe('focusElement', () => { |
| 11 |
it('focuses an element', () => { |
| 12 |
const [element] = create('<button></button>'); |
| 13 |
focusElement(element); |
| 14 |
expect(document.activeElement).toBe(element); |
| 15 |
}); |
| 16 |
|
| 17 |
it('prevents scroll on focus', () => { |
| 18 |
const [element] = create('<button></button>'); |
| 19 |
const focusMock = vi.spyOn(element, 'focus'); |
| 20 |
focusElement(element); |
| 21 |
expect(focusMock).toHaveBeenCalledWith({ preventScroll: true }); |
| 22 |
}); |
| 23 |
|
| 24 |
it('accepts an element selector', () => { |
| 25 |
const [element] = create('<button></button>'); |
| 26 |
focusElement('button'); |
| 27 |
expect(document.activeElement).toBe(element); |
| 28 |
}); |
| 29 |
|
| 30 |
it('fails silently for missing elements', () => { |
| 31 |
// @ts-expect-error invalid argument |
| 32 |
expect(() => focusElement(null)).not.toThrow(); |
| 33 |
expect(() => focusElement('input')).not.toThrow(); |
| 34 |
}); |
| 35 |
|
| 36 |
it('sets a tab index', () => { |
| 37 |
const [element] = create('<button></button>'); |
| 38 |
focusElement(element); |
| 39 |
expect(element.tabIndex).toBe(-1); |
| 40 |
}); |
| 41 |
|
| 42 |
it('restores the previous tabindex', () => { |
| 43 |
const [element] = create('<button tabindex="4"></button>'); |
| 44 |
focusElement(element); |
| 45 |
expect(element.tabIndex).toBe(4); |
| 46 |
}); |
| 47 |
}); |
| 48 |
|
| 49 |
describe('getAutofocusElement', () => { |
| 50 |
it('finds the first autofocus element', () => { |
| 51 |
const [first] = create('<button autofocus></button><input autofocus>'); |
| 52 |
expect(getAutofocusElement()).toBe(first); |
| 53 |
}); |
| 54 |
|
| 55 |
it('ignores elements in inert containers', () => { |
| 56 |
create('<div inert><button autofocus></button></div>'); |
| 57 |
expect(getAutofocusElement()).toBeUndefined(); |
| 58 |
}); |
| 59 |
|
| 60 |
it('ignores elements in aria-disabled containers', () => { |
| 61 |
create('<div aria-disabled><button autofocus></button></div>'); |
| 62 |
expect(getAutofocusElement()).toBeUndefined(); |
| 63 |
}); |
| 64 |
|
| 65 |
it('ignores elements in aria-hidden containers', () => { |
| 66 |
create('<div aria-hidden="true"><button autofocus></button></div>'); |
| 67 |
expect(getAutofocusElement()).toBeUndefined(); |
| 68 |
}); |
| 69 |
}); |
| 70 |
|
| 71 |
describe('focusAutofocusElement', () => { |
| 72 |
it('focuses the autofocus element', () => { |
| 73 |
const [element] = create('<button autofocus></button>'); |
| 74 |
const focusMock = vi.spyOn(element, 'focus'); |
| 75 |
focusAutofocusElement(); |
| 76 |
expect(focusMock).toHaveBeenCalledWith(); |
| 77 |
}); |
| 78 |
}); |
| 79 |
}); |