Actually-useful tests
This commit is contained in:
parent
44fe1ff7b5
commit
a8fd5aa2fe
|
@ -4,6 +4,10 @@
|
||||||
import { template } from '../src/html';
|
import { template } from '../src/html';
|
||||||
import './test-utils';
|
import './test-utils';
|
||||||
|
|
||||||
|
function compareHTML(nodes: ChildNode[], expected: string) {
|
||||||
|
expect(nodes.map((n: any) => n.outerHTML).join('')).toEqual(expected);
|
||||||
|
}
|
||||||
|
|
||||||
describe('basic templating', () => {
|
describe('basic templating', () => {
|
||||||
it('should produce a node', () => {
|
it('should produce a node', () => {
|
||||||
const x = document.createElement('x');
|
const x = document.createElement('x');
|
||||||
|
@ -12,36 +16,29 @@ describe('basic templating', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should substitute a string', () => {
|
it('should substitute a string', () => {
|
||||||
const x = document.createElement('x');
|
|
||||||
const y = 'abc';
|
const y = 'abc';
|
||||||
x.appendChild(document.createTextNode('abc'));
|
compareHTML(template()`<x>${y}</x>`, '<x>abc</x>');
|
||||||
expect('' + template()`<x>${y}</x>`).toEqual('' + [x]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should substitute a node', () => {
|
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()`<z>q</z>`;
|
const y = template()`<z>q</z>`;
|
||||||
expect('' + template()`<x>${y}</x>`).toEqual('' + [x]);
|
compareHTML(template()`<x>${y}</x>`, '<x><z>q</z></x>');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should substitute an array of strings', () => {
|
it('should substitute an array of strings', () => {
|
||||||
const x = document.createElement('x');
|
|
||||||
const y = ['abc', 'def'];
|
const y = ['abc', 'def'];
|
||||||
x.appendChild(document.createTextNode('abcdef'));
|
compareHTML(template()`<x>${y}</x>`, '<x>abcdef</x>');
|
||||||
expect('' + template()`<x>${y}</x>`).toEqual('' + [x]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should substitute an array of strings and nodes', () => {
|
it('should substitute an array of strings and nodes', () => {
|
||||||
const x = document.createElement('x');
|
|
||||||
const y = ['abc', template()`<z>q</z>`, 'def'];
|
const y = ['abc', template()`<z>q</z>`, 'def'];
|
||||||
const z = document.createElement('z');
|
compareHTML(template()`<x>${y}</x>`, '<x>abc<z>q</z>def</x>');
|
||||||
z.appendChild(document.createTextNode('q'));
|
});
|
||||||
x.appendChild(document.createTextNode('abc'));
|
|
||||||
x.appendChild(z);
|
it('should substitute into attributes', () => {
|
||||||
x.appendChild(document.createTextNode('def'));
|
const v = () => 'aaa';
|
||||||
expect('' + template()`<x>${y}</x>`).toEqual('' + [x]);
|
const ps = [() => '123', () => '234'];
|
||||||
|
compareHTML(template()`<q><x t="${v()}">${ps.map(p => p())}</x></q>`,
|
||||||
|
'<q><x t="aaa">123234</x></q>');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue