Skip to content

Commit 54562b4

Browse files
test: refactor
1 parent 0b17496 commit 54562b4

File tree

4 files changed

+287
-180
lines changed

4 files changed

+287
-180
lines changed

test/render.test.js

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
4+
var it = require('mocha').it
5+
var expect = require('chai').expect
6+
var describe = require('mocha').describe
7+
8+
var render = require('../lib')
9+
10+
var tree = require('./templates/parser.js')
11+
var html = fs.readFileSync(path.resolve(__dirname, 'templates/render.html'), 'utf8')
12+
13+
describe('PostHTML Render', function () {
14+
it('{String}', function () {
15+
expect(render('Hello world!')).to.eql('Hello world!')
16+
})
17+
18+
it('{Number}', function () {
19+
expect(render(555)).to.eql('555')
20+
})
21+
22+
it('{Array}', function () {
23+
expect(render([ 'Hello world!' ])).to.eql('Hello world!')
24+
})
25+
26+
describe('Tags', function () {
27+
it('Empty', function () {
28+
expect(render({ content: [ 'Test' ] })).to.eql('<div>Test</div>')
29+
})
30+
31+
it('{Boolean} (false) -> {String}', function () {
32+
expect(render({ tag: false, content: 'Test' })).to.eql('Test')
33+
expect(render({ tag: false, content: [ 'Test' ] })).to.eql('Test')
34+
})
35+
36+
it('{Boolean} (false) -> {Number}', function () {
37+
expect(render({ tag: false, content: 555 })).to.eql('555')
38+
expect(render({ tag: false, content: [ 555 ] })).to.eql('555')
39+
})
40+
})
41+
42+
describe('Attrs', function () {
43+
it('Empty', function () {
44+
var fixture = { attrs: { alt: '' } }
45+
var expected = '<div alt=""></div>'
46+
47+
expect(render(fixture)).to.eql(expected)
48+
})
49+
50+
it('Single', function () {
51+
var fixture = {
52+
attrs: {
53+
id: 'header'
54+
}
55+
}
56+
var expected = '<div id="header"></div>'
57+
58+
expect(render(fixture)).to.eql(expected)
59+
})
60+
61+
it('Multiple', function () {
62+
var fixture = {
63+
attrs: {
64+
id: 'header',
65+
style: 'color:red',
66+
'data-id': 'header'
67+
}
68+
}
69+
var expected = '<div id="header" style="color:red" data-id="header"></div>'
70+
71+
expect(render(fixture)).to.eql(expected)
72+
})
73+
74+
it('{Boolean} (true)', function () {
75+
var fixture = {
76+
attrs: {
77+
disabled: true
78+
}
79+
}
80+
var expected = '<div disabled></div>'
81+
82+
expect(render(fixture)).to.eql(expected)
83+
})
84+
85+
it('{Boolean} (false)', function () {
86+
var fixture = {
87+
attrs: {
88+
disabled: false
89+
}
90+
}
91+
var expected = '<div></div>'
92+
93+
expect(render(fixture)).to.eql(expected)
94+
})
95+
96+
it('{Number}', function () {
97+
var fixture = {
98+
attrs: {
99+
val: 5
100+
}
101+
}
102+
var expected = '<div val="5"></div>'
103+
104+
expect(render(fixture)).to.eql(expected)
105+
})
106+
})
107+
108+
describe('Content', function () {
109+
it('{String}', function () {
110+
var fixture = { content: 'Hello world!' }
111+
var expected = '<div>Hello world!</div>'
112+
113+
expect(render(fixture)).to.eql(expected)
114+
})
115+
116+
it('{Array<String>}', function () {
117+
var fixture = { content: [ 'Hello world!' ] }
118+
var expected = '<div>Hello world!</div>'
119+
120+
expect(render(fixture)).to.eql(expected)
121+
})
122+
123+
it('{Number}', function () {
124+
expect(render({ content: 555 })).to.eql('<div>555</div>')
125+
expect(render({ content: [ 555 ] })).to.eql('<div>555</div>')
126+
})
127+
128+
it('{Array<Number>}', function () {
129+
expect(render({ content: [ 555 ] })).to.eql('<div>555</div>')
130+
})
131+
132+
it('{Array}', function () {
133+
var fixture = {
134+
content: [
135+
[
136+
555,
137+
{ tag: 'div', content: 666 },
138+
777
139+
]
140+
]
141+
}
142+
var expected = '<div>555<div>666</div>777</div>'
143+
144+
expect(render(fixture)).to.eql(expected)
145+
})
146+
147+
it('Nested', function () {
148+
var fixture = {
149+
content: [
150+
{
151+
content: [
152+
{
153+
content: [ 'Test', {} ]
154+
}
155+
]
156+
}
157+
]
158+
}
159+
var expected = '<div><div><div>Test<div></div></div></div></div>'
160+
161+
expect(render(fixture)).to.eql(expected)
162+
})
163+
})
164+
165+
describe('Tree', function () {
166+
it('Empty', function () {
167+
expect(render()).to.eql('')
168+
})
169+
170+
it('HTML', function () {
171+
expect(html).to.eql(render(tree))
172+
})
173+
174+
it('Immutable', function () {
175+
var t = [{
176+
tag: 'div',
177+
content: [
178+
{
179+
tag: false,
180+
content: [
181+
{ tag: 'div' },
182+
{ tag: 'span', content: ['Text'] }
183+
]
184+
}
185+
]
186+
}]
187+
188+
var t1 = JSON.stringify(t)
189+
190+
render(t)
191+
192+
var t2 = JSON.stringify(t)
193+
194+
expect(t1).to.eql(t2)
195+
})
196+
})
197+
198+
describe('Options', function () {
199+
describe('singleTag', function () {
200+
it('Defaults', function () {
201+
var SINGLE_TAGS = [
202+
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'
203+
]
204+
205+
expect(render(
206+
SINGLE_TAGS.map(function (tag) {
207+
return { tag: tag }
208+
}
209+
))).to.eql(
210+
SINGLE_TAGS.map(function (tag) {
211+
return '<' + tag + '>'
212+
}
213+
).join(''))
214+
})
215+
216+
it('Custom (Options)', function () {
217+
var options = { singleTags: [ 'rect' ] }
218+
219+
var fixture = { tag: 'rect' }
220+
var expected = '<rect>'
221+
222+
expect(render(fixture, options)).to.eql(expected)
223+
})
224+
225+
it('Attrs', function () {
226+
var options = { singleTags: [ 'rect' ] }
227+
228+
var fixture = { tag: 'rect', attrs: { id: 'id' } }
229+
var expected = '<rect id="id">'
230+
231+
expect(render(fixture, options)).to.eql(expected)
232+
})
233+
})
234+
235+
describe('closingSingleTag', function () {
236+
it('Tag', function () {
237+
var options = { closingSingleTag: 'tag' }
238+
239+
var fixture = { tag: 'br' }
240+
var expected = '<br></br>'
241+
242+
expect(render(fixture, options)).to.eql(expected)
243+
})
244+
245+
it('Slash', function () {
246+
var options = { closingSingleTag: 'slash' }
247+
248+
var fixture = { tag: 'br' }
249+
var expected = '<br />'
250+
251+
expect(render(fixture, options)).to.eql(expected)
252+
})
253+
254+
it('Default', function () {
255+
var options = { closingSingleTag: 'default' }
256+
257+
var fixture = { tag: 'br' }
258+
var expected = '<br>'
259+
260+
expect(render(fixture, options)).to.eql(expected)
261+
})
262+
})
263+
})
264+
})

test/templates/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>PostHTML Render</title>
5+
<script src="../../lib/browser.min.js"></script>
6+
<script>
7+
window.onload = function () {
8+
var tree = {
9+
tag: 'h1',
10+
attrs: {
11+
id: 'header',
12+
style: 'color:red;',
13+
'data-id': 'header'
14+
},
15+
content: 'Test'
16+
}
17+
18+
document.body.innerHTML = render(tree);
19+
};
20+
</script>
21+
</head>
22+
<body></body>
23+
</html>

test/templates/test.html

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)