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>
|
25 | </section>
|
26 |
|
27 | <script src="https://unpkg.com/mocha@4.0.1/mocha.js"></script>
|
28 | <script src="https://unpkg.com/chai@4.1.2/chai.js"></script>
|
29 | <script src='/dist/composi.js'></script>
|
30 | <script>mocha.setup('bdd')</script>
|
31 | <script>
|
32 | const {h, mount, render} = composi
|
33 | let expect = chai.expect
|
34 |
|
35 | function H1(){
|
36 | return h('h1', {id: 'title'}, 'The Title')
|
37 | }
|
38 |
|
39 | mount(H1(), '#h1-test')
|
40 |
|
41 | function List() {
|
42 | return h('ul', {class: 'list'},[
|
43 | h('li', {}, 'one'),
|
44 | h('li', {}, 'two'),
|
45 | h('li', {}, 'three')
|
46 | ])
|
47 | }
|
48 | mount(List(), '#list-test')
|
49 |
|
50 | function Alert(message) {
|
51 | return h('h2', {style: {color: 'red'}} , `Alert: ${message}!`)
|
52 | }
|
53 | mount(Alert('this is a message'), '#alert-test')
|
54 |
|
55 | const things = [
|
56 | {name: 'Apple', key: 101},
|
57 | {name: 'Chair', key: 102},
|
58 | {name: 'Horse', key: 103}
|
59 | ]
|
60 | function List2(data) {
|
61 | return h(
|
62 | 'ul',
|
63 | {},
|
64 | data.map(item => h('li', {key: item.key}, item.name))
|
65 | )
|
66 | }
|
67 | let vNode = List2(things)
|
68 | let list2 = mount(List2(things), '#render-test')
|
69 |
|
70 | setTimeout(() => {
|
71 | things.push({name: 'Popcorn', key: 104})
|
72 | vNode = List2(things)
|
73 | render(List2(things), list2)
|
74 | }, 3000)
|
75 |
|
76 | describe('Create virtual node for list', function() {
|
77 | it('virtual node should have three children', function() {
|
78 | expect(vNode.children.length).to.equal(3)
|
79 | })
|
80 | it('virtual node children should equal "Apple", "Chair" and "Horse"', function() {
|
81 | expect(vNode.children[0].children[0]).to.equal('Apple')
|
82 | expect(vNode.children[1].children[0]).to.equal('Chair')
|
83 | expect(vNode.children[2].children[0]).to.equal('Horse')
|
84 | })
|
85 | it('virtual node children should have following keys: "101", "102", "103"', function() {
|
86 | expect(vNode.children[0].props.key).to.equal(101)
|
87 | expect(vNode.children[1].props.key).to.equal(102)
|
88 | expect(vNode.children[2].props.key).to.equal(103)
|
89 | })
|
90 | })
|
91 |
|
92 | describe("Mount h1", function() {
|
93 | const title = document.querySelector('#title')
|
94 | it('title should be "h1"', function() {
|
95 | expect(title.nodeName).to.equal('H1')
|
96 | });
|
97 | it('title should have id equal to "title"', function() {
|
98 | expect(title.id).to.equal('title')
|
99 | })
|
100 | it('title content should be "The Title"', function() {
|
101 | expect(title.textContent).to.equal('The Title')
|
102 | })
|
103 | })
|
104 | describe('Mount list', function() {
|
105 | const list = document.querySelector('ul')
|
106 | it('list should be of type "UL"', function() {
|
107 | expect(list.nodeName).to.equal('UL')
|
108 | })
|
109 | it('list should have class of "list"', function() {
|
110 | expect(list.className).to.equal('list')
|
111 | })
|
112 | it('list should have three children', function() {
|
113 | expect(list.childNodes).to.have.lengthOf(3)
|
114 | })
|
115 | it('list item one should be type "LI"', function() {
|
116 | expect(list.childNodes[0].nodeName).to.equal('LI')
|
117 | })
|
118 | it('list 1 item content should be "one"', function() {
|
119 | expect(list.childNodes[0].textContent).to.equal('one')
|
120 | })
|
121 | it('list 2 item content should be "two"', function() {
|
122 | expect(list.childNodes[1].textContent).to.equal('two')
|
123 | })
|
124 | it('list 3 item content should be "three"', function() {
|
125 | expect(list.childNodes[2].textContent).to.equal('three')
|
126 | })
|
127 | })
|
128 | describe('Mount alert', function() {
|
129 | const alert = document.querySelector('h2')
|
130 | it('alert should be of type "H2"', function() {
|
131 | expect(alert.nodeName).to.equal('H2')
|
132 | })
|
133 | it('alert content should be "Alert: this is a message!"', function() {
|
134 | expect(alert.textContent).to.equal("Alert: this is a message!")
|
135 | })
|
136 | it('alert should have style of "color: red"', function() {
|
137 | expect(alert.style.color).to.equal('red')
|
138 | })
|
139 | })
|
140 | describe('Mount list to update with render function', function() {
|
141 | const list = document.querySelector('#render-test ul')
|
142 |
|
143 | it('should have just three children', function() {
|
144 | expect(list.children.length).to.equal(3)
|
145 | })
|
146 | it('list items should contain "Apple", "Chair" and "Horse"', function() {
|
147 | expect(list.children[0].textContent).to.equal('Apple')
|
148 | expect(list.children[1].textContent).to.equal('Chair')
|
149 | expect(list.children[2].textContent).to.equal('Horse')
|
150 | })
|
151 |
|
152 | })
|
153 | describe('Update list with render function', function(done) {
|
154 | it('list virtual node should have four children', function(done) {
|
155 | setTimeout(() => {
|
156 | expect(vNode.children.length).to.equal(4)
|
157 | }, 3000)
|
158 | done()
|
159 | })
|
160 | it('list virtual node now have four items"', function(done) {
|
161 | setTimeout(() => {
|
162 | expect(vNode.children.length).to.equal(4)
|
163 | }, 3000)
|
164 | done()
|
165 | })
|
166 | const list = document.querySelector('#render-test ul')
|
167 | it('virtual node children should be "Apple", "Chair", "Horse" and "Popcorn', function(done) {
|
168 | setTimeout(() => {
|
169 | expect(vNode.children[0].children[0]).to.equal('Apple')
|
170 | expect(vNode.children[1].children[0]).to.equal('Chair')
|
171 | expect(vNode.children[2].children[0]).to.equal('Horse')
|
172 | expect(vNode.children[3].children[0]).to.equal('Popcorn')
|
173 | }, 3000)
|
174 | done()
|
175 | })
|
176 | it('virtual node children keys should be "101", "102", "103", "104"', function(done) {
|
177 | setTimeout(() => {
|
178 | expect(vNode.children[0].props.key).to.equal(101)
|
179 | expect(vNode.children[1].props.key).to.equal(102)
|
180 | expect(vNode.children[2].props.key).to.equal(103)
|
181 | expect(vNode.children[3].props.key).to.equal(104)
|
182 | }, 3000)
|
183 | done()
|
184 | })
|
185 | it('DOM list should have four list items', function(done) {
|
186 | setTimeout(() => {
|
187 | expect(list.children.length).to.equal(4)
|
188 | }, 3000)
|
189 | done()
|
190 | })
|
191 | it('DOM list items should contain "Apple", "Chair", "Horse" and "Popcorn"', function(done) {
|
192 | setTimeout(() => {
|
193 | expect(list.children[0].textContent).to.equal('Apple')
|
194 | expect(list.children[1].textContent).to.equal('Chair')
|
195 | expect(list.children[2].textContent).to.equal('Horse')
|
196 | expect(list.children[3].textContent).to.equal('Popcorn')
|
197 | }, 3000)
|
198 | done()
|
199 | })
|
200 |
|
201 | })
|
202 |
|
203 | mocha.run()
|
204 | </script>
|
205 | </body>
|
206 | </html> |
\ | No newline at end of file |