// layout-pick.mjs: the card at rest (spec ยง3.1), against the DOM stub. // Eight layout pictures, one lit; what each choice writes; the question // in the card before a template is put on a canvas slide; and the // question withdrawn when the slide changes under it (Review Focus 2). import test from 'node:test' import assert from 'node:assert/strict' import { el, withDocument } from './dom-stub.mjs' import { litFor, pickOps, mountLayoutPick, PICTURES } from './layout-pick.mjs' test('litFor: Free placement on a canvas slide, else the template', () => { assert.equal(litFor({ layout: 'canvas', template: 'title-body' }), 'free') assert.equal(litFor({ layout: 'flow', template: 'quote' }), 'quote') assert.equal(litFor({ layout: 'flow', template: 'blank' }), 'blank') assert.equal(litFor(null), '') }) test('pickOps: the four cases, op for op', () => { const flow = { id: 's1', layout: 'flow', template: 'title-body' } const canvas = { id: 's2', layout: 'canvas', template: 'blank' } assert.deepEqual(pickOps(flow, 'section'), { ops: [{ op: 'set_slide', id: 's1', template: 'section' }], ask: false }) assert.deepEqual(pickOps(flow, 'free'), { ops: [{ op: 'set_slide', id: 's1', layout: 'canvas' }], ask: false }) assert.deepEqual(pickOps(canvas, 'quote'), { ops: [{ op: 'set_slide', id: 's2', layout: 'flow' }, { op: 'set_slide', id: 's2', template: 'quote' }], ask: true }) assert.deepEqual(pickOps(flow, 'title-body'), { ops: [], ask: false }) assert.deepEqual(pickOps(canvas, 'free'), { ops: [], ask: false }) }) test('the pictures are the seven templates, then Free placement', () => { assert.deepEqual(PICTURES, ['title', 'title-body', 'two-column', 'section', 'image-full', 'quote', 'blank', 'free']) }) // The slide form as the server draws it: the eight radios, the question // with its two buttons, and one quiet row with its popover. function card () { const radios = PICTURES.map(v => { const r = el('input', { name: 'slides-layout-pick', value: v }) r.name = 'slides-layout-pick' r.value = v r.checked = false return r }) const pick = el('fieldset', { 'slides-layout-pick': '' }, radios.map(r => el('label', { 'slides-layout-pick-item': '' }, [r]))) const action = el('button', { 'data-command': 'layout-leave-free' }) const cancel = el('button', { 'data-command': 'layout-leave-free-cancel' }) const text = el('p', { id: 'slides-confirm-layout-text', 'slides-confirm-layout-text': '' }) const confirm = el('div', { 'slides-confirm-layout': '' }, [text, el('p', {}, [action, cancel])]) confirm.hidden = true const row = el('button', { popovertarget: 'slides-pop-transition' }) const select = el('select', { name: 'rst-slide-transition' }) const pop = el('div', { id: 'slides-pop-transition', popover: '' }, [select]) let open = true pop.matches = (orig => sel => sel === ':popover-open' ? open : orig.call(pop, sel))(pop.matches) pop.hidePopover = () => { open = false } const form = el('form', {}, [confirm, pick, row, pop]) const lit = () => radios.filter(r => r.checked).map(r => r.value) // The pictures marked as asked about, and what each radio is described by. const asked = () => radios.filter(r => r.parentNode.hasAttribute('data-asked')).map(r => r.value) const described = () => radios.filter(r => r.getAttribute('aria-describedby') === 'slides-confirm-layout-text').map(r => r.value) const choose = v => { const r = radios.find(x => x.value === v) for (const x of radios) x.checked = x === r r.dispatchEvent({ type: 'change' }) } const press = b => b.dispatchEvent({ type: 'click' }) return { form, radios, confirm, action, cancel, row, select, pop, lit, asked, described, choose, press, isOpen: () => open } } function mounted (slide) { const c = card() const commits = [] let current = slide const pick = mountLayoutPick({ form: c.form, getSlide: () => current, commit: ops => { commits.push(ops); return Promise.resolve(true) } }) pick.fill() return { ...c, pick, commits, set: s => { current = s; pick.fill() } } } test('fill lights exactly one picture: the template, or Free placement', () => { const ctx = withDocument() try { const m = mounted({ id: 's1', layout: 'flow', template: 'two-column' }) assert.deepEqual(m.lit(), ['two-column']) m.set({ id: 's1', layout: 'canvas', template: 'two-column' }) assert.deepEqual(m.lit(), ['free']) } finally { ctx.restore() } }) test('a choice on a flow slide commits its op, and asks nothing', () => { const ctx = withDocument() try { const m = mounted({ id: 's1', layout: 'flow', template: 'title-body' }) m.choose('quote') m.choose('free') assert.deepEqual(m.commits, [ [{ op: 'set_slide', id: 's1', template: 'quote' }], [{ op: 'set_slide', id: 's1', layout: 'canvas' }] ]) assert.equal(m.confirm.hidden, true) } finally { ctx.restore() } }) test('a template on a canvas slide asks in the card first and commits nothing', () => { const ctx = withDocument() try { const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) m.choose('section') assert.equal(m.confirm.hidden, false, 'the question shows') assert.equal(ctx.document.activeElement, m.action, 'focus on its action') assert.deepEqual(m.commits, []) assert.deepEqual(m.lit(), ['free'], 'the lit picture is the slide\'s true one while it asks') assert.deepEqual(m.asked(), ['section'], 'the picture asked about is marked apart') assert.deepEqual(m.described(), ['section'], 'and described by the question') // A colleague's keystroke elsewhere refills the card: the question stays. m.set({ id: 's2', layout: 'canvas', template: 'blank', blocks: [] }) assert.equal(m.confirm.hidden, false) assert.deepEqual(m.lit(), ['free']) assert.deepEqual(m.asked(), ['section']) } finally { ctx.restore() } }) test('its action commits both ops in one commit call', () => { const ctx = withDocument() try { const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) m.choose('section') m.press(m.action) assert.deepEqual(m.commits, [[ { op: 'set_slide', id: 's2', layout: 'flow' }, { op: 'set_slide', id: 's2', template: 'section' } ]]) assert.equal(m.confirm.hidden, true) assert.deepEqual(m.lit(), ['section']) assert.deepEqual(m.asked(), [], 'no picture is asked about once answered') assert.deepEqual(m.described(), []) assert.equal(ctx.document.activeElement, m.radios.find(r => r.value === 'section'), 'focus on the picture chosen') } finally { ctx.restore() } }) test('cancel commits nothing and lights Free placement again', () => { const ctx = withDocument() try { const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) m.choose('quote') m.press(m.cancel) assert.deepEqual(m.commits, []) assert.equal(m.confirm.hidden, true) assert.deepEqual(m.lit(), ['free']) assert.deepEqual(m.asked(), []) assert.deepEqual(m.described(), []) assert.equal(ctx.document.activeElement, m.radios.find(r => r.value === 'free'), 'focus back on Free placement') // And the action, pressed after, has nothing to commit. m.press(m.action) assert.deepEqual(m.commits, []) } finally { ctx.restore() } }) test('choosing Free placement again while asked withdraws the question', () => { const ctx = withDocument() try { const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) m.choose('quote') m.choose('free') assert.equal(m.confirm.hidden, true) assert.deepEqual(m.commits, []) assert.deepEqual(m.lit(), ['free']) } finally { ctx.restore() } }) // Review Focus 2: the slide changes under the open question. test('fill after the slide became flow elsewhere withdraws the question, and the action commits nothing', () => { const ctx = withDocument() try { const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) m.choose('section') m.set({ id: 's2', layout: 'flow', template: 'blank' }) assert.equal(m.confirm.hidden, true, 'the question is gone') assert.deepEqual(m.lit(), ['blank'], 'the true value lit') assert.deepEqual(m.asked(), [], 'and its mark') assert.deepEqual(m.described(), []) m.press(m.action) assert.deepEqual(m.commits, []) } finally { ctx.restore() } }) test('fill on another slide, or none, withdraws the question', () => { const ctx = withDocument() try { const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) m.choose('section') m.set({ id: 's3', layout: 'canvas', template: 'blank' }) assert.equal(m.confirm.hidden, true) assert.deepEqual(m.lit(), ['free']) m.choose('quote') m.set(null) assert.equal(m.confirm.hidden, true) assert.deepEqual(m.commits, []) } finally { ctx.restore() } }) test('Escape in a row\'s popover closes it, focuses its row, and goes no further', () => { const ctx = withDocument() try { const m = mounted({ id: 's1', layout: 'flow', template: 'title' }) const ev = { type: 'keydown', key: 'Escape' } m.select.dispatchEvent(ev) assert.equal(m.isOpen(), false) assert.equal(ev.defaultPrevented, true) assert.equal(ev.stopped, true, 'selection.mjs never hears it') assert.equal(ctx.document.activeElement, m.row) } finally { ctx.restore() } }) test('destroy takes every listener off the form', () => { const ctx = withDocument() try { const m = mounted({ id: 's1', layout: 'flow', template: 'title' }) m.pick.destroy() for (const type of ['change', 'click', 'keydown']) assert.equal(m.form.countListeners(type), 0, type) } finally { ctx.restore() } })