UNPKG

7.72 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>Document</title>
8 <link href="https://unpkg.com/mocha@4.0.1/mocha.css" rel="stylesheet" />
9 <link rel="stylesheet" href="/test/styles.css">
10 <link rel="icon" type="image/png" href="/resources/favicon-16x16.png" sizes="16x16" />
11 <link rel="icon" type="image/png" href="/resources/favicon-32x32.png" sizes="32x32" />
12</head>
13<body>
14 <nav onclick="location.pathname='/test/index.html'">
15 <h1>Mocha Tests - Composi Fragment Tag</h1>
16 </nav>
17 <section>
18 <div id="mocha"></div>
19 <div id="messages"></div>
20 <div id="fixtures"></div>
21 <div id="hide-tests">
22 <div id="fragment-test"></div>
23 <div id="fragment-test2"></div>
24 <ul id="fragment-test3"></ul>
25 </div>
26 </section>
27
28 <script src="https://unpkg.com/mocha@4.0.1/mocha.js"></script>
29 <script src="https://unpkg.com/chai@4.1.2/chai.js"></script>
30 <script src='/dist/composi.js'></script>
31 <script>mocha.setup('bdd')</script>
32 <script>
33 const {h, mount, render, patch, Fragment} = composi
34 let expect = chai.expect
35
36
37
38 describe('Examine Fragment tag virtual node', function() {
39 const letters = ['A', 'B', 'C', 'D', 'E', 'F']
40 function NestFragments({letters}) {
41 return (
42 h(
43 'main',
44 {},
45 [
46 Fragment(
47 null,
48 [
49 h('span', {}, ['A']),
50 h('span', {}, ['B']),
51 Fragment(null, [
52 h('span', {}, ['C']),
53 h('span', {}, ['D']),
54 Fragment(null, [
55 h('span', {}, ['E']),
56 h('span', {}, ['F']),
57 ])
58 ])
59 ]
60 )
61 ]
62 )
63 )
64 }
65 const fragmentVnode = NestFragments({letters})
66 window.fragmentVnode = fragmentVnode
67 it('VNode should have six children', function() {
68 expect(fragmentVnode.children.length).to.equal(6)
69 })
70 it('Children should have value of "A", "B", "C", "D", "E", "F"', function() {
71 expect(fragmentVnode.children[0].children[0]).to.equal('A')
72 expect(fragmentVnode.children[1].children[0]).to.equal('B')
73 expect(fragmentVnode.children[2].children[0]).to.equal('C')
74 expect(fragmentVnode.children[3].children[0]).to.equal('D')
75 expect(fragmentVnode.children[4].children[0]).to.equal('E')
76 expect(fragmentVnode.children[5].children[0]).to.equal('F')
77 })
78 it('The containing element should be "main"', function() {
79 expect(fragmentVnode.type).to.equal('main')
80 })
81 it('frament child type should be "span"', function() {
82 expect(fragmentVnode.children[0].type).to.equal('span')
83 expect(fragmentVnode.children[1].type).to.equal('span')
84 expect(fragmentVnode.children[2].type).to.equal('span')
85 expect(fragmentVnode.children[3].type).to.equal('span')
86 expect(fragmentVnode.children[4].type).to.equal('span')
87 expect(fragmentVnode.children[5].type).to.equal('span')
88 })
89 })
90
91 describe('Use Fragment tag', function() {
92 const stuff = ['Apples', 'Oranges', 'Bananas']
93 const fragTest = document.querySelector('#fragment-test')
94 function Items(stuff) {
95 const items = stuff.map(item => h('li', {}, [item]))
96 return Fragment(null, items)
97 }
98 mount(h('ul', {}, [Items(stuff)]), '#fragment-test')
99 it('list should have three items', function() {
100 expect(fragTest.firstElementChild.children.length).to.equal(3)
101 })
102 it('list items should be of type "LI"', function() {
103 expect(fragTest.firstElementChild.children[0].nodeName).to.equal('LI')
104 expect(fragTest.firstElementChild.children[1].nodeName).to.equal('LI')
105 expect(fragTest.firstElementChild.children[2].nodeName).to.equal('LI')
106 })
107 it('list items parent should be of type "UL"', function() {
108 expect(fragTest.firstElementChild.nodeName).to.equal('UL')
109 })
110 })
111
112 describe('Nested Fragment tags should flatten out', function() {
113 const letters = ['a', 'b', 'c', 'd', 'e']
114 const fragTest2 = document.querySelector('#fragment-test2')
115 const items = Fragment(
116 null, [
117 h('span', {}, [letters[0]]),
118 h('span', {}, [letters[1]]),
119 Fragment(
120 null, [
121 h('span', {}, [letters[2]]),
122 h('span', {}, [letters[3]]),
123 Fragment(
124 null, [
125 h('span', {}, [letters[4]])
126 ]
127 )
128 ]
129 )
130 ]
131 )
132 const vNodeTest = h('div', {}, items)
133 window.vNodeTest = vNodeTest
134 mount(vNodeTest, '#fragment-test2')
135 it('vNode should have only five children', function() {
136 expect(vNodeTest.children.length).to.equal(5)
137 })
138 it('vNode type should be "div"', function() {
139 expect(vNodeTest.type).to.equal('div')
140 })
141 it('vNode children type should be "span"', function() {
142 expect(vNodeTest.children[0].type).to.equal('span')
143 expect(vNodeTest.children[1].type).to.equal('span')
144 expect(vNodeTest.children[2].type).to.equal('span')
145 expect(vNodeTest.children[3].type).to.equal('span')
146 expect(vNodeTest.children[4].type).to.equal('span')
147 })
148 it('vNode children should have value of "a, b, c, d, e"', function() {
149 expect(vNodeTest.children[0].children[0]).to.equal('a')
150 expect(vNodeTest.children[1].children[0]).to.equal('b')
151 expect(vNodeTest.children[2].children[0]).to.equal('c')
152 expect(vNodeTest.children[3].children[0]).to.equal('d')
153 expect(vNodeTest.children[4].children[0]).to.equal('e')
154 })
155 it('should flatten fragments into five siblings in DOM', function() {
156 expect(fragTest2.firstElementChild.children.length).to.equal(5)
157 })
158 it('Fragment children in DOM should be of type "SPAN"', function() {
159 expect(fragTest2.firstElementChild.children[0].nodeName).to.equal('SPAN')
160 expect(fragTest2.firstElementChild.children[1].nodeName).to.equal('SPAN')
161 expect(fragTest2.firstElementChild.children[2].nodeName).to.equal('SPAN')
162 expect(fragTest2.firstElementChild.children[3].nodeName).to.equal('SPAN')
163 expect(fragTest2.firstElementChild.children[4].nodeName).to.equal('SPAN')
164 })
165 it('Fragment parent in DOM should be of type "DIV"', function() {
166 expect(fragTest2.firstElementChild.nodeName).to.equal('DIV')
167 })
168 })
169 describe('Cannot insert Fragment tag directly into DOM', function() {
170 // #fragment-test3
171 let c
172 function ListItems() {
173 return (
174 Fragment(null, [
175 h('li', {}, 'One'),
176 h('li', {}, 'Two'),
177 h('li', {}, 'Three')
178 ])
179 )
180 }
181 try {
182 mount(ListItems(), document.querySelector('#fragment-test3'))
183 } catch(err) {
184 renderError = err
185 }
186 it('Render should generate an error message when attempting to insert Fragment tag directly into DOM', function() {
187 expect(typeof renderError).to.equal('object')
188 expect(renderError.message).to.equal('Cannot insert Fragment tag directly into DOM.')
189 })
190 it('UL tag in DOM should remain empty', function() {
191 const ul = document.querySelector('#fragment-test3')
192 expect(ul.children.length).to.equal(0)
193 })
194
195 })
196
197 mocha.run()
198 </script>
199</body>
200</html>
\No newline at end of file