-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathindex.js
168 lines (146 loc) · 4.4 KB
/
index.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
global.document = require('html-element').document;
var test = require('tape')
var h = require('../')
var o = require('observable')
var spy = require('ispy')
var simu = require('simulate')
test('simple', function (t) {
t.equal(h('h1').outerHTML, '<h1></h1>')
t.equal(h('h1', 'hello world').outerHTML, '<h1>hello world</h1>')
t.end()
})
test('nested', function(t) {
t.equal(h('div',
h('h1', 'Title'),
h('p', 'Paragraph')
).outerHTML, '<div><h1>Title</h1><p>Paragraph</p></div>')
t.end()
})
test('arrays for nesting is ok', function(t){
t.equal(h('div',
[h('h1', 'Title'), h('p', 'Paragraph')]
).outerHTML, '<div><h1>Title</h1><p>Paragraph</p></div>')
t.end()
})
test('can use namespace in name', function(t){
t.equal(h('myns:mytag').outerHTML, '<myns:mytag></myns:mytag>');
t.end()
})
test('can use id selector', function(t){
t.equal(h('div#frame').outerHTML, '<div id="frame"></div>')
t.end()
})
test('can use class selector', function(t){
t.equal(h('div.panel').outerHTML, '<div class="panel"></div>')
t.end()
})
test('can default element types', function(t){
t.equal(h('.panel').outerHTML, '<div class="panel"></div>')
t.equal(h('#frame').outerHTML, '<div id="frame"></div>')
t.end()
})
test('can set properties', function(t){
var a = h('a', {href: 'http://google.com'})
t.equal(a.href, 'http://google.com/')
var checkbox = h('input', {name: 'yes', type: 'checkbox'})
t.equal(checkbox.outerHTML, '<input name="yes" type="checkbox">')
t.end()
})
test('registers event handlers', function(t){
var onClick = spy()
var p = h('p', {onclick: onClick}, 'something')
simu.click(p)
t.assert(onClick.called)
t.end()
})
test('sets styles', function(t){
var div = h('div', {style: {'color': 'red'}})
t.equal(div.style.color, 'red')
t.end()
})
test('sets styles as text', function(t){
var div = h('div', {style: 'color: red'})
t.equal(div.style.color, 'red')
t.end()
})
test('sets data attributes', function(t){
var div = h('div', {'data-value': 5})
t.equal(div.getAttribute('data-value'), '5') // failing for IE9
t.end()
})
test('boolean, number, date, regex get to-string\'ed', function(t){
var e = h('p', true, false, 4, new Date('Mon Jan 15 2001'), /hello/)
t.assert(e.outerHTML.match(/<p>truefalse4Mon Jan 15.+2001.*\/hello\/<\/p>/))
t.end()
})
test('observable content', function(t){
var title = o()
title('Welcome to HyperScript!')
var h1 = h('h1', title)
t.equal(h1.outerHTML, '<h1>Welcome to HyperScript!</h1>')
title('Leave, creep!')
t.equal(h1.outerHTML, '<h1>Leave, creep!</h1>')
t.end()
})
test('observable property', function(t){
var checked = o()
checked(true)
var checkbox = h('input', {type: 'checkbox', checked: checked})
t.equal(checkbox.checked, true)
checked(false)
t.equal(checkbox.checked, false)
t.end()
})
test('observable style', function(t){
var color = o()
color('red')
var div = h('div', {style: {'color': color}})
t.equal(div.style.color, 'red')
color('blue')
t.equal(div.style.color, 'blue')
t.end()
})
test('context basic', function(t){
var _h = h.context()
var p = _h('p', 'hello')
t.equal(p.outerHTML, '<p>hello</p>')
_h.cleanup()
t.end()
})
test('context cleanup removes observable listeners', function(t){
var _h = h.context()
var text = o()
text('hello')
var color = o()
color('red')
var className = o()
className('para')
var p = _h('p', {style: {color: color}, className: className}, text)
t.equal(p.outerHTML, '<p style=\"color: red; \" class=\"para\">hello</p>')
_h.cleanup()
color('blue')
text('world')
className('section')
t.equal(p.outerHTML, '<p style=\"color: red; \" class=\"para\">hello</p>')
t.end()
})
test('context cleanup removes event handlers', function(t){
var _h = h.context()
var onClick = spy()
var button = _h('button', 'Click me!', {onclick: onClick})
_h.cleanup()
simu.click(button)
t.assert(!onClick.called, 'click listener was not triggered')
t.end()
})
test('unicode selectors', function (t) {
t.equal(h('.⛄').outerHTML, '<div class="⛄"></div>')
t.equal(h('span#⛄').outerHTML, '<span id="⛄"></span>')
t.end()
})
test('classes from tag and attrs are merged', t => {
t.equal(h('.c1', {class: 'c2'}).outerHTML, '<div class="c1 c2"></div>')
t.equal(h('.c1.c2', {class: 'c3 c4'}).outerHTML, '<div class="c1 c2 c3 c4"></div>')
t.equal(h('.c1.c2', {class: 'c2 c3'}).outerHTML, '<div class="c1 c2 c3"></div>')
t.end()
})