-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
290 lines (267 loc) · 6.8 KB
/
test.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* eslint-disable no-template-curly-in-string */
'use strict'
const path = require('path')
const test = require('ava')
const { render, renderFile } = require('./index')
test('server side render', async t => {
t.plan(2)
try {
const rendered = await render(`
<div class="testing">
<h1>hello!</h1>
</div>
`)
t.true(rendered.includes('<h1>hello!</h1>'), 'contains a child element')
t.true(rendered.includes('<div class="testing">'), 'attribute gets set')
} catch (e) {
t.fail(e)
}
})
test('passing another element to html on server side render', async t => {
t.plan(1)
try {
const button = await render('<button>click</button>')
const rendered = await render(`
<div class="testing">
${button}
</div>
`)
t.true(rendered.includes('<button>click</button>'), 'button rendered correctly')
} catch (e) {
t.fail(e)
}
})
test('style attribute', async t => {
t.plan(1)
const expected = `
<h1 style="color: red">
Hey TEST, <span style="color: blue">This</span> is a card!!!
</h1>
`
const name = 'test'
try {
const result = await render(`
<h1 style="color: red">
Hey ${name.toUpperCase()}, <span style="color: blue">This</span> is a card!!!
</h1>
`)
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('unescape html', async t => {
t.plan(1)
try {
const expected = '<span>Hello <strong>there</strong></span>'
const result = await render('<span>Hello <strong>there</strong></span>')
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('event attribute', async t => {
t.plan(1)
const template = '<div onmouseover="${onmouseover}">Hello</div>'
const expected = '<div onmouseover="">Hello</div>'
try {
const result = await render(template, {
onmouseover: function () {}
})
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('boolean attribute', async t => {
t.plan(1)
const template = '<input disabled=${disabled} autofocus=${autofocus}>'
const expected = '<input disabled="disabled" >'
try {
const result = await render(template, {
disabled: true,
autofocus: false
})
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('spread attributes', async t => {
t.plan(1)
const template = '<div ${props}>Hello</div>'
const expected = '<div class="abc" id="def">Hello</div>'
try {
const result = await render(template, { props: { class: 'abc', id: 'def' } })
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('render HTML only', async t => {
t.plan(1)
const expected = '<p>Hello World</p>'
try {
const result = await render(expected)
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('evaluate JS expressions', async t => {
t.plan(1)
const template = '<p>High ${2 + 3}</p>'
const expected = '<p>High 5</p>'
try {
const result = await render(template)
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('simple value', async t => {
t.plan(1)
const template = '<p>High ${howMany}</p>'
const expected = '<p>High 5</p>'
try {
const result = await render(template, { howMany: 5 })
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('accept promises as values', async t => {
t.plan(1)
const template = '<p>High ${howMany}</p>'
const expected = '<p>High 5</p>'
try {
const result = await render(template, { howMany: Promise.resolve(5) })
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('unescaped/raw HTML value', async t => {
t.plan(1)
const template = '<p>${raw(highFive)}</p>'
const expected = '<p><strong>Up High</strong></p>'
try {
const result = await render(template, {
highFive: '<strong>Up High</strong>'
})
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('loop over object creating select options', async t => {
const template =
'<select>' +
'${Object.keys(states).map((s) => {' +
'return `<option value="${s}">${states[s]}</option>`' +
'})}' +
'</select>'
const expected =
'<select><option value="AL">Alabama</option><option value="GA">Georgia</option></select>'
try {
const result = await render(template, {
states: {
AL: 'Alabama',
GA: 'Georgia'
}
})
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test.cb('render with a callback, absent values', t => {
t.plan(1)
const expected = '<header>2018 © Nobody Important</header>'
render(expected, (err, result) => {
if (err) t.fail(err)
t.is(expected, result)
t.end()
})
})
test.cb('render with callback and values', t => {
t.plan(1)
const template = '<header><h1>${title}</h1></header>'
const expected = '<header><h1>Hello!</h1></header>'
render(template, { title: 'Hello!' }, (err, result) => {
if (err) t.fail(err)
t.is(expected, result)
t.end()
})
})
test.cb('render with template include', t => {
const template = '<h1>${heading}</h1>${include(\'test/b/c/c.html\')}'
const expected = '<h1>Hello!</h1><h2>2018 © Nobody Important</h2>'
render(template, { heading: 'Hello!' }, (err, result) => {
if (err) t.fail(err)
t.is(expected, result)
t.end()
})
})
test.cb('renderFile with a callback, absent values', t => {
t.plan(1)
const expected = '<h2>2018 © Nobody Important</h2>'
renderFile('test/b/c/c.html', (err, result) => {
if (err) t.fail(err)
t.is(expected, result)
t.end()
})
})
test('renderFile as promise, absent values', async t => {
t.plan(1)
try {
const expected = '<h2>2018 © Nobody Important</h2>'
const result = await renderFile('test/b/c/c.html')
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test.cb('renderFile with callback and values', t => {
t.plan(1)
const expected = '<header><h1>Hello!</h1><h2>2018 © Nobody Important</h2></header>'
renderFile('test/b/b.html', { heading: 'Hello!' }, (err, result) => {
if (err) t.fail(err)
t.is(expected, result)
t.end()
})
})
test('renderFile as promise with values', async t => {
t.plan(1)
try {
const expected = '<header><h1>Hello!</h1><h2>2018 © Nobody Important</h2></header>'
const result = await renderFile('test/b/b.html', { heading: 'Hello!' })
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})
test('renderFile that includes another template', async t => {
t.plan(1)
const expected =
`<html>
<head>
<title>So boring!</title>
</head>
<body>
<header><h1>Whaaat!</h1><h2>2018 © Nobody Important</h2></header>
</body>
</html>`
const location = path.join(__dirname, 'test/a.html')
try {
const result = await renderFile(location, {
title: 'So boring!',
header: {
heading: 'Whaaat!'
}
})
t.is(expected, result)
} catch (e) {
t.fail(e)
}
})