-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.mjs
84 lines (61 loc) · 2.18 KB
/
test.mjs
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
import {strict as assert} from 'assert'
import {dom} from './main.mjs'
console.time('tests')
// Parsing & Serialization ////////////////////////////////////////////////////
const testVar = String(0.5938800190254814)
const fragment = dom`<!doctype html>
${testVar}
<html>
<div>
<!-- foo -->
${dom`<ul id="embedded">${testVar}</ul>`}
${testVar}
<i class="bar">Hi</i>
<link rel="stylesheet" href="/baz.css">
</div>
</html>${testVar}`
const html = fragment.outerHTML
console.log(html)
// Comment should be gone.
assert(!html.includes('<!--'))
assert(!html.includes('-->'))
assert(!html.includes(' foo '))
// General parsing check.
assert(html.includes('<!doctype html>'))
assert(html.includes('</div>'))
assert(html.includes('<i class="bar">'))
assert(html.includes('</i>'))
assert(html.includes('<link rel="stylesheet" href="/baz.css">'))
// Variables should be printed.
assert(html.endsWith(testVar))
assert(html.includes('<div>'))
// The embedded DOM should appear.
assert(html.includes('<div>'))
// <script> are eaten.
assert(dom`<div><script>...this just eaten...</script></div>`)
// <styles> are eaten.
assert(dom`<div><style>...this just eaten...</style></div>`)
// <!doctype html>
const doctype = dom`<!doctype html><html></html>`
assert(doctype.childNodes[0].html == 'true')
// Should strip redundant linebreaks.
assert(!html.includes('\n\n'))
// Query & Manipulation ///////////////////////////////////////////////////////
// querySelector(nodeName)
assert.equal(fragment.querySelector('div').nodeName, 'DIV')
// querySelectorAll(nodeName)
assert.equal(fragment.querySelectorAll('div').length, 1)
// .querySelector(#id)
assert.equal(fragment.querySelector('#embedded').id, 'embedded')
// classList.contains()
assert(fragment.querySelector('.bar').classList.contains('bar'))
// classList.add()
fragment.querySelector('.bar').classList.add('baz')
assert(fragment.querySelector('.bar').classList.contains('baz'))
// classList.remove()
fragment.querySelector('.bar').classList.remove('baz')
assert(!fragment.querySelector('.bar').classList.contains('baz'))
// .remove()
fragment.querySelector('#embedded').remove()
assert.equal(fragment.querySelector('#embedded'), null)
console.timeEnd('tests')