// @vitest-environment happy-dom import { afterEach, describe, expect, it } from 'vitest'; import { canTakeFocus, containsFocus, deepActiveElementOf, focusablesIn, isUnavailable, tabbablesIn, usesKey, } from '../src/focusable'; afterEach(() => { document.body.innerHTML = ''; }); const html = (markup: string): HTMLElement => { const root = document.createElement('div'); root.innerHTML = markup; document.body.append(root); return root; }; describe('focusablesIn', () => { it('finds what takes focus, in order, without the disabled, the hidden and the inert', () => { const root = html(` Link Not a link C
F
G
`); expect(focusablesIn(root).map((element) => element.id)).toEqual([ 'a', 'link', 'field', 'programmatic', 'editable', ]); expect(tabbablesIn(root).map((element) => element.id)).toEqual([ 'a', 'link', 'field', 'editable', ]); expect(canTakeFocus(root.querySelector('#disabled')!)).toBe(false); }); it('takes what a slot shows: the page elements assigned to it, in place of its own content', () => { const host = html(` `); const shadow = host.attachShadow({ mode: 'open' }); shadow.innerHTML = ` `; expect(focusablesIn(shadow).map((element) => element.id)).toEqual([ 'before', 'slotted', 'shown', ]); }); }); describe('isUnavailable', () => { it('is true for disabled controls and aria-disabled ones', () => { const root = html(` Off `); expect(isUnavailable(root.querySelector('#on')!)).toBe(false); expect(isUnavailable(root.querySelector('#off')!)).toBe(true); expect(isUnavailable(root.querySelector('#aria-off')!)).toBe(true); }); }); describe('usesKey', () => { it('leaves a text field its caret keys, a select its Up and Down, a slider every arrow', () => { const root = html(` `); const element = (id: string) => root.querySelector(`#${id}`)!; expect(usesKey(element('text'), 'ArrowLeft')).toBe(true); expect(usesKey(element('text'), 'Home')).toBe(true); expect(usesKey(element('text'), 'ArrowDown')).toBe(false); expect(usesKey(element('select'), 'ArrowDown')).toBe(true); expect(usesKey(element('select'), 'ArrowRight')).toBe(false); expect(usesKey(element('range'), 'ArrowUp')).toBe(true); expect(usesKey(element('range'), 'ArrowRight')).toBe(true); expect(usesKey(element('button'), 'ArrowRight')).toBe(false); expect(usesKey(element('checkbox'), 'ArrowRight')).toBe(false); expect(usesKey(element('text'), 'Enter')).toBe(false); }); }); describe('deepActiveElementOf', () => { it('looks into shadow roots, and is null while nothing has focus', () => { expect(deepActiveElementOf(document)).toBeNull(); const host = html(''); const shadow = host.attachShadow({ mode: 'open' }); shadow.innerHTML = ''; const inside = shadow.querySelector('button')!; inside.focus(); expect(deepActiveElementOf(document)).toBe(inside); }); }); describe('containsFocus', () => { it('sees focus inside through a shadow root, and in a control slotted into one', () => { const host = html(''); const shadow = host.attachShadow({ mode: 'open' }); shadow.innerHTML = '
'; const viewer = shadow.getElementById('viewer')!; const outside = html('').querySelector('button')!; shadow.querySelector('button')!.focus(); expect(containsFocus(viewer)).toBe(true); // Its parent is the host, outside the viewer; the slot it shows in is inside. happy-dom // assigns it to the slot but has no `assignedSlot`, which browsers give it. const slotted = document.getElementById('slotted')!; Object.defineProperty(slotted, 'assignedSlot', { value: shadow.querySelector('slot') }); slotted.focus(); expect(containsFocus(viewer)).toBe(true); outside.focus(); expect(containsFocus(viewer)).toBe(false); }); });