This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
/
test.coffee
205 lines (157 loc) · 7.5 KB
/
test.coffee
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
{html} = require './index'
{parseTagString} = common = require './common'
module.exports =
'isComponent':
'object with render function is a component': (test) ->
test.ok common.isComponent {render: ->}
test.done()
'empty object is not a component': (test) ->
test.ok not common.isComponent {}
test.done()
'string is not a component': (test) ->
test.ok not common.isComponent 'foo'
test.done()
'parseTag':
'empty': (test) ->
test.ok not common.parseTag ''
test.done()
'invalid tag': (test) ->
test.ok not common.parseTag '$h1'
test.done()
'valid tag': (test) ->
test.equal 'h1', common.parseTag 'h1'
test.done()
'tag with suffix': (test) ->
test.equal 'h1', common.parseTag 'h1#heading.awesome.super'
test.done()
'parseId':
'empty': (test) ->
test.ok not common.parseId ''
test.done()
'pure id': (test) ->
test.equal 'heading', common.parseId '#heading'
test.done()
'id in tagstring': (test) ->
test.equal 'heading', common.parseId 'h1#heading.awesome.super'
test.done()
'parseClass':
'empty': (test) ->
test.ok not common.parseClass ''
test.done()
'one class': (test) ->
test.equal 'awesome', common.parseClass '.awesome'
test.done()
'three classes': (test) ->
test.equal 'awesome super cool', common.parseClass '.awesome.super.cool'
test.done()
'three classes in tagstring': (test) ->
test.equal 'awesome super cool', common.parseClass 'h1.awesome.super#heading.cool'
test.done()
'html':
'throw when':
'attribute value is undefined': (test) ->
test.throws -> html ['div', {foo: undefined}]
test.done()
'void tag has content': (test) ->
test.throws -> html ['input', 'lorem ipsum']
test.throws -> html ['input', ['span']]
test.done()
'missing data': (test) ->
test.throws -> html null
test.done()
'empty array': (test) ->
data = []
test.equal html(data), ''
test.done()
'string is encoded': (test) ->
data = '<script>alert("foo"); alert(\'bar\');</script>'
test.equal html(data), "<script>alert("foo"); alert('bar');</script>"
test.done()
'void tag':
'without attributes': (test) ->
data = ['input']
test.equal html(data), '<input />'
test.done()
'with attributes': (test) ->
data = ['input', {type: 'text', name: 'data'}]
test.equal html(data), '<input type="text" name="data" />'
test.done()
'with classes and ids in tag string': (test) ->
data = ['input#my-id.my-class-1.my-class-2']
test.equal html(data), '<input id="my-id" class="my-class-1 my-class-2" />'
test.done()
'attributes override classes and ids in tag string': (test) ->
data = ['input#my-id.my-class', {class: 'my-real-class', id: 'my-real-id'}]
test.equal html(data), '<input class="my-real-class" id="my-real-id" />'
test.done()
'attributes are encoded': (test) ->
data = ['img', {id: '" onclick=\'alert("foo")\''}]
test.equal html(data), '<img id="" onclick=\'alert("foo")\'" />'
test.done()
'regular tag':
'empty':
'without attributes': (test) ->
data = ['div']
test.equal html(data), '<div></div>'
test.done()
'with attributes': (test) ->
data = ['div', {'data-toggle': 'tag', style: 'margin: 10px'}]
test.equal html(data), '<div data-toggle="tag" style="margin: 10px"></div>'
test.done()
'text content':
'without attributes': (test) ->
data = ['a', 'my text content']
test.equal html(data), '<a>my text content</a>'
test.done()
'with attributes': (test) ->
data = ['a', {href: '/', class: 'my-class'}, 'my text content']
test.equal html(data), '<a href="/" class="my-class">my text content</a>'
test.done()
'html content':
'without attributes': (test) ->
data = ['p',
['a', 'first link']
['p', ['a', 'second link']]]
test.equal html(data), '<p><a>first link</a><p><a>second link</a></p></p>'
test.done()
'with attributes': (test) ->
data = ['p', {id: 'container'},
['a', 'first link']
['p', ['a', 'second link']]]
test.equal html(data), '<p id="container"><a>first link</a><p><a>second link</a></p></p>'
test.done()
'component':
'direct argument': (test) ->
component = {render: -> ['p', 'paragraph from a component']}
test.equal html(component), '<p>paragraph from a component</p>'
test.done()
'in html': (test) ->
component = {render: -> ['p', 'paragraph from a component']}
data = ['div', {align: 'left'}, ['span', component, 'text after component']]
test.equal html(data), '<div align="left"><span><p>paragraph from a component</p>text after component</span></div>'
test.done()
'component array in html': (test) ->
data = ['div', [
{render: -> ['p', 'first']}
{render: -> ['p', 'second']}
{render: -> ['p', 'third']}
]]
test.equal html(data), '<div><p>first</p><p>second</p><p>third</p></div>'
test.done()
'everything together': (test) ->
data =
['html',
['head',
['title', 'a title']
['script', {
src: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'
type: 'text/javascript'}]]
['body',
['div#container',
['h1', 'a heading']
['h2.secondary-heading', 'another heading']
['ul', 'first second third'.split(' ').map((x) -> ['li', ['a', x]])]
['p', 'Before the break', ['br'], 'After the break']]]]
expected ='<html><head><title>a title</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script></head><body><div id="container"><h1>a heading</h1><h2 class="secondary-heading">another heading</h2><ul><li><a>first</a></li><li><a>second</a></li><li><a>third</a></li></ul><p>Before the break<br />After the break</p></div></body></html>'
test.equal html(data), expected
test.done()