-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.jsx
241 lines (174 loc) · 6.3 KB
/
test.jsx
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
var assert = require('assert');
var h_ = require('snabbdom/h');
describe("loader", function() {
it("should transpile to the equivalent h call", function() {
var dom = <div>test</div>;
var dom2 = h_("div", {}, ["test"]);
assert.equal("div", dom.sel);
assert.equal("test", dom.children[0].text);
assert.deepEqual(dom, dom2);
});
it("should contain text", function() {
var dom = <div>test</div>;
assert.equal("div", dom.sel);
assert.equal("test", dom.children[0].text);
});
it("should bind text", function() {
var text = "Lorem";
var dom = <div>{text}</div>;
assert.equal("div", dom.sel);
assert.equal(text, dom.children[0].text);
});
it("should bind text expressions", function() {
var text = "Lorem";
var dom = <div>{text + " ipsum"}</div>;
assert.equal("div", dom.sel);
assert.equal("Lorem ipsum", dom.children[0].text);
});
it("should extract attrs", function() {
var dom = <div dir="ltr">test</div>;
assert.equal("div", dom.sel);
assert.equal("ltr", dom.data.attrs.dir);
});
it("should extract events", function() {
var doSomething = function() {};
var dom = <div on-click={doSomething}>test</div>;
assert.equal("div", dom.sel);
assert.equal(doSomething, dom.data.on.click);
});
it("should extract multiple events", function() {
var onClick = function() {};
var onMouseOver = function() {};
var dom = <div on-click={onClick} on-mouseover={onMouseOver}>test</div>;
assert.equal("div", dom.sel);
assert.equal(onClick, dom.data.on.click);
assert.equal(onMouseOver, dom.data.on.mouseover);
});
it("should extract class hash when using multiple -", function() {
var truthy = true;
var dom = <div class-bar={false} class-foo={truthy} class-goo={true}>Lorem ipsum</div>;
assert.equal(true, dom.data.class.foo);
assert.equal(false, dom.data.class.bar);
assert.equal(true, dom.data.class.goo);
});
it("should extract class hash when using _", function() {
var truthy = true;
var dom = <div class_={{
foo: truthy,
bar: false,
goo: true
}}>Lorem ipsum</div>;
assert.equal(true, dom.data.class.foo);
assert.equal(false, dom.data.class.bar);
assert.equal(true, dom.data.class.goo);
});
it("should extract style hash when using _", function() {
var fontWeight = "bold";
var dom = <span style_={{
border: '1px solid #bada55',
color: '#c0ffee',
fontWeight: fontWeight
}}>Lorem ipsum</span>;
assert.equal("span", dom.sel);
assert.equal("1px solid #bada55", dom.data.style.border);
assert.equal("bold", dom.data.style.fontWeight);
assert.equal("Lorem ipsum", dom.children[0].text);
});
it("should put style string as attribute", function() {
var dom = <span style="color: red">Lorem ipsum</span>;
assert.equal("color: red", dom.data.attrs.style);
});
it("should handle key_", function() {
var num = 11;
var dom = <span key_={num}>Foo</span>;
assert.equal(11, dom.data.key);
});
it("should manage children in children", function() {
var dom = <div>text 1
<div>first</div>
<ul>
<li>list item 1</li>
<li>list item 2</li>
</ul>
text2</div>;
assert.equal("div", dom.sel);
var children = dom.children;
assert.equal("text 1", children[0].text);
assert.equal("div", children[1].sel);
assert.equal("ul", children[2].sel);
var grandChildren = children[2].children;
assert.equal("li", grandChildren[0].sel);
assert.equal("list item 1", grandChildren[0].children[0].text);
assert.equal("list item 2", grandChildren[1].children[0].text);
assert.equal("text2", children[3].text);
});
it("should manage svg", function() {
var dom = <svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">
<rect height="100" style="stroke:#ff0000; fill: #0000ff" width="100" x="10" y="10"/>
</svg>;
assert.equal("svg", dom.sel);
assert.equal("http://www.w3.org/1999/xlink", dom.data.attrs["xmlns:xlink"]);
assert.equal("http://www.w3.org/2000/svg", dom.data.attrs.xmlns);
assert.equal("rect", dom.children[0].sel);
assert.equal("100", dom.children[0].data.attrs.height);
assert.equal("10", dom.children[0].data.attrs.x);
assert.equal("stroke:#ff0000; fill: #0000ff", dom.children[0].data.attrs.style);
});
/*
it("should handle member tag name", function() {
//var tagName = "span";
var dom = <button.large x="100">Lorem
<span></span>
</button.large>;
assert.equal("button.large", dom.sel);
assert.equal("span", dom.children[1].sel);
});
*/
it("should handle self closing tags", function() {
var dom = <span class="button"/>;
assert.equal("span", dom.sel);
assert.equal("button", dom.data.attrs.class);
});
it("should handle function tag name", function() {
var X = function() {};
var dom = <X class="foo"/>;
assert.equal(X, dom.sel);
assert.equal("foo", dom.data.attrs.class);
});
it("should handle this", function() {
this.FOO = function() {};
var dom = <div on-click={this.FOO}>test</div>;
assert.equal(this.FOO, dom.data.on.click);
});
it("should handle boolean attributes", function() {
var dom = <input checked name="ok"/>;
assert.equal("input", dom.sel);
assert.equal(true, dom.data.attrs.checked);
});
it("should handle empty expressions", function() {
var dom = <input>{}</input>;
assert.equal("input", dom.sel);
assert.equal(null, dom.children);
});
it("should handle this in tag name", function() {
this.FOO = function() {};
var dom = <this.FOO>test</this.FOO>;
assert.equal(this.FOO, dom.sel);
});
it("should handle a child in code", function() {
var dom = <div>{ <span></span> }</div>;
assert.equal("span", dom.children[0].sel);
});
it("should handle children in code", function() {
var dom = <div>{ [<span></span>, <ul></ul>] }</div>;
assert.equal(2, dom.children.length);
assert.equal("ul", dom.children[1].sel);
});
it("should insert insert-hook", function() {
var onInsert = function(vnode) {
};
var dom = <div hook-insert={onInsert}>test</div>;
assert.equal(onInsert, dom.data.hook.insert);
});
// spread operator ... {...props} => props_={{props}}
});