|
| 1 | +import type { UIDocument, UINode } from 'blocks-schema'; |
| 2 | +import { renderToStaticMarkup } from 'react-dom/server'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | + |
| 5 | +import { readPath, resolveBinding, resolveNodeProps } from '../bindings'; |
| 6 | +import { composeRegistry, registeredTypes } from '../registry'; |
| 7 | +import { DocumentRenderer } from '../renderer'; |
| 8 | +import type { BlockProps, BlockRegistry } from '../types'; |
| 9 | + |
| 10 | +function Passthrough({ node, children }: BlockProps) { |
| 11 | + return ( |
| 12 | + <div data-type={node.type} data-key={node.key}> |
| 13 | + {children} |
| 14 | + </div> |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +function Text({ props }: BlockProps) { |
| 19 | + return <span>{String(props.text ?? '')}</span>; |
| 20 | +} |
| 21 | + |
| 22 | +const registry: BlockRegistry = { |
| 23 | + Page: Passthrough, |
| 24 | + Section: Passthrough, |
| 25 | + Markdown: Text, |
| 26 | +}; |
| 27 | + |
| 28 | +function doc(page: UINode): UIDocument { |
| 29 | + return { formatVersion: '1.0', type: 'UISchema', id: 'doc-1', page }; |
| 30 | +} |
| 31 | + |
| 32 | +describe('DocumentRenderer', () => { |
| 33 | + it('renders the node tree recursively through the registry', () => { |
| 34 | + const html = renderToStaticMarkup( |
| 35 | + <DocumentRenderer |
| 36 | + document={doc({ |
| 37 | + type: 'Page', |
| 38 | + key: 'page', |
| 39 | + props: {}, |
| 40 | + children: [ |
| 41 | + { |
| 42 | + type: 'Section', |
| 43 | + key: 'intro', |
| 44 | + props: {}, |
| 45 | + children: [{ type: 'Markdown', key: 'text', props: { text: 'hello' }, children: [] }], |
| 46 | + }, |
| 47 | + ], |
| 48 | + })} |
| 49 | + registry={registry} |
| 50 | + />, |
| 51 | + ); |
| 52 | + |
| 53 | + expect(html).toContain('data-key="page"'); |
| 54 | + expect(html).toContain('data-key="intro"'); |
| 55 | + expect(html).toContain('<span>hello</span>'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('falls back to UnknownBlock for unregistered node types', () => { |
| 59 | + const html = renderToStaticMarkup( |
| 60 | + <DocumentRenderer |
| 61 | + document={doc({ type: 'Page', key: 'page', props: {}, children: [ |
| 62 | + { type: 'HoloDeck', key: 'holo', props: {}, children: [] }, |
| 63 | + ] })} |
| 64 | + registry={registry} |
| 65 | + />, |
| 66 | + ); |
| 67 | + |
| 68 | + expect(html).toContain('data-block-unknown="HoloDeck"'); |
| 69 | + expect(html).toContain('Unknown block: HoloDeck'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('resolves bindings against the external scope', () => { |
| 73 | + const html = renderToStaticMarkup( |
| 74 | + <DocumentRenderer |
| 75 | + document={doc({ |
| 76 | + type: 'Page', |
| 77 | + key: 'page', |
| 78 | + props: {}, |
| 79 | + children: [ |
| 80 | + { |
| 81 | + type: 'Markdown', |
| 82 | + key: 'text', |
| 83 | + props: { text: 'static' }, |
| 84 | + bindings: { text: '{{ row.title }}' }, |
| 85 | + children: [], |
| 86 | + }, |
| 87 | + ], |
| 88 | + })} |
| 89 | + registry={registry} |
| 90 | + scope={{ row: { title: 'Bound Title' } }} |
| 91 | + />, |
| 92 | + ); |
| 93 | + |
| 94 | + expect(html).toContain('<span>Bound Title</span>'); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('registry layering', () => { |
| 99 | + it('later layers win', () => { |
| 100 | + const composed = composeRegistry(registry, { Markdown: Passthrough }); |
| 101 | + expect(composed.Markdown).toBe(Passthrough); |
| 102 | + expect(composed.Page).toBe(Passthrough); |
| 103 | + expect(registeredTypes(composed)).toEqual(['Markdown', 'Page', 'Section']); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +describe('bindings', () => { |
| 108 | + it('reads dotted paths', () => { |
| 109 | + expect(readPath({ a: { b: { c: 1 } } }, 'a.b.c')).toBe(1); |
| 110 | + expect(readPath({ a: 1 }, 'a.b')).toBeUndefined(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns raw values for single-placeholder expressions', () => { |
| 114 | + expect(resolveBinding('{{ flag }}', { flag: false })).toBe(false); |
| 115 | + expect(resolveBinding('Hello {{ name }}!', { name: 'Ada' })).toBe('Hello Ada!'); |
| 116 | + expect(resolveBinding('Hi {{ missing }}.', {})).toBe('Hi .'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('overlays bindings on static props', () => { |
| 120 | + const node: UINode = { |
| 121 | + type: 'Markdown', |
| 122 | + key: 'k', |
| 123 | + props: { text: 'static', keep: true }, |
| 124 | + bindings: { text: '{{ title }}' }, |
| 125 | + children: [], |
| 126 | + }; |
| 127 | + expect(resolveNodeProps(node, { title: 'dyn' })).toEqual({ text: 'dyn', keep: true }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments