This repository was archived by the owner on Sep 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragment.html
219 lines (211 loc) · 8.11 KB
/
fragment.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link href="https://unpkg.com/mocha@4.0.1/mocha.css" rel="stylesheet" />
<link rel="stylesheet" href="/test/styles.css">
<link rel="icon" type="image/png" href="/resources/favicon-16x16.png" sizes="16x16" />
<link rel="icon" type="image/png" href="/resources/favicon-32x32.png" sizes="32x32" />
</head>
<body>
<nav onclick="location.pathname='/test/index.html'">
<h1>Mocha Tests - Composi Fragment Tag</h1>
</nav>
<section>
<div id="mocha"></div>
<div id="messages"></div>
<div id="fixtures"></div>
<div id="hide-tests">
<div id="fragment-test"></div>
<div id="fragment-test2"></div>
<ul id="fragment-test3"></ul>
</div>
</section>
<script src="https://unpkg.com/mocha@4.0.1/mocha.js"></script>
<script src="https://unpkg.com/chai@4.1.2/chai.js"></script>
<script src='/test/composi.js'></script>
<script>mocha.setup('bdd')</script>
<script>
const {h, render, Fragment} = composi
let expect = chai.expect
describe('Examine Fragment tag virtual node', function() {
const letters = ['A', 'B', 'C', 'D', 'E', 'F']
function NestFragments({letters}) {
return (
h(
'main',
{},
[
Fragment(
null,
[
h('span', {}, ['A']),
h('span', {}, ['B']),
Fragment(null, [
h('span', {}, ['C']),
h('span', {}, ['D']),
Fragment(null, [
h('span', {}, ['E']),
h('span', {}, ['F']),
])
])
]
)
]
)
)
}
const fragmentVnode = NestFragments({letters})
window.fragmentVnode = fragmentVnode
it('VNode should have six children', function() {
expect(fragmentVnode.children.length).to.equal(6)
})
it('Children should have value of "A", "B", "C", "D", "E", "F"', function() {
expect(fragmentVnode.children[0].children[0].type).to.equal('A')
expect(fragmentVnode.children[1].children[0].type).to.equal('B')
expect(fragmentVnode.children[2].children[0].type).to.equal('C')
expect(fragmentVnode.children[3].children[0].type).to.equal('D')
expect(fragmentVnode.children[4].children[0].type).to.equal('E')
expect(fragmentVnode.children[5].children[0].type).to.equal('F')
})
it('The containing element should be "main"', function() {
expect(fragmentVnode.type).to.equal('main')
})
it('frament child type should be "span"', function() {
expect(fragmentVnode.children[0].type).to.equal('span')
expect(fragmentVnode.children[1].type).to.equal('span')
expect(fragmentVnode.children[2].type).to.equal('span')
expect(fragmentVnode.children[3].type).to.equal('span')
expect(fragmentVnode.children[4].type).to.equal('span')
expect(fragmentVnode.children[5].type).to.equal('span')
})
})
describe('Use Fragment tag', function() {
const stuff = ['Apples', 'Oranges', 'Bananas']
const fragTest = document.querySelector('#fragment-test')
function Items(stuff) {
const items = stuff.map(item => h('li', {}, [item]))
return Fragment(null, items)
}
render(h('ul', {}, [Items(stuff)]), '#fragment-test')
it('list should have three items', function() {
expect(fragTest.firstElementChild.children.length).to.equal(3)
})
it('list items should be of type "LI"', function() {
expect(fragTest.firstElementChild.children[0].nodeName).to.equal('LI')
expect(fragTest.firstElementChild.children[1].nodeName).to.equal('LI')
expect(fragTest.firstElementChild.children[2].nodeName).to.equal('LI')
})
it('list items parent should be of type "UL"', function() {
expect(fragTest.firstElementChild.nodeName).to.equal('UL')
})
})
describe('Nested Fragment tags should flatten out', function() {
const letters = ['a', 'b', 'c', 'd', 'e']
const fragTest2 = document.querySelector('#fragment-test2')
const items = Fragment(
null, [
h('span', {}, [letters[0]]),
h('span', {}, [letters[1]]),
Fragment(
null, [
h('span', {}, [letters[2]]),
h('span', {}, [letters[3]]),
Fragment(
null, [
h('span', {}, [letters[4]])
]
)
]
)
]
)
const vNodeTest = h('div', {}, items)
window.vNodeTest = vNodeTest
render(vNodeTest, '#fragment-test2')
it('vNode should have only five children', function() {
expect(vNodeTest.children.length).to.equal(5)
})
it('vNode type should be "div"', function() {
expect(vNodeTest.type).to.equal('div')
})
it('vNode children type should be "span"', function() {
expect(vNodeTest.children[0].type).to.equal('span')
expect(vNodeTest.children[1].type).to.equal('span')
expect(vNodeTest.children[2].type).to.equal('span')
expect(vNodeTest.children[3].type).to.equal('span')
expect(vNodeTest.children[4].type).to.equal('span')
})
it('vNode children should have value of "a, b, c, d, e"', function() {
expect(vNodeTest.children[0].children[0].type).to.equal('a')
expect(vNodeTest.children[1].children[0].type).to.equal('b')
expect(vNodeTest.children[2].children[0].type).to.equal('c')
expect(vNodeTest.children[3].children[0].type).to.equal('d')
expect(vNodeTest.children[4].children[0].type).to.equal('e')
})
it('should flatten nested fragments into five siblings in DOM', function() {
/*
Use nested fragments to test if they flatten during render:
const items = Fragment(
null, [
h('span', {}, [letters[0]]),
h('span', {}, [letters[1]]),
Fragment(
null, [
h('span', {}, [letters[2]]),
h('span', {}, [letters[3]]),
Fragment(
null, [
h('span', {}, [letters[4]])
]
)
]
)
]
)
*/
expect(fragTest2.firstElementChild.children.length).to.equal(5)
})
it('Fragment children in DOM should be of type "SPAN"', function() {
expect(fragTest2.firstElementChild.children[0].nodeName).to.equal('SPAN')
expect(fragTest2.firstElementChild.children[1].nodeName).to.equal('SPAN')
expect(fragTest2.firstElementChild.children[2].nodeName).to.equal('SPAN')
expect(fragTest2.firstElementChild.children[3].nodeName).to.equal('SPAN')
expect(fragTest2.firstElementChild.children[4].nodeName).to.equal('SPAN')
})
it('Fragment parent in DOM should be of type "DIV"', function() {
expect(fragTest2.firstElementChild.nodeName).to.equal('DIV')
})
})
describe('Cannot insert Fragment tag directly into DOM', function() {
// #fragment-test3
function ListItems() {
return (
Fragment(null, [
h('li', {}, 'One'),
h('li', {}, 'Two'),
h('li', {}, 'Three')
])
)
}
try {
render(ListItems(), document.querySelector('#fragment-test3'))
} catch(err) {
renderError = err
}
it('Render should generate an error message when attempting to insert Fragment tag directly into DOM', function() {
expect(typeof renderError).to.equal('object')
expect(renderError.message).to.equal('Cannot insert Fragment tag directly into DOM.')
})
it('UL tag in DOM should remain empty', function() {
const ul = document.querySelector('#fragment-test3')
expect(ul.children.length).to.equal(0)
})
})
mocha.run()
</script>
</body>
</html>