// fixtures/toolbar-roving.test.mjs — the formatting bar's roving tab // stop reaches only controls that are drawn (spec I2, revision 2.1's C2). // // From 64rem the bar is the inspector, which the person can close; its // controls are then display: none but still in the roving list. A tab // stop parked on one, or Escape in the editor focusing one, sends focus // nowhere. setupRoving's drawn() asks checkVisibility(); these hold it to // that without a browser (review on #106: the filter had no test that // could fail). import { test } from 'node:test' import assert from 'node:assert/strict' import { setupRoving } from './toolbar-roving.mjs' globalThis.document = { activeElement: null } // A control as setupRoving reads it: enabled or not, drawn or not, and a // record of every focus() it is given. function control(name, { drawn = true, disabled = false, tabIndex = -1 } = {}) { return { name, disabled, tabIndex, drawn, focused: 0, checkVisibility() { return this.drawn }, focus() { this.focused++; document.activeElement = this }, } } function root() { const handlers = {} return { handlers, addEventListener(type, fn) { handlers[type] = fn } } } test('the tab stop skips a control that is not drawn', () => { const hidden = control('hidden', { drawn: false }) const shown = control('shown') setupRoving(root(), [hidden, shown]) assert.equal(hidden.tabIndex, -1, 'the stop was parked on a control that is not drawn') assert.equal(shown.tabIndex, 0) }) test('with nothing drawn, focusStop reports false and focuses nothing', () => { const a = control('a', { drawn: false, tabIndex: 0 }) const b = control('b', { drawn: false }) const roving = setupRoving(root(), [a, b]) assert.equal(roving.focusStop(), false) assert.equal(a.focused + b.focused, 0, 'focus was given to a control that is not drawn') }) test('a stop whose control stops being drawn moves to one that is', () => { const a = control('a') const b = control('b') const roving = setupRoving(root(), [a, b]) assert.equal(a.tabIndex, 0) a.drawn = false roving.ensureReachable() assert.equal(b.tabIndex, 0, 'the stop stayed on a control that is no longer drawn') assert.equal(roving.focusStop(), true) assert.equal(b.focused, 1) assert.equal(a.focused, 0) }) test('the arrow keys walk only the drawn controls', () => { const r = root() const a = control('a') const gone = control('gone', { drawn: false }) const c = control('c') setupRoving(r, [a, gone, c]) a.focus() r.handlers.keydown({ key: 'ArrowRight', preventDefault() {} }) assert.equal(document.activeElement, c, 'ArrowRight landed on a control that is not drawn') })