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 pathinitialize.test.js
107 lines (87 loc) · 2.55 KB
/
initialize.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
/** @jsx etch.dom */
const etch = require('../../lib/index')
describe('etch.initialize(component)', () => {
it('returns an element with content based on the render method of the given component', () => {
let component = {
render () {
return <div>Hello World</div>
},
update () {}
}
etch.initialize(component)
expect(component.element.textContent).to.equal('Hello World')
})
it('creates references to DOM elements', () => {
let component = {
render () {
return <div><span ref='greeting'>Hello</span> <span ref='greeted'>World</span></div>
},
update () {}
}
etch.initialize(component)
expect(component.refs.greeting.textContent).to.equal('Hello')
expect(component.refs.greeted.textContent).to.equal('World')
})
it('updates references to DOM elements', async () => {
let componentIndexWithRef = 1
let component = {
render () {
let firstElementProperties = componentIndexWithRef === 0 ? {ref: 'selected'} : {}
let secondElementProperties = componentIndexWithRef === 1 ? {ref: 'selected'} : {}
return (
<ul>
<li {...firstElementProperties}>one</li>
<li {...secondElementProperties}>two</li>
</ul>
)
},
update () {}
}
etch.initialize(component)
expect(component.refs.selected.textContent).to.equal('two')
componentIndexWithRef = 0
await etch.update(component)
expect(component.refs.selected.textContent).to.equal('one')
})
it('nests references correctly', async () => {
class Component {
constructor(props, children) {
this.children = children
etch.initialize(this)
}
update() {}
render () {
return <div>{this.children}</div>
}
}
class TestHarness {
constructor() {
etch.initialize(this)
}
update() {}
render() {
return (
<Component ref="outer">
<Component ref="middle">
<div ref="inner" />
</Component>
</Component>
)
}
}
const harness = new TestHarness()
expect(harness.refs.outer).to.be.ok
expect(harness.refs.middle).to.be.ok
expect(harness.refs.inner).to.be.ok
expect(harness.refs.outer.refs.middle).to.be.undefined
})
it('throws an exception if undefined is returned from render', () => {
let component = {
render () {},
update () {}
}
expect(function () {
etch.initialize(component)
}).to.throw(/invalid falsy value/)
})
})