-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
113 lines (103 loc) · 3.42 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import test from 'ava';
import {JSDOM} from 'jsdom';
import doma from './index.js';
const {window} = new JSDOM('');
global.document = window.document;
global.DocumentFragment = window.DocumentFragment;
global.Text = window.Text;
global.Element = window.Element;
global.HTMLElement = window.HTMLElement;
global.HTMLParagraphElement = window.HTMLParagraphElement;
function getHTML(dom) {
const element = document.createElement('div');
element.append(dom);
return element.innerHTML;
}
test('domesticate', t => {
const html = '<wolf></wolf>';
const dom = doma(html);
t.true(dom instanceof DocumentFragment);
t.is(dom.children.length, 1);
t.true(dom.firstChild instanceof Element);
t.is(dom.querySelectorAll('*').length, 1);
t.is(getHTML(dom), html);
t.is(dom.textContent, '');
});
test('domesticate many', t => {
const html = '<cat></cat><strong class="Animal"></strong>';
const dom = doma(html);
t.true(dom instanceof DocumentFragment);
t.is(dom.children.length, 2);
t.true(dom.firstChild instanceof Element);
t.is(dom.firstChild.tagName, 'CAT');
t.true(dom.lastChild instanceof HTMLElement);
t.is(dom.querySelectorAll('*').length, 2);
t.is(getHTML(dom), html);
t.is(dom.textContent, '');
});
test('domesticate offspring', t => {
const html = '<cat><kitten></kitten></cat>';
const dom = doma(html);
t.true(dom instanceof DocumentFragment);
t.is(dom.children.length, 1);
t.true(dom.firstChild instanceof Element);
t.true(dom.querySelector('kitten') instanceof Element);
t.is(dom.querySelectorAll('*').length, 2);
t.is(getHTML(dom), html);
t.is(dom.textContent, '');
});
test('domesticate fantasy', t => {
const html = 'unicorns and rainbows';
const dom = doma(html);
t.true(dom instanceof DocumentFragment);
t.is(dom.childNodes.length, 1);
t.is(dom.children.length, 0);
t.true(dom.firstChild instanceof Text);
t.is(dom.querySelectorAll('*').length, 0);
t.is(getHTML(dom), html);
t.is(dom.textContent, '');
});
test('domesticate animal kingdom', t => {
const html = ' <p>Ci son due <coccodrilli> ed un <orango-tango>, due piccoli <serpenti> e un <aquila reale="">, il <gatto type="cat">, il <topo>, l’<elefante weight="heavy">: non manca più nessuno; solo non si vedono i due <leocorni aria-label="🦄">.</p>';
const dom = doma(html);
t.true(dom instanceof DocumentFragment);
t.is(dom.childNodes.length, 2);
t.is(dom.children.length, 1);
t.true(dom.firstChild instanceof Text);
t.is(dom.firstChild.textContent, ' ');
t.true(dom.lastChild instanceof HTMLParagraphElement);
t.is(dom.querySelectorAll('*').length, 9);
t.is(
getHTML(dom),
html.replace(
'</p>',
'</leocorni></elefante></topo></gatto></aquila></serpenti></orango-tango></coccodrilli></p>',
),
);
t.is(dom.textContent, '');
});
test('domesticate one', t => {
const html = '<animal></animal>';
const dom = doma.one(html);
t.true(dom instanceof Element);
t.is(dom.childNodes.length, 0);
t.is(dom.firstChild, null);
t.is(dom.querySelectorAll('*').length, 0);
t.is(getHTML(dom), html);
t.is(dom.textContent, '');
});
test('domesticate one wish', t => {
const html = 'flying pigs';
const dom = doma.one(html);
t.is(dom, undefined);
});
test('domesticate one dirty farm', t => {
const html = 'go to <town></town> on wild <puns/>';
const dom = doma.one(html);
t.true(dom instanceof Element);
t.is(dom.childNodes.length, 0);
t.is(dom.firstChild, null);
t.is(dom.querySelectorAll('*').length, 0);
t.is(getHTML(dom), '<town></town>');
t.is(dom.textContent, '');
});