| 1 | // Every layout New slide offers is a batch the ops module and the write |
| 2 | // gate both accept, makes the template it names and the empty blocks it |
| 3 | // lists, and lands where it was asked to. |
| 4 | import test from 'node:test' |
| 5 | import assert from 'node:assert/strict' |
| 6 | import * as Y from 'yjs' |
| 7 | import { applyOps, view } from './ops.mjs' |
| 8 | import { checkDoc } from './check.mjs' |
| 9 | import { LAYOUTS, LAYOUT_NAMES, layoutOps, layoutAfter } from './slide-layouts.mjs' |
| 10 | |
| 11 | let n = 0 |
| 12 | const mint = () => 'blk' + String(++n).padStart(9, '0') |
| 13 | |
| 14 | test('each layout makes its template and its empty blocks, and passes the gate', () => { |
| 15 | for (const name of LAYOUT_NAMES) { |
| 16 | const doc = new Y.Doc() |
| 17 | applyOps(doc, [{ op: 'set_meta', title: 'T', theme: 'paper' }]) |
| 18 | applyOps(doc, layoutOps('slide' + name.replace(/-/g, '').slice(0, 7).padEnd(7, 'x'), name, undefined, mint)) |
| 19 | const s = view(doc).slides[0] |
| 20 | assert.equal(s.template, LAYOUTS[name].template, name) |
| 21 | assert.deepEqual(s.blocks.map(b => b.type), LAYOUTS[name].blocks.map(b => b.type), name) |
| 22 | for (const b of s.blocks) { |
| 23 | for (const p of b.text ?? []) for (const r of p) assert.equal(r.t, '', `${name}: ${b.type} starts empty`) |
| 24 | } |
| 25 | assert.equal(checkDoc(doc), null, name) |
| 26 | } |
| 27 | }) |
| 28 | |
| 29 | test('a layout goes after the slide it is asked to follow', () => { |
| 30 | const doc = new Y.Doc() |
| 31 | applyOps(doc, [ |
| 32 | { op: 'add_slide', id: 'first', layout: 'flow', template: 'title' }, |
| 33 | { op: 'add_slide', id: 'last', layout: 'flow', template: 'title' } |
| 34 | ]) |
| 35 | applyOps(doc, layoutOps('middle', 'quote', 'first', mint)) |
| 36 | assert.deepEqual(view(doc).slides.map(s => s.id), ['first', 'middle', 'last']) |
| 37 | }) |
| 38 | |
| 39 | test('an unknown layout still makes a slide, and plain New slide follows the one before', () => { |
| 40 | assert.equal(layoutOps('s', 'no-such-layout', undefined, mint)[0].template, 'title-body') |
| 41 | assert.equal(layoutAfter('title'), 'title-text') |
| 42 | assert.equal(layoutAfter('two-column'), 'two-column') |
| 43 | assert.equal(layoutAfter('image-full'), 'picture') |
| 44 | assert.equal(layoutAfter(undefined), 'title-text') |
| 45 | }) |
| 46 | |