-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.js
382 lines (334 loc) · 14.4 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const should = require('should'),
{ PassThrough } = require('stream'),
Handlebars = require('handlebars'),
asyncHelpers = require('../index')
const { resolve } = require('eslint-plugin-promise/rules/lib/promise-statics')
describe('Test async helpers', () => {
it('Test single async helper', async () => {
const hbs = asyncHelpers(Handlebars)
hbs.registerHelper('sleep', async () => new Promise((resolve) => {
setTimeout(() => resolve('Done!'), 50)
}))
const result = await hbs.compile('Mark when is completed: {{#sleep}}{{/sleep}}')()
should.equal(result, 'Mark when is completed: Done!')
})
it('Test async helper with partial template', async () => {
const hbs = asyncHelpers(Handlebars)
hbs.registerPartial('myPartial', 'Some text: {{#sleep}}{{/sleep}}')
hbs.registerHelper('sleep', async () => new Promise((resolve) => {
setTimeout(() => resolve('Done!'), 50)
}))
const result = await hbs.compile('Mark when is completed: {{> myPartial}}')()
should.equal(result, 'Mark when is completed: Some text: Done!')
})
it('Test each helper with async helpers inside', async () => {
const hbs = asyncHelpers(Handlebars)
hbs.registerHelper('sleep', async () => new Promise((resolve) => {
setTimeout(() => resolve('Done!'), 50)
}))
const items = [
'Gaston',
'Joaquin',
'James',
'Ala',
'Giannis',
'Adam',
'Drew'
],
result = await hbs.compile('Devs \n{{#each items}}Dev: {{this}} {{#sleep}}{{/sleep}}\n{{/each}}')({items})
should.equal(result, 'Devs \nDev: Gaston Done!\nDev: Joaquin Done!\nDev: James Done!\nDev: Ala Done!\nDev: Giannis Done!\nDev: Adam Done!\nDev: Drew Done!\n')
})
it('Test each helper with async helpers resolver', async () => {
const hbs = asyncHelpers(Handlebars),
items = Promise.resolve([
'Gaston',
'Joaquin',
'James',
'Ala',
'Giannis',
'Adam',
'Drew'
])
hbs.registerHelper('getArray', () => new Promise((resolve) => {
setTimeout(() => resolve(items), 50)
}))
const result = await hbs.compile('Devs \n{{#each (getArray items)}}Dev: {{this}}\n{{/each}}')({items})
should.equal(result, 'Devs \nDev: Gaston\nDev: Joaquin\nDev: James\nDev: Ala\nDev: Giannis\nDev: Adam\nDev: Drew\n')
})
it('Test lookupProperty', async() => {
const hbs = asyncHelpers(Handlebars),
template = `{{person.[0].firstName}}`,
expected = 'John'
const compiled = hbs.compile(template),
result = await compiled({
person: [
Promise.resolve({firstName: 'John', lastName: 'Q'}),
]
})
should.equal(result, expected)
})
it('Test lookupProperty with nested promises', async() => {
const hbs = asyncHelpers(Handlebars),
template = `{{person.[0].firstName}}`,
expected = 'John'
const compiled = hbs.compile(template),
result = await compiled({
person: Promise.all([
Promise.resolve({firstName: 'John', lastName: 'Q'}),
])
})
should.equal(result, expected)
})
it('Test with helper', async () => {
const hbs = asyncHelpers(Handlebars),
template = `<div class="names">
<ul>
<li>John, Q</li>
<li>John, McKlein</li>
<li>Susan, Morrison</li>
<li>Mick, Jagger</li>
</ul>
</div>
<div>
{{#with (ipInfo)}}
<p>Country: {{country}}</p>
{{/with}}
</div>`,
expected = `<div class="names">
<ul>
<li>John, Q</li>
<li>John, McKlein</li>
<li>Susan, Morrison</li>
<li>Mick, Jagger</li>
</ul>
</div>
<div>
<p>Country: Canada</p>
</div>`
hbs.registerHelper('ipInfo', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 50)
})
return {country: 'Canada'}
})
const compiled = hbs.compile(template),
result = await compiled({
person: [
{firstName: 'John', lastName: 'Q'},
{firstName: 'John', lastName: 'McKlein'},
{firstName: 'Susan', lastName: 'Morrison'},
{firstName: 'Mick', lastName: 'Jagger'}
]
})
should.equal(result, expected)
})
it('Test if helper', async () => {
const hbs = asyncHelpers(Handlebars),
template = `{{#if (shouldShow data)}}<p>Show</p>{{else}}<p>No Show</p>{{/if}}`,
expected = `<p>Show</p>`
hbs.registerHelper('shouldShow', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 50)
})
return true
})
const compiled = hbs.compile(template),
result = await compiled({
data: {}
})
should.equal(result, expected)
})
it('Test unless helper', async () => {
const hbs = asyncHelpers(Handlebars),
template = `{{#unless (shouldShow data)}}<p>Show</p>{{/unless}}`,
expected = `<p>Show</p>`
hbs.registerHelper('shouldShow', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 50)
})
return null
})
const compiled = hbs.compile(template),
result = await compiled({
data: {}
})
should.equal(result, expected)
})
it('Test with custom helpers and complex replacements', async() => {
const timeout = ms => new Promise(res => setTimeout(res, ms)),
delay = async function delayFn() {
await timeout(50)
return 1000
},
hbs = asyncHelpers(Handlebars)
hbs.registerHelper({
extend: async function(partial, options) {
let context = this,
// noinspection JSUnresolvedVariable
template = hbs.partials[partial] || options.data?.partials?.partial
// Partial template required
if (typeof template === 'undefined') {
throw new Error("Missing layout partial: '" + partial + "'")
}
// Parse blocks and discard output
await options.fn(context)
if (!(typeof template === 'function')) {
template = hbs.compile(template)
}
// Render final layout partial with revised blocks
return template(context, options)
},
append: function(block, options) {
this.blocks = this.blocks || {}
this.blocks[block] = {
should: 'append',
fn: options.fn
}
},
prepend: function(block, options) {
this.blocks = this.blocks || {}
this.blocks[block] = {
should: 'prepend',
fn: options.fn
}
},
replace: function(block, options) {
this.blocks = this.blocks || {}
this.blocks[block] = {
should: 'replace',
fn: options.fn
}
},
block: function(name, options) {
this.blocks = this.blocks || {}
let block = this.blocks[name]
let results = []
switch (block && block.fn && block.should) {
case 'append':
results.push(options.fn(this))
results.push(block.fn(this))
break
case 'prepend':
results.push(block.fn(this))
results.push(options.fn(this))
break
case 'replace':
results.push(block.fn(this))
break
default:
results.push(options.fn(this))
break
}
return Promise.all(results).then(results => results.join(''))
}
})
hbs.registerHelper('delay', delay)
hbs.registerHelper('cursor', async(options) => {
await timeout(50)
return [{ name: 'test' }, { name: 'test2' }]
})
hbs.registerPartial('layout', '<html><body><h1>Layout</h1><div>{{#block "body_replace"}}<i>Text before</i>{{/block}}</div><div>{{#block "body_append"}}<i>Text before content</i>{{/block}}</div><div>{{#block "body_prepend"}}<i>Text after content</i>{{/block}}</div></body></html>')
const compiled = hbs.compile(`{{#extend "layout"}}{{#prepend "body_prepend"}}{{#each (cursor)}}{{#if @first}}<span>test first</span>{{/if}}<div><h2>{{name}}</h2><p>{{#delay}}{{/delay}}</p></div>{{/each}}{{/prepend}}{{#append "body_append"}}{{#each (cursor)}}<div><a>{{name}}</a><p>{{#delay}}{{/delay}}</p></div>{{/each}}{{/append}}{{#replace "body_replace"}}<ul>{{#each (cursor)}}<li>{{name}} - {{#delay}}{{/delay}}</li>{{/each}}</ul>{{/replace}}{{/extend}}`),
expected = '<html><body><h1>Layout</h1><div><ul><li>test - 1000</li><li>test2 - 1000</li></ul></div><div><i>Text before content</i><div><a>test</a><p>1000</p></div><div><a>test2</a><p>1000</p></div></div><div><span>test first</span><div><h2>test</h2><p>1000</p></div><div><h2>test2</h2><p>1000</p></div><i>Text after content</i></div></body></html>'
should.equal(await compiled(), expected)
})
it('Test single variable be a promise value', async () => {
const hbs = asyncHelpers(Handlebars),
template = `<p>{{value}}</p>`,
expected = `<p>Gaston Robledo</p>`
const compiled = hbs.compile(template),
result = await compiled({
value: new Promise((resolve) => resolve('Gaston Robledo'))
})
should.equal(result, expected)
})
it('Test each with a stream source', async () => {
const hbs = asyncHelpers(Handlebars),
template = `{{#each source}}<p>{{this}}</p>{{/each}}`,
expected = `<p>Gaston</p><p>Joaquin</p><p>James</p>`,
source = new PassThrough()
source.push('Gaston')
source.push('Joaquin')
source.end('James')
const compiled = hbs.compile(template),
result = await compiled({
source
})
should.equal(result, expected)
})
it('Test normal usage of handlebars', async () => {
const hbs = asyncHelpers(Handlebars),
template = `{{#each source}}<p>{{this}}</p>{{/each}}{{#if showThis}}<p>Showed</p>{{/if}}`,
expected = `<p>Gaston</p><p>Joaquin</p><p>James</p><p>Showed</p>`,
compiled = hbs.compile(template),
result = await compiled({
source: ['Gaston', 'Joaquin', 'James'],
showThis: true
})
should.equal(result, expected)
})
it('Test nested partials', async () => {
const hbs = asyncHelpers(Handlebars),
template = '<div>Parent {{> child}}</div>',
child = '<div>Child: {{> grandChild}}</div>',
grandChild = '<p>Grand Child: {{#delayed 50}}{{/delayed}}</p>',
expected = '<div>Parent <div>Child: <p>Grand Child: Hello!</p></div></div>'
hbs.registerHelper('delayed', (time) => {
return new Promise((resolve) => {
setTimeout(() => resolve('Hello!'), time)
})
})
hbs.registerPartial('child', child)
hbs.registerPartial('grandChild', grandChild)
const compiled = hbs.compile(template),
result = await compiled()
should.equal(result, expected)
})
it('Test indentation with async partial', async () => {
const hbs = asyncHelpers(Handlebars),
template = `<div>Parent
{{> child}}
<div class="test">
{{> child}}
</div>
</div>`,
child = '<div>Child:\n\t\t\t{{> grandChild}}\n</div>',
grandChild = '<p>\nGrand Child: {{#delayed 50}}\n{{/delayed}}\n</p>'
hbs.registerHelper('delayed', (time) => {
return new Promise((resolve) => {
setTimeout(() => resolve('Hello!'), time)
})
})
hbs.registerPartial('child', child)
hbs.registerPartial('grandChild', grandChild)
const compiled = hbs.compile(template)
const result = compiled()
await result.should.be.fulfilled();
})
it('Test synchronous helper', async () => {
const hbs = asyncHelpers(Handlebars),
template = '<div>Value: {{#multiply 100 20}}{{/multiply}}</div>',
expected = '<div>Value: 2000</div>'
hbs.registerHelper('multiply', (a, b) => {
return a * b
})
const compiled = hbs.compile(template),
result = await compiled()
should.equal(result, expected)
})
it('Test custom helper without noEscape', async () => {
const hbs = asyncHelpers(Handlebars),
template = '<div>Value: {{toUpperAsync "my text"}}</div>',
expected = '<div>Value: MY TEXT</div>'
hbs.registerHelper('toUpperAsync', async (value) => {
return Promise.resolve(value.toUpperCase())
})
const compiled = hbs.compile(template),
result = await compiled()
should.equal(result, expected)
})
it('check version', () => {
const hbs = asyncHelpers(Handlebars)
should(hbs.ASYNC_VERSION).equal(require('../package.json').version)
})
})