a post-component library for building user-interfaces on the web.

add basic server tests (#51)

authored by tombl.dev and committed by

GitHub 1231ce56 19c3cb8d

+71 -1
+1 -1
src/client/tests/basic.test.ts
··· 140 140 t.assert.strictEqual(el.innerHTML, '<!---->') 141 141 }) 142 142 143 - test('invalid part placement produces warning', { skip: process.env.NODE_ENV === 'production' }, (t: TestContext) => { 143 + test('invalid part placement raises error', { skip: process.env.NODE_ENV === 'production' }, (t: TestContext) => { 144 144 const { root, el } = setup() 145 145 146 146 t.assert.throws(() => root.render(html`<${'div'}>${'text'}</${'div'}>`), {
+5
src/server.ts
··· 188 188 const { _statics: statics, _dynamics: dynamics } = is_html(value) ? value : single_part_template(value) 189 189 const template = compile_template(statics) 190 190 191 + assert( 192 + template.parts.length === dynamics.length, 193 + 'expected the same number of dynamics as parts. do you have a ${...} in an unsupported place?', 194 + ) 195 + 191 196 for (let i = 0; i < template.statics.length - 1; i++) { 192 197 yield template.statics[i] 193 198 yield* template.parts[i](dynamics)
+65
src/server/tests/basic.test.ts
··· 1 + import { html } from 'dhtml' 2 + import { renderToString } from 'dhtml/server' 3 + import test, { type TestContext } from 'node:test' 4 + 5 + globalThis.__DEV__ = process.env.NODE_ENV !== 'production' 6 + 7 + test('basic html renders correctly', (t: TestContext) => { 8 + t.assert.strictEqual(renderToString(html`<h1>Hello, world!</h1>`), '<h1>Hello, world!</h1>') 9 + }) 10 + 11 + test('inner content renders correctly', (t: TestContext) => { 12 + t.assert.strictEqual(renderToString(html`<h1>${html`Inner content!`}</h1>`), '<h1>Inner content!</h1>') 13 + }) 14 + 15 + test('template with number renders correctly', (t: TestContext) => { 16 + const template = (n: number) => html`<h1>Hello, ${n}!</h1>` 17 + t.assert.strictEqual(renderToString(template(1)), '<h1>Hello, 1!</h1>') 18 + t.assert.strictEqual(renderToString(template(2)), '<h1>Hello, 2!</h1>') 19 + }) 20 + 21 + test('basic children render correctly', (t: TestContext) => { 22 + t.assert.strictEqual( 23 + renderToString(html`<span>${'This is a'}</span> ${html`test`} ${html`test`} ${html`test`}`), 24 + '<span>This is a</span> test test test', 25 + ) 26 + }) 27 + 28 + test('errors are thrown cleanly', (t: TestContext) => { 29 + const oops = new Error('oops') 30 + let thrown 31 + try { 32 + renderToString( 33 + html`${{ 34 + render() { 35 + throw oops 36 + }, 37 + }}`, 38 + ) 39 + } catch (error) { 40 + thrown = error 41 + } 42 + t.assert.strictEqual(thrown, oops) 43 + }) 44 + 45 + test('invalid part placement raises error', { skip: process.env.NODE_ENV === 'production' }, (t: TestContext) => { 46 + t.assert.throws(() => renderToString(html`<${'div'}>${'text'}</${'div'}>`)) 47 + }) 48 + 49 + test('parts in comments do not throw', (t: TestContext) => { 50 + renderToString(html`<!-- ${'text'} -->`) 51 + }) 52 + 53 + test( 54 + 'manually specifying internal template syntax throws', 55 + { skip: process.env.NODE_ENV === 'production' }, 56 + (t: TestContext) => { 57 + t.assert.throws(() => { 58 + renderToString(html`${1} dyn-$0$`) 59 + }) 60 + }, 61 + ) 62 + 63 + test('syntax close but not exact does not throw', (t: TestContext) => { 64 + t.assert.strictEqual(renderToString(html`dyn-$${0}1$`), 'dyn-$01$') 65 + })