This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathdom.test.js
441 lines (358 loc) · 12.3 KB
/
dom.test.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/** @jsx etch.dom */
const etch = require('../../lib/index')
describe('etch.dom', () => {
it('defaults properties to an empty object', () => {
let props = null
class MyComponent {
constructor (p) {
props = p
}
render () { return <span /> }
update () {}
}
let owner = {
render () {
return <MyComponent />
},
update () {}
}
etch.initialize(owner)
expect(props).to.eql({})
})
it('normalizes camel-cased property names to dash-seperated attributes for SVG tags', function () {
let component = {
render () {
return <circle colorProfile='foo' colorRendering='bar' />
},
update () {}
}
etch.initialize(component)
expect(component.element.outerHTML).to.equal('<circle color-profile="foo" color-rendering="bar"></circle>')
})
it('supports assigning innerHTML to SVG tags', function () {
let component = {
render () {
return <svg innerHTML="<circle></circle>"></svg>
},
update () {}
}
etch.initialize(component)
expect(component.element.outerHTML).to.equal('<svg><circle></circle></svg>')
})
it('ignores nulls passed in the place of children, but throws an error if other invalid values are passed', () => {
const element = etch.render(
<div><span/>{null}<p/></div>
);
expect(Array.from(element.children).map(c => c.tagName)).to.eql(['SPAN', 'P'])
expect(() => etch.render(
<div>{false}</div>
)).to.throw('Invalid child node: false')
expect(() => etch.render(
<div>{undefined}</div>
)).to.throw('Invalid child node: undefined')
expect(() => etch.render(
<div>{() => {}}</div>
)).to.throw('Invalid child node: () => {}')
})
describe('when a component constructor is used as a tag name', () => {
describe('on initial render', () => {
it('constructs the component with the specified properties and children, then appends its element to the DOM', () => {
class ChildComponent {
constructor (properties, children) {
this.properties = properties
this.children = children
etch.initialize(this)
}
render () {
return <div>{this.properties.greeting} {this.children}</div>
}
update () {}
}
let parentComponent = {
render () {
return (
<div>
<ChildComponent greeting='Hello'>
<span>World</span>
</ChildComponent>
</div>
)
},
update () {}
}
etch.initialize(parentComponent)
expect(parentComponent.element.textContent).to.equal('Hello World')
})
})
describe('on update', () => {
describe('if the child component class is the same', () => {
describe('if the child component defines an update() method', () => {
it('invokes the update method with the new properties and children', async () => {
class ChildComponent {
constructor (properties, children) {
this.properties = properties
this.children = children
etch.initialize(this)
}
render () {
return <div>{this.properties.greeting} {this.children}</div>
}
update (properties, children) {
this.properties = properties
this.children = children
etch.update(this)
}
}
let parentComponent = {
greeting: 'Hello',
greeted: 'World',
render () {
return (
<div>
<ChildComponent greeting={this.greeting}>
<span>{this.greeted}</span>
</ChildComponent>
</div>
)
},
update () {}
}
etch.initialize(parentComponent)
expect(parentComponent.element.textContent).to.equal('Hello World')
let initialChildElement = parentComponent.element.firstChild
parentComponent.greeting = 'Goodnight'
parentComponent.greeted = 'Moon'
await etch.update(parentComponent)
expect(parentComponent.element.textContent).to.equal('Goodnight Moon')
expect(parentComponent.element.firstChild).to.equal(initialChildElement)
})
})
describe('if the child component does not define an update method', () => {
it('throws an error', async () => {
let component = {
render () {
return (
<div />
)
}
}
expect(() => etch.initialize(component)).to.throw(Error)
})
})
})
describe('if the component class changes', () => {
it('builds a new component instance and replaces the previous element with its element', async () => {
class ChildComponentA {
constructor () {
etch.initialize(this)
}
render () {
return <div>A</div>
}
update () {}
}
class ChildComponentB {
constructor () {
etch.initialize(this)
}
render () {
return <div>B</div>
}
update () {}
}
let parentComponent = {
condition: true,
render () {
if (this.condition) {
return <div><ChildComponentA /></div>
} else {
return <div><ChildComponentB /></div>
}
},
update () {}
}
etch.initialize(parentComponent)
expect(parentComponent.element.textContent).to.equal('A')
let initialChildElement = parentComponent.element.firstChild
parentComponent.condition = false
await etch.update(parentComponent)
expect(parentComponent.element.textContent).to.equal('B')
expect(parentComponent.element.firstChild).not.to.equal(initialChildElement)
})
})
describe('if components are reordered', () => {
it('builds a new component instance and replaces the previous element with its element', async () => {
class ChildComponentA {
constructor () {
this.updateCalled = false
etch.initialize(this)
}
render () {
return <div>A</div>
}
update () {
this.updateCalled = true
}
}
class ChildComponentB {
constructor () {
this.updateCalled = false
etch.initialize(this)
}
render () {
return <div>B</div>
}
update () {
this.updateCalled = true
}
}
let parentComponent = {
condition: true,
render () {
if (this.condition) {
return (
<div>
<ChildComponentA key='a' ref='a' />
<ChildComponentB key='b' ref='b' />
</div>
)
} else {
return (
<div>
<ChildComponentB key='b' ref='b' />
<ChildComponentA key='a' ref='a' />
</div>
)
}
},
update () {}
}
etch.initialize(parentComponent)
let element = parentComponent.element
let childComponentA = parentComponent.refs.a
let childComponentB = parentComponent.refs.b
let childElementA = element.children[0]
let childElementB = element.children[1]
expect(childComponentA.updateCalled).to.be.false
expect(childComponentB.updateCalled).to.be.false
parentComponent.condition = false
await etch.update(parentComponent)
expect(element.children[0]).to.equal(childElementB)
expect(element.children[1]).to.equal(childElementA)
expect(parentComponent.refs.a).to.equal(childComponentA)
expect(parentComponent.refs.a.element).to.equal(childElementA)
expect(parentComponent.refs.b).to.equal(childComponentB)
expect(parentComponent.refs.b.element).to.equal(childElementB)
expect(childComponentA.updateCalled).to.be.true
expect(childComponentB.updateCalled).to.be.true
})
})
})
describe('when the child component constructor tag has a ref property', () => {
it('creates a reference to the child component object on the parent component', async () => {
class ChildComponentA {
constructor (properties) {
this.properties = properties
etch.initialize(this)
}
render () {
return <div ref='self'>A</div>
}
update (properties) {
this.properties = properties
}
}
class ChildComponentB {
constructor (properties) {
this.properties = properties
etch.initialize(this)
}
render () {
return <div ref='self'>B</div>
}
update () {}
}
let parentComponent = {
renderA: true,
refName: 'child',
render () {
if (this.renderA) {
return <div><ChildComponentA ref={this.refName} /></div>
} else if (this.renderB) {
return <div><ChildComponentB ref={this.refName} /></div>
} else {
return <div />
}
},
update () {}
}
etch.initialize(parentComponent)
expect(parentComponent.refs.child instanceof ChildComponentA).to.be.true
expect(parentComponent.refs.child.properties.ref).to.equal('child')
expect(parentComponent.refs.child.refs.self.textContent).to.equal('A')
parentComponent.refName = 'kid'
await etch.update(parentComponent)
expect(parentComponent.refs.child).to.be.undefined
expect(parentComponent.refs.kid instanceof ChildComponentA).to.be.true
expect(parentComponent.refs.kid.properties.ref).to.equal('kid')
expect(parentComponent.refs.kid.refs.self.textContent).to.equal('A')
parentComponent.refName = 'child'
parentComponent.renderA = false
parentComponent.renderB = true
await etch.update(parentComponent)
expect(parentComponent.refs.kid).to.be.undefined
expect(parentComponent.refs.child instanceof ChildComponentB).to.be.true
expect(parentComponent.refs.child.properties.ref).to.equal('child')
expect(parentComponent.refs.child.refs.self.textContent).to.equal('B')
parentComponent.renderB = false
await etch.update(parentComponent)
expect('child' in parentComponent.refs).to.be.false
})
it('does not delete a reference to a different component when a component is destroyed', async function () {
class ChildComponentA {
constructor () {
etch.initialize(this)
}
render () {
return <div>A</div>
}
update (properties) {}
}
class ChildComponentB {
constructor () {
etch.initialize(this)
}
render () {
return <div>B</div>
}
update () {}
}
let parentComponent = {
condition: true,
render () {
if (this.condition) {
return (
<div>
<div />
<ChildComponentA ref='child' />
</div>
)
} else {
return (
<div>
<ChildComponentB ref='child' />
</div>
)
}
},
update () {}
}
etch.initialize(parentComponent)
parentComponent.condition = false
await etch.update(parentComponent)
expect(parentComponent.refs.child).to.not.be.undefined
expect(parentComponent.refs.child.constructor).to.equal(ChildComponentB)
})
})
})
})