|
| 1 | +import { id } from './core.ts'; |
| 2 | +import { close, open, text as ttyText } from '@bomb.sh/tty'; |
| 3 | +import { describe, expect, test } from 'vitest'; |
| 4 | +import { useBoxElement } from './elements/box.ts'; |
| 5 | +import { useTextElement } from './elements/text.ts'; |
| 6 | +import { createHost } from './host.ts'; |
| 7 | +import { layout } from './layout.ts'; |
| 8 | + |
| 9 | +describe('layout', () => { |
| 10 | + describe('box', () => { |
| 11 | + test('lays out host children in element order', () => { |
| 12 | + const host = createHost(); |
| 13 | + useBoxElement(host); |
| 14 | + |
| 15 | + const parent = host.createElement('box'); |
| 16 | + const first = host.createElement('box'); |
| 17 | + const second = host.createElement('box'); |
| 18 | + |
| 19 | + host.insertBefore(parent, first); |
| 20 | + host.insertBefore(parent, second); |
| 21 | + host.insertBefore(host.element, parent); |
| 22 | + host.insertBefore(parent, second, first); |
| 23 | + |
| 24 | + expect([...layout(host.root)]).toEqual([ |
| 25 | + open(id(parent.node!)), |
| 26 | + open(id(second.node!)), |
| 27 | + close(), |
| 28 | + open(id(first.node!)), |
| 29 | + close(), |
| 30 | + close(), |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + test('coalesces runs of text literals around child elements', () => { |
| 35 | + const host = createHost(); |
| 36 | + useBoxElement(host); |
| 37 | + |
| 38 | + const parent = host.createElement('box'); |
| 39 | + const child = host.createElement('box'); |
| 40 | + host.insertBefore(parent, host.createLiteral('Hello')); |
| 41 | + host.insertBefore(parent, host.createLiteral(', ')); |
| 42 | + host.insertBefore(parent, child); |
| 43 | + host.insertBefore(parent, host.createLiteral('World')); |
| 44 | + host.insertBefore(parent, host.createLiteral('!')); |
| 45 | + host.insertBefore(host.element, parent); |
| 46 | + |
| 47 | + expect([...layout(host.root)]).toEqual([ |
| 48 | + open(id(parent.node!)), |
| 49 | + ttyText('Hello, '), |
| 50 | + open(id(child.node!)), |
| 51 | + close(), |
| 52 | + ttyText('World!'), |
| 53 | + close(), |
| 54 | + ]); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('text', () => { |
| 59 | + test('coalesces literals with the enclosing properties', () => { |
| 60 | + const host = createHost(); |
| 61 | + useTextElement(host); |
| 62 | + |
| 63 | + const element = host.createElement('text'); |
| 64 | + host.setProperty(element, 'color', 1); |
| 65 | + host.insertBefore(element, host.createLiteral('Hello')); |
| 66 | + host.insertBefore(element, host.createLiteral(', ')); |
| 67 | + host.insertBefore(element, host.createLiteral('World!')); |
| 68 | + host.insertBefore(host.element, element); |
| 69 | + |
| 70 | + expect([...layout(element.node!)]).toEqual([ttyText('Hello, World!', { color: 1 })]); |
| 71 | + }); |
| 72 | + |
| 73 | + test('coalesces text literals at the host root', () => { |
| 74 | + const host = createHost(); |
| 75 | + host.insertBefore(host.element, host.createLiteral('Hello')); |
| 76 | + host.insertBefore(host.element, host.createLiteral(', world!')); |
| 77 | + |
| 78 | + expect([...layout(host.root)]).toEqual([ttyText('Hello, world!')]); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments