import { describe, expect, it, vi } from 'vitest'; import { focusAutofocusElement, focusElement, getAutofocusElement } from '../../src/focus.js'; function create(html: string): HTMLElement[] { document.body.innerHTML = html; return Array.from(document.body.children) as HTMLElement[]; } describe('focus', () => { describe('focusElement', () => { it('focuses an element', () => { const [element] = create(''); focusElement(element); expect(document.activeElement).toBe(element); }); it('prevents scroll on focus', () => { const [element] = create(''); const focusMock = vi.spyOn(element, 'focus'); focusElement(element); expect(focusMock).toHaveBeenCalledWith({ preventScroll: true }); }); it('accepts an element selector', () => { const [element] = create(''); focusElement('button'); expect(document.activeElement).toBe(element); }); it('fails silently for missing elements', () => { // @ts-expect-error invalid argument expect(() => focusElement(null)).not.toThrow(); expect(() => focusElement('input')).not.toThrow(); }); it('sets a tab index', () => { const [element] = create(''); focusElement(element); expect(element.tabIndex).toBe(-1); }); it('restores the previous tabindex', () => { const [element] = create(''); focusElement(element); expect(element.tabIndex).toBe(4); }); }); describe('getAutofocusElement', () => { it('finds the first autofocus element', () => { const [first] = create(''); expect(getAutofocusElement()).toBe(first); }); it('ignores elements in inert containers', () => { create('
'); expect(getAutofocusElement()).toBeUndefined(); }); it('ignores elements in aria-disabled containers', () => { create(''); expect(getAutofocusElement()).toBeUndefined(); }); it('ignores elements in aria-hidden containers', () => { create(''); expect(getAutofocusElement()).toBeUndefined(); }); }); describe('focusAutofocusElement', () => { it('focuses the autofocus element', () => { const [element] = create(''); const focusMock = vi.spyOn(element, 'focus'); focusAutofocusElement(); expect(focusMock).toHaveBeenCalledWith(); }); }); });