| 1 | // layout-pick.mjs: the card at rest (spec §3.1), against the DOM stub. |
| 2 | // Eight layout pictures, one lit; what each choice writes; the question |
| 3 | // in the card before a template is put on a canvas slide; and the |
| 4 | // question withdrawn when the slide changes under it (Review Focus 2). |
| 5 | import test from 'node:test' |
| 6 | import assert from 'node:assert/strict' |
| 7 | import { el, withDocument } from './dom-stub.mjs' |
| 8 | import { litFor, pickOps, mountLayoutPick, PICTURES } from './layout-pick.mjs' |
| 9 | |
| 10 | test('litFor: Free placement on a canvas slide, else the template', () => { |
| 11 | assert.equal(litFor({ layout: 'canvas', template: 'title-body' }), 'free') |
| 12 | assert.equal(litFor({ layout: 'flow', template: 'quote' }), 'quote') |
| 13 | assert.equal(litFor({ layout: 'flow', template: 'blank' }), 'blank') |
| 14 | assert.equal(litFor(null), '') |
| 15 | }) |
| 16 | |
| 17 | test('pickOps: the four cases, op for op', () => { |
| 18 | const flow = { id: 's1', layout: 'flow', template: 'title-body' } |
| 19 | const canvas = { id: 's2', layout: 'canvas', template: 'blank' } |
| 20 | assert.deepEqual(pickOps(flow, 'section'), { ops: [{ op: 'set_slide', id: 's1', template: 'section' }], ask: false }) |
| 21 | assert.deepEqual(pickOps(flow, 'free'), { ops: [{ op: 'set_slide', id: 's1', layout: 'canvas' }], ask: false }) |
| 22 | assert.deepEqual(pickOps(canvas, 'quote'), { |
| 23 | ops: [{ op: 'set_slide', id: 's2', layout: 'flow' }, { op: 'set_slide', id: 's2', template: 'quote' }], |
| 24 | ask: true |
| 25 | }) |
| 26 | assert.deepEqual(pickOps(flow, 'title-body'), { ops: [], ask: false }) |
| 27 | assert.deepEqual(pickOps(canvas, 'free'), { ops: [], ask: false }) |
| 28 | }) |
| 29 | |
| 30 | test('the pictures are the seven templates, then Free placement', () => { |
| 31 | assert.deepEqual(PICTURES, ['title', 'title-body', 'two-column', 'section', 'image-full', 'quote', 'blank', 'free']) |
| 32 | }) |
| 33 | |
| 34 | // The slide form as the server draws it: the eight radios, the question |
| 35 | // with its two buttons, and one quiet row with its popover. |
| 36 | function card () { |
| 37 | const radios = PICTURES.map(v => { |
| 38 | const r = el('input', { name: 'slides-layout-pick', value: v }) |
| 39 | r.name = 'slides-layout-pick' |
| 40 | r.value = v |
| 41 | r.checked = false |
| 42 | return r |
| 43 | }) |
| 44 | const pick = el('fieldset', { 'slides-layout-pick': '' }, radios.map(r => el('label', { 'slides-layout-pick-item': '' }, [r]))) |
| 45 | const action = el('button', { 'data-command': 'layout-leave-free' }) |
| 46 | const cancel = el('button', { 'data-command': 'layout-leave-free-cancel' }) |
| 47 | const text = el('p', { id: 'slides-confirm-layout-text', 'slides-confirm-layout-text': '' }) |
| 48 | const confirm = el('div', { 'slides-confirm-layout': '' }, [text, el('p', {}, [action, cancel])]) |
| 49 | confirm.hidden = true |
| 50 | const row = el('button', { popovertarget: 'slides-pop-transition' }) |
| 51 | const select = el('select', { name: 'rst-slide-transition' }) |
| 52 | const pop = el('div', { id: 'slides-pop-transition', popover: '' }, [select]) |
| 53 | let open = true |
| 54 | pop.matches = (orig => sel => sel === ':popover-open' ? open : orig.call(pop, sel))(pop.matches) |
| 55 | pop.hidePopover = () => { open = false } |
| 56 | const form = el('form', {}, [confirm, pick, row, pop]) |
| 57 | const lit = () => radios.filter(r => r.checked).map(r => r.value) |
| 58 | // The pictures marked as asked about, and what each radio is described by. |
| 59 | const asked = () => radios.filter(r => r.parentNode.hasAttribute('data-asked')).map(r => r.value) |
| 60 | const described = () => radios.filter(r => r.getAttribute('aria-describedby') === 'slides-confirm-layout-text').map(r => r.value) |
| 61 | const choose = v => { |
| 62 | const r = radios.find(x => x.value === v) |
| 63 | for (const x of radios) x.checked = x === r |
| 64 | r.dispatchEvent({ type: 'change' }) |
| 65 | } |
| 66 | const press = b => b.dispatchEvent({ type: 'click' }) |
| 67 | return { form, radios, confirm, action, cancel, row, select, pop, lit, asked, described, choose, press, isOpen: () => open } |
| 68 | } |
| 69 | |
| 70 | function mounted (slide) { |
| 71 | const c = card() |
| 72 | const commits = [] |
| 73 | let current = slide |
| 74 | const pick = mountLayoutPick({ form: c.form, getSlide: () => current, commit: ops => { commits.push(ops); return Promise.resolve(true) } }) |
| 75 | pick.fill() |
| 76 | return { ...c, pick, commits, set: s => { current = s; pick.fill() } } |
| 77 | } |
| 78 | |
| 79 | test('fill lights exactly one picture: the template, or Free placement', () => { |
| 80 | const ctx = withDocument() |
| 81 | try { |
| 82 | const m = mounted({ id: 's1', layout: 'flow', template: 'two-column' }) |
| 83 | assert.deepEqual(m.lit(), ['two-column']) |
| 84 | m.set({ id: 's1', layout: 'canvas', template: 'two-column' }) |
| 85 | assert.deepEqual(m.lit(), ['free']) |
| 86 | } finally { ctx.restore() } |
| 87 | }) |
| 88 | |
| 89 | test('a choice on a flow slide commits its op, and asks nothing', () => { |
| 90 | const ctx = withDocument() |
| 91 | try { |
| 92 | const m = mounted({ id: 's1', layout: 'flow', template: 'title-body' }) |
| 93 | m.choose('quote') |
| 94 | m.choose('free') |
| 95 | assert.deepEqual(m.commits, [ |
| 96 | [{ op: 'set_slide', id: 's1', template: 'quote' }], |
| 97 | [{ op: 'set_slide', id: 's1', layout: 'canvas' }] |
| 98 | ]) |
| 99 | assert.equal(m.confirm.hidden, true) |
| 100 | } finally { ctx.restore() } |
| 101 | }) |
| 102 | |
| 103 | test('a template on a canvas slide asks in the card first and commits nothing', () => { |
| 104 | const ctx = withDocument() |
| 105 | try { |
| 106 | const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) |
| 107 | m.choose('section') |
| 108 | assert.equal(m.confirm.hidden, false, 'the question shows') |
| 109 | assert.equal(ctx.document.activeElement, m.action, 'focus on its action') |
| 110 | assert.deepEqual(m.commits, []) |
| 111 | assert.deepEqual(m.lit(), ['free'], 'the lit picture is the slide\'s true one while it asks') |
| 112 | assert.deepEqual(m.asked(), ['section'], 'the picture asked about is marked apart') |
| 113 | assert.deepEqual(m.described(), ['section'], 'and described by the question') |
| 114 | // A colleague's keystroke elsewhere refills the card: the question stays. |
| 115 | m.set({ id: 's2', layout: 'canvas', template: 'blank', blocks: [] }) |
| 116 | assert.equal(m.confirm.hidden, false) |
| 117 | assert.deepEqual(m.lit(), ['free']) |
| 118 | assert.deepEqual(m.asked(), ['section']) |
| 119 | } finally { ctx.restore() } |
| 120 | }) |
| 121 | |
| 122 | test('its action commits both ops in one commit call', () => { |
| 123 | const ctx = withDocument() |
| 124 | try { |
| 125 | const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) |
| 126 | m.choose('section') |
| 127 | m.press(m.action) |
| 128 | assert.deepEqual(m.commits, [[ |
| 129 | { op: 'set_slide', id: 's2', layout: 'flow' }, |
| 130 | { op: 'set_slide', id: 's2', template: 'section' } |
| 131 | ]]) |
| 132 | assert.equal(m.confirm.hidden, true) |
| 133 | assert.deepEqual(m.lit(), ['section']) |
| 134 | assert.deepEqual(m.asked(), [], 'no picture is asked about once answered') |
| 135 | assert.deepEqual(m.described(), []) |
| 136 | assert.equal(ctx.document.activeElement, m.radios.find(r => r.value === 'section'), 'focus on the picture chosen') |
| 137 | } finally { ctx.restore() } |
| 138 | }) |
| 139 | |
| 140 | test('cancel commits nothing and lights Free placement again', () => { |
| 141 | const ctx = withDocument() |
| 142 | try { |
| 143 | const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) |
| 144 | m.choose('quote') |
| 145 | m.press(m.cancel) |
| 146 | assert.deepEqual(m.commits, []) |
| 147 | assert.equal(m.confirm.hidden, true) |
| 148 | assert.deepEqual(m.lit(), ['free']) |
| 149 | assert.deepEqual(m.asked(), []) |
| 150 | assert.deepEqual(m.described(), []) |
| 151 | assert.equal(ctx.document.activeElement, m.radios.find(r => r.value === 'free'), 'focus back on Free placement') |
| 152 | // And the action, pressed after, has nothing to commit. |
| 153 | m.press(m.action) |
| 154 | assert.deepEqual(m.commits, []) |
| 155 | } finally { ctx.restore() } |
| 156 | }) |
| 157 | |
| 158 | test('choosing Free placement again while asked withdraws the question', () => { |
| 159 | const ctx = withDocument() |
| 160 | try { |
| 161 | const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) |
| 162 | m.choose('quote') |
| 163 | m.choose('free') |
| 164 | assert.equal(m.confirm.hidden, true) |
| 165 | assert.deepEqual(m.commits, []) |
| 166 | assert.deepEqual(m.lit(), ['free']) |
| 167 | } finally { ctx.restore() } |
| 168 | }) |
| 169 | |
| 170 | // Review Focus 2: the slide changes under the open question. |
| 171 | test('fill after the slide became flow elsewhere withdraws the question, and the action commits nothing', () => { |
| 172 | const ctx = withDocument() |
| 173 | try { |
| 174 | const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) |
| 175 | m.choose('section') |
| 176 | m.set({ id: 's2', layout: 'flow', template: 'blank' }) |
| 177 | assert.equal(m.confirm.hidden, true, 'the question is gone') |
| 178 | assert.deepEqual(m.lit(), ['blank'], 'the true value lit') |
| 179 | assert.deepEqual(m.asked(), [], 'and its mark') |
| 180 | assert.deepEqual(m.described(), []) |
| 181 | m.press(m.action) |
| 182 | assert.deepEqual(m.commits, []) |
| 183 | } finally { ctx.restore() } |
| 184 | }) |
| 185 | |
| 186 | test('fill on another slide, or none, withdraws the question', () => { |
| 187 | const ctx = withDocument() |
| 188 | try { |
| 189 | const m = mounted({ id: 's2', layout: 'canvas', template: 'blank' }) |
| 190 | m.choose('section') |
| 191 | m.set({ id: 's3', layout: 'canvas', template: 'blank' }) |
| 192 | assert.equal(m.confirm.hidden, true) |
| 193 | assert.deepEqual(m.lit(), ['free']) |
| 194 | m.choose('quote') |
| 195 | m.set(null) |
| 196 | assert.equal(m.confirm.hidden, true) |
| 197 | assert.deepEqual(m.commits, []) |
| 198 | } finally { ctx.restore() } |
| 199 | }) |
| 200 | |
| 201 | test('Escape in a row\'s popover closes it, focuses its row, and goes no further', () => { |
| 202 | const ctx = withDocument() |
| 203 | try { |
| 204 | const m = mounted({ id: 's1', layout: 'flow', template: 'title' }) |
| 205 | const ev = { type: 'keydown', key: 'Escape' } |
| 206 | m.select.dispatchEvent(ev) |
| 207 | assert.equal(m.isOpen(), false) |
| 208 | assert.equal(ev.defaultPrevented, true) |
| 209 | assert.equal(ev.stopped, true, 'selection.mjs never hears it') |
| 210 | assert.equal(ctx.document.activeElement, m.row) |
| 211 | } finally { ctx.restore() } |
| 212 | }) |
| 213 | |
| 214 | test('destroy takes every listener off the form', () => { |
| 215 | const ctx = withDocument() |
| 216 | try { |
| 217 | const m = mounted({ id: 's1', layout: 'flow', template: 'title' }) |
| 218 | m.pick.destroy() |
| 219 | for (const type of ['change', 'click', 'keydown']) assert.equal(m.form.countListeners(type), 0, type) |
| 220 | } finally { ctx.restore() } |
| 221 | }) |
| 222 | |