oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/toolbar-roving.test.mjs — the formatting bar's roving tab
2// stop reaches only controls that are drawn (spec I2, revision 2.1's C2).
3//
4// From 64rem the bar is the inspector, which the person can close; its
5// controls are then display: none but still in the roving list. A tab
6// stop parked on one, or Escape in the editor focusing one, sends focus
7// nowhere. setupRoving's drawn() asks checkVisibility(); these hold it to
8// that without a browser (review on #106: the filter had no test that
9// could fail).
10import { test } from 'node:test'
11import assert from 'node:assert/strict'
12import { setupRoving } from './toolbar-roving.mjs'
13
14globalThis.document = { activeElement: null }
15
16// A control as setupRoving reads it: enabled or not, drawn or not, and a
17// record of every focus() it is given.
18function control(name, { drawn = true, disabled = false, tabIndex = -1 } = {}) {
19 return {
20 name,
21 disabled,
22 tabIndex,
23 drawn,
24 focused: 0,
25 checkVisibility() { return this.drawn },
26 focus() { this.focused++; document.activeElement = this },
27 }
28}
29
30function root() {
31 const handlers = {}
32 return { handlers, addEventListener(type, fn) { handlers[type] = fn } }
33}
34
35test('the tab stop skips a control that is not drawn', () => {
36 const hidden = control('hidden', { drawn: false })
37 const shown = control('shown')
38 setupRoving(root(), [hidden, shown])
39 assert.equal(hidden.tabIndex, -1, 'the stop was parked on a control that is not drawn')
40 assert.equal(shown.tabIndex, 0)
41})
42
43test('with nothing drawn, focusStop reports false and focuses nothing', () => {
44 const a = control('a', { drawn: false, tabIndex: 0 })
45 const b = control('b', { drawn: false })
46 const roving = setupRoving(root(), [a, b])
47 assert.equal(roving.focusStop(), false)
48 assert.equal(a.focused + b.focused, 0, 'focus was given to a control that is not drawn')
49})
50
51test('a stop whose control stops being drawn moves to one that is', () => {
52 const a = control('a')
53 const b = control('b')
54 const roving = setupRoving(root(), [a, b])
55 assert.equal(a.tabIndex, 0)
56 a.drawn = false
57 roving.ensureReachable()
58 assert.equal(b.tabIndex, 0, 'the stop stayed on a control that is no longer drawn')
59 assert.equal(roving.focusStop(), true)
60 assert.equal(b.focused, 1)
61 assert.equal(a.focused, 0)
62})
63
64test('the arrow keys walk only the drawn controls', () => {
65 const r = root()
66 const a = control('a')
67 const gone = control('gone', { drawn: false })
68 const c = control('c')
69 setupRoving(r, [a, gone, c])
70 a.focus()
71 r.handlers.keydown({ key: 'ArrowRight', preventDefault() {} })
72 assert.equal(document.activeElement, c, 'ArrowRight landed on a control that is not drawn')
73})
74