|
| 1 | +import { assertEquals, assertThrows } from '@std/assert' |
| 2 | +import Helper from '@app/Helper.ts' |
| 3 | +import Create from '@app/Create.ts' |
| 4 | + |
| 5 | +const el = Helper.el |
| 6 | + |
| 7 | +Deno.test('create(el.root(...)) produces valid schema', () => { |
| 8 | + const schema = Create.create( |
| 9 | + el.root( |
| 10 | + el.header( |
| 11 | + { id: 'header', layout: { width: '100%', height: 56 }, style: { fill: '#1a1a2e' } }, |
| 12 | + el.h1({ id: 'title', style: { font: '20px sans-serif', fill: '#eee' } }, 'Hello') |
| 13 | + ), |
| 14 | + el.main({ layout: { width: '100%', flex: 1 } }, el.a({ attrs: { href: '/' } }, 'Home')) |
| 15 | + ) |
| 16 | + ) |
| 17 | + assertEquals(schema.root.length, 2) |
| 18 | + assertEquals(schema.root[0]?.type, 'header') |
| 19 | + assertEquals(schema.root[0]?.children?.[0]?.type, 'h1') |
| 20 | + assertEquals(schema.root[0]?.children?.[0]?.content, 'Hello') |
| 21 | + assertEquals(schema.root[1]?.type, 'main') |
| 22 | + assertEquals(schema.root[1]?.children?.[0]?.type, 'a') |
| 23 | + assertEquals(schema.root[1]?.children?.[0]?.content, 'Home') |
| 24 | +}) |
| 25 | + |
| 26 | +Deno.test('el.div with children returns children array', () => { |
| 27 | + const node = el.div(el.span('a'), el.span('b')) |
| 28 | + assertEquals(node.type, 'div') |
| 29 | + assertEquals(node.children?.length, 2) |
| 30 | + assertEquals(node.children?.[0]?.content, 'a') |
| 31 | + assertEquals(node.children?.[1]?.content, 'b') |
| 32 | +}) |
| 33 | + |
| 34 | +Deno.test('el.div with props and children', () => { |
| 35 | + const node = el.div({ id: 'box' }, el.span('a'), el.span('b')) |
| 36 | + assertEquals(node.type, 'div') |
| 37 | + assertEquals(node.id, 'box') |
| 38 | + assertEquals(node.children?.length, 2) |
| 39 | +}) |
| 40 | + |
| 41 | +Deno.test('el.div with props and string returns content', () => { |
| 42 | + const node = el.div({ id: 'x' }, 'hello') |
| 43 | + assertEquals(node.type, 'div') |
| 44 | + assertEquals(node.id, 'x') |
| 45 | + assertEquals(node.content, 'hello') |
| 46 | +}) |
| 47 | + |
| 48 | +Deno.test('el.div with props returns node with id layout style attrs', () => { |
| 49 | + const node = el.div({ |
| 50 | + id: 'x', |
| 51 | + layout: { width: 100 }, |
| 52 | + style: { fill: '#f00' }, |
| 53 | + attrs: { 'data-test': 'ok' } |
| 54 | + }) |
| 55 | + assertEquals(node.type, 'div') |
| 56 | + assertEquals(node.id, 'x') |
| 57 | + assertEquals(node.layout?.width, 100) |
| 58 | + assertEquals(node.style?.fill, '#f00') |
| 59 | + assertEquals(node.attrs?.['data-test'], 'ok') |
| 60 | +}) |
| 61 | + |
| 62 | +Deno.test('el.div with string returns content', () => { |
| 63 | + const node = el.div('hello') |
| 64 | + assertEquals(node.type, 'div') |
| 65 | + assertEquals(node.content, 'hello') |
| 66 | +}) |
| 67 | + |
| 68 | +Deno.test('el.div() returns node with type div', () => { |
| 69 | + const node = el.div() |
| 70 | + assertEquals(node.type, 'div') |
| 71 | +}) |
| 72 | + |
| 73 | +Deno.test('el.img with no args returns img node', () => { |
| 74 | + const node = el.img() |
| 75 | + assertEquals(node.type, 'img') |
| 76 | +}) |
| 77 | + |
| 78 | +Deno.test('el.img with content throws', () => { |
| 79 | + assertThrows( |
| 80 | + () => el.img('text' as unknown as Record<string, unknown>), |
| 81 | + Error, |
| 82 | + 'void tag "img" must not have content or children' |
| 83 | + ) |
| 84 | +}) |
| 85 | + |
| 86 | +Deno.test('el.img with props returns img with src alt', () => { |
| 87 | + const node = el.img({ src: '/x.png', alt: 'image' }) |
| 88 | + assertEquals(node.type, 'img') |
| 89 | + assertEquals(node.src, '/x.png') |
| 90 | + assertEquals(node.alt, 'image') |
| 91 | +}) |
| 92 | + |
| 93 | +Deno.test('el.node container with string and children throws', () => { |
| 94 | + assertThrows( |
| 95 | + () => el.node('div', 'text', el.span('x')), |
| 96 | + Error, |
| 97 | + 'content (string) and children cannot be mixed' |
| 98 | + ) |
| 99 | +}) |
| 100 | + |
| 101 | +Deno.test('el.node normalises tag to lowercase', () => { |
| 102 | + const node = el.node('DIV') |
| 103 | + assertEquals(node.type, 'div') |
| 104 | +}) |
| 105 | + |
| 106 | +Deno.test('el.node void tag with children throws', () => { |
| 107 | + assertThrows( |
| 108 | + () => el.node('img', el.span('x')), |
| 109 | + Error, |
| 110 | + 'void tag "img" must not have content or children' |
| 111 | + ) |
| 112 | +}) |
| 113 | + |
| 114 | +Deno.test('el.node void tag with only props does not throw', () => { |
| 115 | + const node = el.input({ id: 'field', attrs: { placeholder: 'Enter' } }) |
| 116 | + assertEquals(node.type, 'input') |
| 117 | + assertEquals(node.id, 'field') |
| 118 | + assertEquals(node.attrs?.['placeholder'], 'Enter') |
| 119 | +}) |
| 120 | + |
| 121 | +Deno.test('el.node with first child then array of children', () => { |
| 122 | + const node = el.node('div', el.span('a'), [el.span('b'), el.span('c')]) |
| 123 | + assertEquals(node.type, 'div') |
| 124 | + assertEquals(node.children?.length, 3) |
| 125 | + assertEquals(node.children?.[0]?.content, 'a') |
| 126 | + assertEquals(node.children?.[1]?.content, 'b') |
| 127 | + assertEquals(node.children?.[2]?.content, 'c') |
| 128 | +}) |
| 129 | + |
| 130 | +Deno.test('el.root with single node returns definition with one node', () => { |
| 131 | + const rootDefinition = el.root(el.p('only')) |
| 132 | + assertEquals(rootDefinition.root.length, 1) |
| 133 | + assertEquals(rootDefinition.root[0]?.type, 'p') |
| 134 | + assertEquals(rootDefinition.root[0]?.content, 'only') |
| 135 | +}) |
| 136 | + |
| 137 | +Deno.test('el.root() returns empty root', () => { |
| 138 | + const rootDefinition = el.root() |
| 139 | + assertEquals(rootDefinition.root.length, 0) |
| 140 | +}) |
| 141 | + |
| 142 | +Deno.test('el.root([n1, n2]) accepts single array', () => { |
| 143 | + const rootDefinition = el.root([el.div(), el.span()]) |
| 144 | + assertEquals(rootDefinition.root.length, 2) |
| 145 | + assertEquals(rootDefinition.root[0]?.type, 'div') |
| 146 | + assertEquals(rootDefinition.root[1]?.type, 'span') |
| 147 | +}) |
| 148 | + |
| 149 | +Deno.test('el.root(n1, n2) returns definition with two nodes', () => { |
| 150 | + const rootDefinition = el.root(el.div(), el.span()) |
| 151 | + assertEquals(rootDefinition.root.length, 2) |
| 152 | + assertEquals(rootDefinition.root[0]?.type, 'div') |
| 153 | + assertEquals(rootDefinition.root[1]?.type, 'span') |
| 154 | +}) |
| 155 | + |
| 156 | +Deno.test('el.section exists and returns node with type section', () => { |
| 157 | + const node = el.section(el.header('Header')) |
| 158 | + assertEquals(node.type, 'section') |
| 159 | + assertEquals(node.children?.length, 1) |
| 160 | + assertEquals(node.children?.[0]?.type, 'header') |
| 161 | +}) |
| 162 | + |
| 163 | +Deno.test('Helper.node() static equals Helper.el.node()', () => { |
| 164 | + const resultA = Helper.node('span', 'text') |
| 165 | + const resultB = Helper.el.node('span', 'text') |
| 166 | + assertEquals(resultA.type, resultB.type) |
| 167 | + assertEquals(resultA.content, resultB.content) |
| 168 | +}) |
| 169 | + |
| 170 | +Deno.test('Helper.node() static equals el.node()', () => { |
| 171 | + const resultA = Helper.node('div', { id: 'x' }) |
| 172 | + const resultB = el.node('div', { id: 'x' }) |
| 173 | + assertEquals(resultA.type, resultB.type) |
| 174 | + assertEquals(resultA.id, resultB.id) |
| 175 | +}) |
| 176 | + |
| 177 | +Deno.test('Helper.root() and el.root() are same', () => { |
| 178 | + const resultA = Helper.root(el.div()) |
| 179 | + const resultB = el.root(el.div()) |
| 180 | + assertEquals(resultA.root.length, resultB.root.length) |
| 181 | + assertEquals(resultA.root[0]?.type, resultB.root[0]?.type) |
| 182 | +}) |
| 183 | + |
| 184 | +Deno.test('Helper.root() static equals el.root()', () => { |
| 185 | + const resultA = Helper.root(el.div(), el.span()) |
| 186 | + const resultB = el.root(el.div(), el.span()) |
| 187 | + assertEquals(resultA.root.length, resultB.root.length) |
| 188 | + assertEquals(resultA.root[0]?.type, resultB.root[0]?.type) |
| 189 | + assertEquals(resultA.root[1]?.type, resultB.root[1]?.type) |
| 190 | +}) |
| 191 | + |
| 192 | +Deno.test('node strips unknown keys from props', () => { |
| 193 | + const node = el.node('div', { id: 'x', foo: 'bar' } as unknown as Record<string, unknown>) |
| 194 | + assertEquals(node.type, 'div') |
| 195 | + assertEquals(node.id, 'x') |
| 196 | + assertEquals('foo' in node, false) |
| 197 | +}) |
0 commit comments