-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.js
140 lines (113 loc) · 3.6 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const test = require('ava');
const {JSDOM} = require('jsdom');
const baseDomify = require('./index.js');
const jsdom = new JSDOM();
const domify = html => baseDomify(html, jsdom.window.document);
test('converts HTML to DOM elements', t => {
const element = domify('<p>Hello</p>');
t.is(element.nodeName, 'P');
t.is(element.textContent, 'Hello');
const elements = domify('<p>one</p><p>two</p><p>three</p>');
t.is(elements.textContent, 'onetwothree');
});
test('ignores trailing/leading whitespace', t => {
const element = domify(' <p>Hello</p> ');
t.is(element.nodeName, 'P');
t.is(element.textContent, 'Hello');
});
test('supports body tags', t => {
const element = domify('<body></body>');
t.is(element.nodeName, 'BODY');
});
test('supports body tags with classes', t => {
const element = domify('<body class="page"></body>');
t.is(element.nodeName, 'BODY');
t.is(element.className, 'page');
});
test('supports legend tags', t => {
const element = domify('<legend>Hello</legend>');
t.is(element.nodeName, 'LEGEND');
});
test('supports table tags', t => {
const element = domify('<table></table>');
t.is(element.nodeName, 'TABLE');
});
test('supports thead tags', t => {
const element = domify('<thead></thead>');
t.is(element.nodeName, 'THEAD');
});
test('supports tbody tags', t => {
const element = domify('<tbody></tbody>');
t.is(element.nodeName, 'TBODY');
});
test('supports tfoot tags', t => {
const element = domify('<tfoot></tfoot>');
t.is(element.nodeName, 'TFOOT');
});
test('supports caption tags', t => {
const element = domify('<caption></caption>');
t.is(element.nodeName, 'CAPTION');
});
test('supports col tags', t => {
const element = domify('<col></col>');
t.is(element.nodeName, 'COL');
});
test('supports td tags', t => {
const element = domify('<td></td>');
t.is(element.nodeName, 'TD');
});
test('supports th tags', t => {
const element = domify('<th></th>');
t.is(element.nodeName, 'TH');
});
test('supports tr tags', t => {
const element = domify('<tr></tr>');
t.is(element.nodeName, 'TR');
});
test('supports script tags', t => {
const element = domify('<script src="example.js"></script>');
t.is(element.nodeName, 'SCRIPT');
});
test('supports option tags', t => {
const element = domify('<option></option>');
t.is(element.nodeName, 'OPTION');
});
test('supports optgroup tags', t => {
const element = domify('<optgroup></optgroup>');
t.is(element.nodeName, 'OPTGROUP');
});
test('does not set parentElement', t => {
const element = domify('<p>Hello</p>');
t.falsy(element.parentElement);
t.falsy(element.parentNode);
});
test('supports text', t => {
const element = domify('text goes here');
t.is(element.nodeName, '#text');
});
test('preserves trailing/leading spaces for textElement', t => {
const element = domify(' text goes here ');
t.is(element.nodeName, '#text');
t.is(element.textContent, ' text goes here ');
});
test('handles comments', t => {
const comment = domify('<!-- mycomment -->');
t.is(comment.nodeType, jsdom.window.Node.COMMENT_NODE);
t.is(comment.data, ' mycomment ');
});
test('handles comments with leading/trailing spaces', t => {
const comment = domify('<!-- mycomment -->');
t.is(comment.nodeType, jsdom.window.Node.COMMENT_NODE);
t.is(comment.data, ' mycomment ');
});
if (globalThis.SVGElement) {
test('svg - supports path tag', t => {
t.true(domify('<path></path>') instanceof globalThis.SVGPathElement);
});
test('svg - supports rect tag', t => {
t.true(domify('<rect></rect>') instanceof globalThis.SVGRectElement);
});
test('svg - supports g tag', t => {
t.true(domify('<g></g>') instanceof globalThis.SVGGElement);
});
}