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>Mount and Render</title>
|
8 | <link href="https://unpkg.com/mocha@4.0.1/mocha.css" rel="stylesheet" />
|
9 | <link rel="stylesheet" href="/test/styles.css">
|
10 | </head>
|
11 | <body>
|
12 | <nav onclick="location.pathname='/test/index.html'">
|
13 | <h1>Mocha Tests - Composi mount & render Function</h1>
|
14 | </nav>
|
15 | <section>
|
16 | <div id="mocha"></div>
|
17 | <div id="messages"></div>
|
18 | <div id="fixtures"></div>
|
19 | <div id="hide-tests">
|
20 | <div id='h1-test'></div>
|
21 | <div id="list-test"></div>
|
22 | <div id="alert-test"></div>
|
23 | <div id="render-test"></div>
|
24 | <div id="lifecycle"></div>
|
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} = composi
|
34 | let expect = chai.expect
|
35 |
|
36 | function H1(){
|
37 | return h('h1', {id: 'title'}, 'The Title')
|
38 | }
|
39 |
|
40 | mount(H1(), '#h1-test')
|
41 |
|
42 | function List() {
|
43 | return h('ul', {class: 'list'},[
|
44 | h('li', {}, 'one'),
|
45 | h('li', {}, 'two'),
|
46 | h('li', {}, 'three')
|
47 | ])
|
48 | }
|
49 | mount(List(), '#list-test')
|
50 |
|
51 | function Alert(message) {
|
52 | return h('h2', {style: {color: 'red'}} , `Alert: ${message}!`)
|
53 | }
|
54 | mount(Alert('this is a message'), '#alert-test')
|
55 |
|
56 | const things = [
|
57 | {name: 'Apple', key: 101},
|
58 | {name: 'Chair', key: 102},
|
59 | {name: 'Horse', key: 103}
|
60 | ]
|
61 | function List2(data) {
|
62 | return h(
|
63 | 'ul',
|
64 | {},
|
65 | data.map(item => h('li', {key: item.key}, item.name))
|
66 | )
|
67 | }
|
68 | let vNode = List2(things)
|
69 | let list2 = mount(List2(things), '#render-test')
|
70 |
|
71 | setTimeout(() => {
|
72 | things.push({name: 'Popcorn', key: 104})
|
73 | vNode = List2(things)
|
74 | render(List2(things), list2)
|
75 | }, 3000)
|
76 |
|
77 | describe('Create virtual node for list', function() {
|
78 | it('virtual node should have three children', function() {
|
79 | expect(vNode.children.length).to.equal(3)
|
80 | })
|
81 | it('virtual node children should equal "Apple", "Chair" and "Horse"', function() {
|
82 | expect(vNode.children[0].children[0]).to.equal('Apple')
|
83 | expect(vNode.children[1].children[0]).to.equal('Chair')
|
84 | expect(vNode.children[2].children[0]).to.equal('Horse')
|
85 | })
|
86 | it('virtual node children should have following keys: "101", "102", "103"', function() {
|
87 | expect(vNode.children[0].props.key).to.equal(101)
|
88 | expect(vNode.children[1].props.key).to.equal(102)
|
89 | expect(vNode.children[2].props.key).to.equal(103)
|
90 | })
|
91 | })
|
92 |
|
93 | describe("Mount h1", function() {
|
94 | const title = document.querySelector('#title')
|
95 | it('title should be "h1"', function() {
|
96 | expect(title.nodeName).to.equal('H1')
|
97 | });
|
98 | it('title should have id equal to "title"', function() {
|
99 | expect(title.id).to.equal('title')
|
100 | })
|
101 | it('title content should be "The Title"', function() {
|
102 | expect(title.textContent).to.equal('The Title')
|
103 | })
|
104 | })
|
105 | describe('Mount list', function() {
|
106 | const list = document.querySelector('ul')
|
107 | it('list should be of type "UL"', function() {
|
108 | expect(list.nodeName).to.equal('UL')
|
109 | })
|
110 | it('list should have class of "list"', function() {
|
111 | expect(list.className).to.equal('list')
|
112 | })
|
113 | it('list should have three children', function() {
|
114 | expect(list.childNodes).to.have.lengthOf(3)
|
115 | })
|
116 | it('list item one should be type "LI"', function() {
|
117 | expect(list.childNodes[0].nodeName).to.equal('LI')
|
118 | })
|
119 | it('list 1 item content should be "one"', function() {
|
120 | expect(list.childNodes[0].textContent).to.equal('one')
|
121 | })
|
122 | it('list 2 item content should be "two"', function() {
|
123 | expect(list.childNodes[1].textContent).to.equal('two')
|
124 | })
|
125 | it('list 3 item content should be "three"', function() {
|
126 | expect(list.childNodes[2].textContent).to.equal('three')
|
127 | })
|
128 | })
|
129 | describe('Mount alert', function() {
|
130 | const alert = document.querySelector('h2')
|
131 | it('alert should be of type "H2"', function() {
|
132 | expect(alert.nodeName).to.equal('H2')
|
133 | })
|
134 | it('alert content should be "Alert: this is a message!"', function() {
|
135 | expect(alert.textContent).to.equal("Alert: this is a message!")
|
136 | })
|
137 | it('alert should have style of "color: red"', function() {
|
138 | expect(alert.style.color).to.equal('red')
|
139 | })
|
140 | })
|
141 | describe('Mount list to update later', function() {
|
142 | const list = document.querySelector('#render-test ul')
|
143 |
|
144 | it('should have just three children', function() {
|
145 | expect(list.children.length).to.equal(3)
|
146 | })
|
147 | it('list items should contain "Apple", "Chair" and "Horse"', function() {
|
148 | expect(list.children[0].textContent).to.equal('Apple')
|
149 | expect(list.children[1].textContent).to.equal('Chair')
|
150 | expect(list.children[2].textContent).to.equal('Horse')
|
151 | })
|
152 |
|
153 | })
|
154 | describe('Update list with render function', function() {
|
155 | it('list virtual node should have four children', function(done) {
|
156 | setTimeout(() => {
|
157 | expect(vNode.children.length).to.equal(4)
|
158 | }, 3000)
|
159 | done()
|
160 | })
|
161 | it('list virtual node now have four items"', function(done) {
|
162 | setTimeout(() => {
|
163 | expect(vNode.children.length).to.equal(4)
|
164 | }, 3000)
|
165 | done()
|
166 | })
|
167 | const list = document.querySelector('#render-test ul')
|
168 | it('virtual node children should be "Apple", "Chair", "Horse" and "Popcorn', function(done) {
|
169 | setTimeout(() => {
|
170 | expect(vNode.children[0].children[0]).to.equal('Apple')
|
171 | expect(vNode.children[1].children[0]).to.equal('Chair')
|
172 | expect(vNode.children[2].children[0]).to.equal('Horse')
|
173 | expect(vNode.children[3].children[0]).to.equal('Popcorn')
|
174 | }, 3000)
|
175 | done()
|
176 | })
|
177 | it('virtual node children keys should be "101", "102", "103", "104"', function(done) {
|
178 | setTimeout(() => {
|
179 | expect(vNode.children[0].props.key).to.equal(101)
|
180 | expect(vNode.children[1].props.key).to.equal(102)
|
181 | expect(vNode.children[2].props.key).to.equal(103)
|
182 | expect(vNode.children[3].props.key).to.equal(104)
|
183 | }, 3000)
|
184 | done()
|
185 | })
|
186 | it('DOM list should have four list items', function(done) {
|
187 | setTimeout(() => {
|
188 | expect(list.children.length).to.equal(4)
|
189 | }, 3000)
|
190 | done()
|
191 | })
|
192 | it('DOM list items should contain "Apple", "Chair", "Horse" and "Popcorn"', function(done) {
|
193 | setTimeout(() => {
|
194 | expect(list.children[0].textContent).to.equal('Apple')
|
195 | expect(list.children[1].textContent).to.equal('Chair')
|
196 | expect(list.children[2].textContent).to.equal('Horse')
|
197 | expect(list.children[3].textContent).to.equal('Popcorn')
|
198 | }, 3000)
|
199 | done()
|
200 | })
|
201 | })
|
202 |
|
203 | describe('Component should execute thee lifecycle hooks', function() {
|
204 | let componentDidMount = false
|
205 | let componentDidUpdate = false
|
206 | let componentDidUnmount = false
|
207 | let unmountedComponents = 0
|
208 | function LifecycleTest(data) {
|
209 | function testMount() {
|
210 | componentDidMount = true
|
211 | }
|
212 | function testUpdate() {
|
213 | componentDidUpdate = true
|
214 | unmountedComponents += 1
|
215 | }
|
216 | function testUnmount() {
|
217 | componentDidUnmount = true
|
218 | }
|
219 | function listItems(items) {
|
220 | console.log(items)
|
221 | return items.map(item => h('li', {onComponentDidUnmount: () => testUnmount()}, item))
|
222 | }
|
223 | return (
|
224 | h(
|
225 | 'ul',
|
226 | {
|
227 | onComponentDidMount: () => testMount(),
|
228 | onComponentDidUpdate: () => testUpdate()
|
229 | },
|
230 | listItems(data)
|
231 | )
|
232 | )
|
233 | }
|
234 | let testComponent = mount(LifecycleTest([1,2]), '#lifecycle')
|
235 |
|
236 | it('Functional component should execute "onComponentDidMount" when it is mounted', function(done) {
|
237 | setTimeout(() => {
|
238 | expect(componentDidMount).to.be.true
|
239 | expect(testComponent.children.length).to.equal(2)
|
240 | }, 1000)
|
241 | done()
|
242 | })
|
243 | it('Functional component should execute "onComponentDidUpdate" when it is updated', function(done) {
|
244 | setTimeout(() => {
|
245 | render(LifecycleTest([1,2,3]), testComponent)
|
246 | expect(componentDidUpdate).to.be.true
|
247 | expect(testComponent.children.length).to.equal(3)
|
248 | }, 1500)
|
249 | done()
|
250 | })
|
251 | it('List component\'s list items should execute "onComponentDidUnmount" when they are deleted', function(done) {
|
252 | setTimeout(() => {
|
253 | render(LifecycleTest([1]), testComponent)
|
254 | expect(componentDidUnmount).to.be.true
|
255 | expect(testComponent.children.length).to.equal(1)
|
256 | expect(unmountedComponents).to.equal(2)
|
257 | }, 2000)
|
258 | done()
|
259 | })
|
260 | })
|
261 |
|
262 | mocha.run()
|
263 | </script>
|
264 | </body>
|
265 | </html> |
\ | No newline at end of file |