-
Notifications
You must be signed in to change notification settings - Fork 780
/
h.js
200 lines (137 loc) · 4.01 KB
/
h.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
var test = require("tape")
var EvStore = require('ev-store')
var h = require("../index")
test("h is a function", function (assert) {
assert.equal(typeof h, "function")
assert.end()
})
test("h returns a vnode", function (assert) {
assert.equal(h("div").tagName, "DIV")
assert.end()
})
test("h defaults tagName to uppercase", function (assert) {
assert.equal(h("").tagName, "DIV")
assert.equal(h("div").tagName, "DIV")
assert.end()
})
test("h preserves tagName case if namespace is given", function (assert) {
assert.equal(h("test", { namespace: "http://www.w3.org/XML/1998/namespace" }).tagName, "test")
assert.end()
})
test("h has props", function (assert) {
assert.equal(h("div", {
foo: "bar"
}).properties.foo, "bar")
assert.end()
})
test("h with text", function (assert) {
var node = h("div", "text")
assert.equal(node.children[0].text, "text")
assert.end()
})
test("h with key", function (assert) {
var node = h("div", { key: "bar" })
assert.equal(node.key, "bar")
assert.end()
})
test("h with ev-", function (assert) {
var node = h("div", { "ev-foo": "bar" })
assert.ok(node.properties["ev-foo"])
var hook = node.properties["ev-foo"]
var elem = {}
hook.hook(elem, "ev-foo")
assert.equal(EvStore(elem).foo, "bar")
assert.end()
})
test("input.value soft hook", function (assert) {
var node = h("input", { value: "text" })
assert.equal(typeof node.properties.value, "object")
var elem = {}
node.properties.value.hook(elem, "value")
assert.equal(elem.value, "text")
assert.end()
})
test("input.value must be string", function (assert) {
assert.throws(function() {
var node = h("input", { value: 1234 })
});
assert.throws(function() {
var node = h("input", { value: {} })
});
assert.end()
})
test("h with child", function (assert) {
var node = h("div", h("span"))
assert.equal(node.children[0].tagName, "SPAN")
assert.end()
})
test("h with children", function (assert) {
var node = h("div", [h("span")])
assert.equal(node.children[0].tagName, "SPAN")
assert.end()
})
test("h with null", function (assert) {
var node = h("div", null)
var node2 = h("div", [null])
assert.equal(node.children.length, 0)
assert.equal(node2.children.length, 0)
assert.end()
})
test("h with undefined", function (assert) {
var node = h("div", undefined)
var node2 = h("div", [undefined])
assert.equal(node.children.length, 0)
assert.equal(node2.children.length, 0)
assert.end()
})
test("h with foreign object", function (assert) {
var errorSingleChild
try {
h("div", null, { foreign: "object" })
} catch (e) {
errorSingleChild = e
}
var errorChildren
try {
h("div", [{ foreign: "object" }])
} catch (e) {
errorChildren = e
}
assert.ok(errorSingleChild);
assert.ok(/Unexpected virtual child/.test(errorSingleChild.message))
assert.ok(errorChildren);
assert.ok(/Unexpected virtual child/.test(errorChildren.message))
assert.end()
})
test("h with class", function (assert) {
var node = h(".foo")
assert.equal(node.properties.className, "foo")
assert.end()
})
test("h with id", function (assert) {
var node = h("#foo")
assert.equal(node.properties.id, "foo")
assert.end()
})
test("h with empty string", function (assert) {
var node = h("")
assert.equal(node.tagName, "DIV")
assert.end()
})
test("h with two classes", function (assert) {
var node = h(".foo", { className: "bar" })
assert.equal(node.properties.className, "foo bar")
assert.end()
})
test("h with two ids", function (assert) {
var node = h("#foo", { id: "bar" })
assert.equal(node.properties.id, "bar")
assert.end()
})
test("h with Number", function (assert) {
var node = h("div", 1)
var node2 = h("div", [1])
assert.equal(node.children[0].text, "1")
assert.equal(node2.children[0].text, "1")
assert.end()
})