UNPKG

7.53 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3
4<head>
5 <meta charset="UTF-8">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <meta http-equiv="X-UA-Compatible" content="ie=edge">
8 <title>Document</title>
9 <link href="https://unpkg.com/mocha@4.0.1/mocha.css" rel="stylesheet" />
10 <link rel="stylesheet" href="/test/styles.css">
11 <link rel="icon" type="image/png" href="/resources/favicon-16x16.png" sizes="16x16" />
12 <link rel="icon" type="image/png" href="/resources/favicon-32x32.png" sizes="32x32" />
13</head>
14
15<body>
16 <nav onclick="location.pathname='/test/index.html'">
17 <h1>Mocha Tests - Composi DataStore</h1>
18 </nav>
19 <section>
20 <div id="mocha"></div>
21 <div id="messages"></div>
22 <div id="fixtures"></div>
23 <div id="hide-tests">
24 <div id="title-component"></div>
25 </div>
26 </section>
27
28 <script src='/dist/composi.js'></script>
29 <script src="https://unpkg.com/mocha@4.0.1/mocha.js"></script>
30 <script src="https://unpkg.com/chai@4.1.2/chai.js"></script>
31 <script>mocha.setup('bdd')</script>
32 <script>
33
34 const { h, DataStore, DataStoreComponent, Observer, uuid } = composi
35 const should = chai.should()
36 const expect = chai.expect
37
38 describe('uuid should create an RFC4122 version 4 compliant uuid', function() {
39 const id = uuid()
40 it('Uuid should have 36 characters', function() {
41 expect(typeof id).to.equal('string')
42 expect(id.length).to.equal(36)
43 })
44 })
45
46 describe('Create DataStore Instance', function () {
47 const dataStore1 = new DataStore({
48 state: {
49 title: 'This is the Title!'
50 }
51 })
52 it('New DataStore', function () {
53 expect(typeof dataStore1).to.equal('object')
54 expect(typeof dataStore1.state).to.equal('object')
55 expect(dataStore1.state.title).to.equal('This is the Title!')
56 })
57 })
58
59 describe('Using setState on dataStore should change its state', function() {
60 const dataStore2 = new DataStore({
61 state: {
62 items: [1,2,3]
63 }
64 })
65 it('New dataStore state should have three items', function() {
66 expect(dataStore2.state.items.length).to.equal(3)
67 expect(dataStore2.state.items[0]).to.equal(1)
68 expect(dataStore2.state.items[1]).to.equal(2)
69 expect(dataStore2.state.items[2]).to.equal(3)
70 })
71 it('Use setState to add item to dataStore', function(done) {
72 setTimeout(() => {
73 dataStore2.setState(prevState => {
74 prevState.items.push(4)
75 return prevState
76 })
77 expect(dataStore2.state.items.length).to.equal(4)
78 expect(dataStore2.state.items[3]).to.equal(4)
79 }, 1000)
80 done()
81 })
82 })
83
84
85 describe('dataStore.watch should react to dataStore.setState', function() {
86 let dataStoreData = []
87
88 const dataStore3 = new DataStore({
89 state: {
90 items: [
91 {
92 key: 101,
93 value: 'Apples'
94 },
95 {
96 key: 102,
97 value: 'Oranges'
98 }
99 ]
100 }
101 })
102 dataStore3.watch('dataStoreStateChanged', data => {
103 dataStoreData = data
104 })
105
106 it('The variable dataStoreData should be an empty array', function() {
107 expect(dataStoreData.length).to.equal(0)
108 })
109 it('Initial dataStore items should be two', function() {
110 expect(dataStore3.state.items.length).to.equal(2)
111 expect(dataStore3.state.items[0].value).to.equal('Apples')
112 expect(dataStore3.state.items[1].value).to.equal('Oranges')
113 })
114 it('Update dataStore using setState should dispatch dataStoreStateChanged event', function(done) {
115 setTimeout(() => {
116 dataStore3.setState(prevState => {
117 prevState.items.push({
118 key: 103,
119 value: 'Bananas'
120 })
121 return prevState
122 })
123 expect(dataStore3.state.items.length).to.equal(3)
124 expect(dataStore3.state.items[2].value).to.equal('Bananas')
125 }, 1000)
126 done()
127 })
128 it('dataStore should have dispatched dataStoreStateChanged and data, resulting in dataStoreData now holding copy of dataStore state', function(done) {
129 setTimeout(() => {
130 expect(dataStoreData.items.length).to.equal(3)
131 expect(dataStoreData.items[0].value).to.equal('Apples')
132 expect(dataStoreData.items[1].value).to.equal('Oranges')
133 expect(dataStoreData.items[2].value).to.equal('Bananas')
134 }, 1500)
135 done()
136 })
137 })
138
139
140 const dataStore4 = new DataStore({
141 state: {
142 title: 'A New Title'
143 }
144 })
145 class Title extends DataStoreComponent {
146 render(data) {
147 return (
148 h(
149 'h1',
150 null,
151 data.title
152 )
153 )
154 }
155 }
156 const title = new Title({
157 container: '#title-component',
158 dataStore: dataStore4
159 })
160 window.dataStore4 = dataStore4
161 window.title = title
162
163 const titleTest = document.querySelector('#title-component')
164
165 describe('dataStore Component should not mount when first initialized with dataStore', function() {
166 it('Component should not yet be mounted', function() {
167 expect(titleTest.children.length).to.equal(0)
168 })
169 })
170
171 describe('Component should mount after passing dataStore.state to its update method', function() {
172 it('component should mount when dataStore is passed to its update method', function(done) {
173 setTimeout(() => {
174 title.update(dataStore4.state)
175 expect(titleTest.children.length).to.equal(1)
176 expect(titleTest.children[0].textContent).to.equal('A New Title')
177 }, 1000)
178 done()
179 })
180 })
181
182 describe('Update state of dataStore used by component', function() {
183 it('Changing state of dataStore with setState should change value of rendered component', function(done) {
184 setTimeout(() => {
185 dataStore4.setState(prevState => {
186 prevState.title = 'This is a brand new title!'
187 return prevState
188 })
189 expect(titleTest.children.length).to.equal(1)
190 expect(titleTest.children[0].textContent).to.equal('This is a brand new title!')
191 }, 2000)
192 done()
193 })
194 })
195
196 let observerDataTest = ''
197 const observer = new Observer()
198 describe('Create a new Observer', function() {
199 it('New observer should have empty event property', function() {
200 expect(Object.keys(observer.events).length).to.equal(0)
201 expect(observerDataTest).to.not.be.ok
202 })
203 })
204
205 describe('Observer with watcher should react to event being dispatched', function() {
206 it('Dispatching "event-test" should trigger watcher for that event', function(done) {
207 setTimeout(() => {
208 observer.watch('event-test', data => {
209 observerDataTest = data
210 })
211 observer.dispatch('event-test', 'This is the dispatched data')
212 })
213 done()
214 })
215 it('After dispatching event, the dispatchd data should have been assigned to test variable', function() {
216 it('The observer watcher should have updated the value of observerDataTest', function(done) {
217 setTimeout(() => {
218 expect(observerDataTest).to.equal('This is the dispatched data')
219 })
220 done()
221 }, 1000)
222 })
223 })
224
225 mocha.run()
226 </script>
227</body>
228
229</html>
\No newline at end of file