diff --git a/packages/html2/test/html.test.ts b/packages/html2/test/html.test.ts index a0ae91f..1fa4205 100644 --- a/packages/html2/test/html.test.ts +++ b/packages/html2/test/html.test.ts @@ -4,6 +4,10 @@ import { template } from '../src/html'; import './test-utils'; +function compareHTML(nodes: ChildNode[], expected: string) { + expect(nodes.map((n: any) => n.outerHTML).join('')).toEqual(expected); +} + describe('basic templating', () => { it('should produce a node', () => { const x = document.createElement('x'); @@ -12,36 +16,29 @@ describe('basic templating', () => { }); it('should substitute a string', () => { - const x = document.createElement('x'); const y = 'abc'; - x.appendChild(document.createTextNode('abc')); - expect('' + template()`${y}`).toEqual('' + [x]); + compareHTML(template()`${y}`, 'abc'); }); it('should substitute a node', () => { - const x = document.createElement('x'); - const z = document.createElement('z'); - z.appendChild(document.createTextNode('q')); - x.appendChild(z); const y = template()`q`; - expect('' + template()`${y}`).toEqual('' + [x]); + compareHTML(template()`${y}`, 'q'); }); it('should substitute an array of strings', () => { - const x = document.createElement('x'); const y = ['abc', 'def']; - x.appendChild(document.createTextNode('abcdef')); - expect('' + template()`${y}`).toEqual('' + [x]); + compareHTML(template()`${y}`, 'abcdef'); }); it('should substitute an array of strings and nodes', () => { - const x = document.createElement('x'); const y = ['abc', template()`q`, 'def']; - const z = document.createElement('z'); - z.appendChild(document.createTextNode('q')); - x.appendChild(document.createTextNode('abc')); - x.appendChild(z); - x.appendChild(document.createTextNode('def')); - expect('' + template()`${y}`).toEqual('' + [x]); + compareHTML(template()`${y}`, 'abcqdef'); + }); + + it('should substitute into attributes', () => { + const v = () => 'aaa'; + const ps = [() => '123', () => '234']; + compareHTML(template()`${ps.map(p => p())}`, + '123234'); }); });