// outline.mjs: View → Outline (spec §4.4), against the DOM stub. The // list keeps its rows across fills; opening it fills it and puts focus // on the selected box's row; a row selects its box and closes; Escape // closes and gives focus back, and goes no further. import test from 'node:test' import assert from 'node:assert/strict' import { el as stub, withDocument } from './dom-stub.mjs' import { fillBlocks, mountOutline } from './outline.mjs' // The list is refilled on EVERY document transaction while it is open, // a colleague's keystroke included. So it is reconciled rather than // rebuilt: an unchanged list has to be the SAME button elements // afterwards, or the focus went out of the document with the old ones. test('the Outline\'s list reconciles by id and keeps its rows', () => { const ctx = withDocument() try { const list = stub('div', { 'slides-outline-list': '' }) const none = stub('p', { 'slides-outline-none': '' }) const blocks = [{ id: 'b1', type: 'heading' }, { id: 'b2', type: 'text' }] const label = b => b.type const ids = () => list.children.map(b => b.dataset.selectBlock) fillBlocks({ list, none, blocks, selected: 'b1', label }) assert.deepEqual(ids(), ['b1', 'b2']) assert.equal(none.hidden, true) const [first, second] = list.children assert.equal(first.getAttribute('aria-pressed'), 'true') assert.equal(first.type, 'button') fillBlocks({ list, none, blocks, selected: 'b1', label }) assert.equal(list.children[0], first, 'the same element, not a replacement') assert.equal(list.children[1], second) // A selection change writes the attribute and moves nothing. fillBlocks({ list, none, blocks, selected: 'b2', label }) assert.equal(list.children[0], first) assert.equal(first.getAttribute('aria-pressed'), 'false') assert.equal(second.getAttribute('aria-pressed'), 'true') // Reordering moves the elements that are already there. fillBlocks({ list, none, blocks: [blocks[1], blocks[0]], selected: '', label }) assert.deepEqual(ids(), ['b2', 'b1']) assert.equal(list.children[0], second) assert.equal(list.children[1], first) // A removed block takes its row with it; an empty slide says so. fillBlocks({ list, none, blocks: [blocks[0]], selected: '', label }) assert.deepEqual(ids(), ['b1']) assert.equal(list.children[0], first) fillBlocks({ list, none, blocks: [], selected: '', label }) assert.deepEqual(ids(), []) assert.equal(none.hidden, false) } finally { ctx.restore() } }) function outline (blocks, selected = '') { const list = stub('div', { 'slides-outline-list': '' }) const none = stub('p', { 'slides-outline-none': '' }) const popover = stub('div', { 'slides-outline': '', popover: '' }, [none, list]) const calls = { show: 0, hide: 0, selected: [] } popover.showPopover = () => { calls.show++ } popover.hidePopover = () => { calls.hide++ } const back = stub('summary') const o = mountOutline({ popover, getBlocks: () => blocks, getSelected: () => selected, select: id => calls.selected.push(id), labels: { types: { heading: 'Heading', shape: 'Shape' } } }) return { o, popover, list, none, calls, back } } const blocks = [ { id: 'h1', type: 'heading', text: [[{ t: 'Welcome to ' }, { t: 'Slides' }]] }, { id: 's1', type: 'shape' } ] test('open fills the list by kind and first words, and focuses the selected box\'s row', () => { const ctx = withDocument() try { const { o, list, calls, back } = outline(blocks, 's1') o.fill() assert.equal(list.children.length, 0, 'closed, nothing is filled') o.open({ returnTo: back }) assert.equal(calls.show, 1) assert.equal(o.isOpen(), true) assert.deepEqual(list.children.map(b => b.textContent), ['Heading: Welcome to Slides', 'Shape']) assert.equal(ctx.document.activeElement, list.children[1]) } finally { ctx.restore() } }) test('with no box on the slide it says so, and focus goes to the popover', () => { const ctx = withDocument() try { const { o, popover, none } = outline([]) o.open() assert.equal(none.hidden, false) assert.equal(ctx.document.activeElement, popover) } finally { ctx.restore() } }) test('a row selects its box and closes, leaving focus to the box', () => { const ctx = withDocument() try { const { o, list, calls, back } = outline(blocks) o.open({ returnTo: back }) assert.equal(ctx.document.activeElement, list.children[0], 'nothing selected: the first row') list.children[0].dispatchEvent({ type: 'click' }) assert.deepEqual(calls.selected, ['h1']) assert.equal(calls.hide, 1) assert.equal(o.isOpen(), false) assert.notEqual(ctx.document.activeElement, back, 'focus is the box\'s, not the menu\'s') } finally { ctx.restore() } }) test('Escape closes it, gives focus back, and goes no further', () => { const ctx = withDocument() try { const { o, list, calls, back } = outline(blocks) o.open({ returnTo: back }) const ev = { type: 'keydown', key: 'Escape' } list.children[0].dispatchEvent(ev) assert.equal(ev.defaultPrevented, true) assert.equal(ev.stopped, true, 'the selection\'s own Escape does not hear it') assert.equal(calls.hide, 1) assert.equal(ctx.document.activeElement, back) assert.deepEqual(calls.selected, []) } finally { ctx.restore() } }) test('closed by the browser (a click outside), it fills no more; destroy takes its listeners off', () => { const ctx = withDocument() try { const { o, popover, list } = outline(blocks) o.open() popover.dispatchEvent({ type: 'toggle', newState: 'closed' }) assert.equal(o.isOpen(), false) blocks.push({ id: 'x1', type: 'shape' }) o.fill() assert.equal(list.children.length, 2) blocks.pop() o.destroy() for (const type of ['click', 'keydown', 'toggle']) assert.equal(popover.countListeners(type), 0, type) } finally { ctx.restore() } }) test('a row from the template takes its words in its label, and keeps its check', () => { const ctx = withDocument() try { const list = stub('div', { 'slides-outline-list': '' }) const make = () => stub('button', { 'estilo-menu-item': '' }, [stub('span', { 'estilo-menu-check': '' }), stub('span', { 'estilo-menu-label': '' })]) fillBlocks({ list, none: null, blocks: [{ id: 'b1', type: 'shape' }], selected: 'b1', label: () => 'Shape', make }) const [row] = list.children assert.equal(row.children.length, 2, 'the check is kept') assert.equal(row.querySelector('[estilo-menu-label]').textContent, 'Shape') assert.equal(row.getAttribute('aria-pressed'), 'true') } finally { ctx.restore() } }) test('open keeps the panel inside the window: moved back from the end edge, no taller than the room under its title', () => { const ctx = withDocument() const was = { w: globalThis.innerWidth, h: globalThis.innerHeight } try { globalThis.innerWidth = 320 globalThis.innerHeight = 700 const { o, popover, back } = outline(blocks) popover.style = {} popover.getBoundingClientRect = () => ({ width: 304 }) back.getBoundingClientRect = () => ({ left: 200, bottom: 60 }) o.open({ returnTo: back }) assert.equal(popover.style.left, '8px', 'its title is 200px in, the panel 304px wide: it starts 8px in') assert.equal(popover.style.top, '60px') assert.equal(popover.style.maxBlockSize, '632px') o.close() back.getBoundingClientRect = () => ({ left: 10, bottom: 60 }) globalThis.innerWidth = 1000 o.open({ returnTo: back }) assert.equal(popover.style.left, '10px', 'room enough: under its title') } finally { globalThis.innerWidth = was.w globalThis.innerHeight = was.h ctx.restore() } })