Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/test/squirrelly.test.js
162 views
1
'use strict'
2
3
const { test } = require('node:test')
4
const Fastify = require('fastify')
5
const fs = require('node:fs')
6
const path = require('node:path')
7
const Sqrl = require('squirrelly')
8
9
const pointOfView = require('../index')
10
11
require('./helper').squirrellyHtmlMinifierTests(true)
12
require('./helper').squirrellyHtmlMinifierTests(false)
13
14
test('reply.view with squirrelly engine and custom templates folder', async t => {
15
t.plan(4)
16
const fastify = Fastify()
17
18
const data = { text: 'text' }
19
20
fastify.register(pointOfView, {
21
engine: {
22
squirrelly: Sqrl
23
},
24
templates: 'templates'
25
})
26
27
fastify.get('/', (_req, reply) => {
28
reply.view('index.squirrelly', data)
29
})
30
31
const address = await fastify.listen({ port: 0 })
32
33
const result = await fetch(address)
34
const responseContent = await result.text()
35
36
t.assert.strictEqual(result.status, 200)
37
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
38
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
39
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
40
41
await fastify.close()
42
})
43
44
test('reply.view with squirrelly engine and custom ext', async t => {
45
t.plan(4)
46
const fastify = Fastify()
47
48
const data = { text: 'text' }
49
50
fastify.register(pointOfView, {
51
engine: {
52
squirrelly: Sqrl
53
},
54
templates: 'templates',
55
viewExt: 'squirrelly'
56
})
57
58
fastify.get('/', (_req, reply) => {
59
reply.view('index', data)
60
})
61
62
const address = await fastify.listen({ port: 0 })
63
64
const result = await fetch(address)
65
const responseContent = await result.text()
66
67
t.assert.strictEqual(result.status, 200)
68
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
69
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
70
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
71
72
await fastify.close()
73
})
74
75
test('reply.view for squirrelly without data-parameter but defaultContext', async t => {
76
t.plan(4)
77
const fastify = Fastify()
78
79
const data = { text: 'text' }
80
81
fastify.register(pointOfView, {
82
engine: {
83
squirrelly: Sqrl
84
},
85
defaultContext: data,
86
templates: 'templates'
87
})
88
89
fastify.get('/', (_req, reply) => {
90
reply.view('index.squirrelly')
91
})
92
93
const address = await fastify.listen({ port: 0 })
94
95
const result = await fetch(address)
96
const responseContent = await result.text()
97
98
t.assert.strictEqual(result.status, 200)
99
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
100
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
101
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
102
103
await fastify.close()
104
})
105
106
test('reply.view for squirrelly without data-parameter and without defaultContext', async t => {
107
t.plan(4)
108
const fastify = Fastify()
109
110
fastify.register(pointOfView, {
111
engine: {
112
squirrelly: Sqrl
113
},
114
templates: 'templates'
115
})
116
117
fastify.get('/', (_req, reply) => {
118
reply.view('index-bare.html')
119
})
120
121
const address = await fastify.listen({ port: 0 })
122
123
const result = await fetch(address)
124
125
const responseContent = await result.text()
126
127
t.assert.strictEqual(result.status, 200)
128
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
129
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
130
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index-bare.html', 'utf8')), responseContent)
131
132
await fastify.close()
133
})
134
135
test('reply.view for squirrelly engine without data-parameter and defaultContext but with reply.locals', async t => {
136
t.plan(4)
137
const fastify = Fastify()
138
139
const localsData = { text: 'text from locals' }
140
141
fastify.register(pointOfView, {
142
engine: {
143
squirrelly: Sqrl
144
}
145
})
146
147
fastify.addHook('preHandler', function (_request, reply, done) {
148
reply.locals = localsData
149
done()
150
})
151
152
fastify.get('/', (_req, reply) => {
153
reply.view('./templates/index-bare.html')
154
})
155
156
const address = await fastify.listen({ port: 0 })
157
158
const result = await fetch(address)
159
160
const responseContent = await result.text()
161
162
t.assert.strictEqual(result.status, 200)
163
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
164
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
165
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index-bare.html', 'utf8'), localsData), responseContent)
166
167
await fastify.close()
168
})
169
170
test('reply.view for squirrelly engine without defaultContext but with reply.locals and data-parameter', async t => {
171
t.plan(4)
172
const fastify = Fastify()
173
174
const localsData = { text: 'text from locals' }
175
const data = { text: 'text' }
176
177
fastify.register(pointOfView, {
178
engine: {
179
squirrelly: Sqrl
180
}
181
})
182
183
fastify.addHook('preHandler', function (_request, reply, done) {
184
reply.locals = localsData
185
done()
186
})
187
188
fastify.get('/', (_req, reply) => {
189
reply.view('./templates/index-bare.html', data)
190
})
191
192
const address = await fastify.listen({ port: 0 })
193
194
const result = await fetch(address)
195
196
const responseContent = await result.text()
197
198
t.assert.strictEqual(result.status, 200)
199
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
200
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
201
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index-bare.html', 'utf8'), data), responseContent)
202
203
await fastify.close()
204
})
205
206
test('reply.view for squirrelly engine without data-parameter but with reply.locals and defaultContext', async t => {
207
t.plan(4)
208
const fastify = Fastify()
209
210
const localsData = { text: 'text from locals' }
211
const contextData = { text: 'text from context' }
212
213
fastify.register(pointOfView, {
214
engine: {
215
squirrelly: Sqrl
216
},
217
defaultContext: contextData
218
})
219
220
fastify.addHook('preHandler', function (_request, reply, done) {
221
reply.locals = localsData
222
done()
223
})
224
225
fastify.get('/', (_req, reply) => {
226
reply.view('./templates/index-bare.html')
227
})
228
229
const address = await fastify.listen({ port: 0 })
230
231
const result = await fetch(address)
232
233
const responseContent = await result.text()
234
235
t.assert.strictEqual(result.status, 200)
236
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
237
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
238
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index-bare.html', 'utf8'), localsData), responseContent)
239
240
await fastify.close()
241
})
242
243
test('reply.view for squirrelly engine with data-parameter and reply.locals and defaultContext', async t => {
244
t.plan(4)
245
const fastify = Fastify()
246
247
const localsData = { text: 'text from locals' }
248
const contextData = { text: 'text from context' }
249
const data = { text: 'text' }
250
251
fastify.register(pointOfView, {
252
engine: {
253
squirrelly: Sqrl
254
},
255
defaultContext: contextData
256
})
257
258
fastify.addHook('preHandler', function (_request, reply, done) {
259
reply.locals = localsData
260
done()
261
})
262
263
fastify.get('/', (_req, reply) => {
264
reply.view('./templates/index-bare.html', data)
265
})
266
267
const address = await fastify.listen({ port: 0 })
268
269
const result = await fetch(address)
270
271
const responseContent = await result.text()
272
273
t.assert.strictEqual(result.status, 200)
274
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
275
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
276
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index-bare.html', 'utf8'), data), responseContent)
277
278
await fastify.close()
279
})
280
281
test('reply.view with squirrelly engine and full path templates folder', async t => {
282
t.plan(4)
283
const fastify = Fastify()
284
285
const data = { text: 'text' }
286
287
fastify.register(pointOfView, {
288
engine: {
289
squirrelly: Sqrl
290
},
291
templates: path.join(__dirname, '../templates')
292
})
293
294
fastify.get('/', (_req, reply) => {
295
reply.view('index.squirrelly', data)
296
})
297
298
const address = await fastify.listen({ port: 0 })
299
300
const result = await fetch(address)
301
302
const responseContent = await result.text()
303
304
t.assert.strictEqual(result.status, 200)
305
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
306
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
307
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
308
309
await fastify.close()
310
})
311
312
test('reply.view with squirrelly engine', async t => {
313
t.plan(4)
314
const fastify = Fastify()
315
316
const data = { text: 'text' }
317
318
fastify.register(pointOfView, {
319
engine: {
320
squirrelly: Sqrl
321
}
322
})
323
324
fastify.get('/', (_req, reply) => {
325
reply.view('templates/index.squirrelly', data)
326
})
327
328
const address = await fastify.listen({ port: 0 })
329
330
const result = await fetch(address)
331
332
const responseContent = await result.text()
333
334
t.assert.strictEqual(result.status, 200)
335
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
336
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
337
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
338
339
await fastify.close()
340
})
341
342
test('reply.view with squirrelly engine and defaultContext', async t => {
343
t.plan(4)
344
const fastify = Fastify()
345
346
const data = { text: 'text' }
347
348
fastify.register(pointOfView, {
349
engine: {
350
squirrelly: Sqrl
351
},
352
defaultContext: data
353
})
354
355
fastify.get('/', (_req, reply) => {
356
reply.view('templates/index.squirrelly', {})
357
})
358
359
const address = await fastify.listen({ port: 0 })
360
361
const result = await fetch(address)
362
363
const responseContent = await result.text()
364
365
t.assert.strictEqual(result.status, 200)
366
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
367
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
368
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
369
370
await fastify.close()
371
})
372
373
test('reply.view with squirrelly engine and includeViewExtension property as true', async t => {
374
t.plan(4)
375
const fastify = Fastify()
376
377
const data = { text: 'text' }
378
379
fastify.register(pointOfView, {
380
engine: {
381
squirrelly: Sqrl
382
},
383
includeViewExtension: true
384
})
385
386
fastify.get('/', (_req, reply) => {
387
reply.view('templates/index', data)
388
})
389
390
const address = await fastify.listen({ port: 0 })
391
392
const result = await fetch(address)
393
394
const responseContent = await result.text()
395
396
t.assert.strictEqual(result.status, 200)
397
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
398
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
399
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
400
401
await fastify.close()
402
})
403
404
test('fastify.view with squirrelly engine and callback in production mode', (t, end) => {
405
t.plan(6)
406
const fastify = Fastify()
407
408
const data = { text: 'text' }
409
410
fastify.register(pointOfView, {
411
engine: {
412
squirrelly: Sqrl
413
},
414
production: true
415
})
416
417
fastify.ready(err => {
418
t.assert.ifError(err)
419
420
fastify.view('templates/index.squirrelly', data, (err, compiled) => {
421
t.assert.ifError(err)
422
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), compiled)
423
424
fastify.ready(err => {
425
t.assert.ifError(err)
426
427
fastify.view('templates/index.squirrelly', data, (err, compiled) => {
428
t.assert.ifError(err)
429
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), compiled)
430
fastify.close()
431
end()
432
})
433
})
434
})
435
})
436
})
437
438
test('fastify.view with squirrelly engine, should throw page missing', (t, end) => {
439
t.plan(3)
440
const fastify = Fastify()
441
442
fastify.register(require('../index'), {
443
engine: {
444
squirrelly: Sqrl
445
}
446
})
447
448
fastify.ready(err => {
449
t.assert.ifError(err)
450
451
fastify.view(null, {}, err => {
452
t.assert.ok(err instanceof Error)
453
t.assert.strictEqual(err.message, 'Missing page')
454
fastify.close()
455
end()
456
})
457
})
458
})
459
460
test('reply.view with squirrelly engine and raw template', async t => {
461
t.plan(4)
462
const fastify = Fastify()
463
464
const data = { text: 'text' }
465
466
fastify.register(pointOfView, {
467
engine: {
468
squirrelly: Sqrl
469
}
470
})
471
472
fastify.get('/', (_req, reply) => {
473
reply.view({ raw: fs.readFileSync('./templates/index.squirrelly', 'utf8') }, data)
474
})
475
476
const address = await fastify.listen({ port: 0 })
477
478
const result = await fetch(address)
479
480
const responseContent = await result.text()
481
482
t.assert.strictEqual(result.status, 200)
483
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
484
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
485
486
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
487
488
await fastify.close()
489
})
490
491
test('reply.view with squirrelly engine and function template', async t => {
492
t.plan(4)
493
const fastify = Fastify()
494
495
const data = { text: 'text' }
496
497
fastify.register(pointOfView, {
498
engine: {
499
squirrelly: Sqrl
500
},
501
templates: 'templates'
502
})
503
504
fastify.get('/', (_req, reply) => {
505
reply.view((data) => `<p>${data.text}</p>`, data)
506
})
507
508
const address = await fastify.listen({ port: 0 })
509
510
const result = await fetch(address)
511
512
const responseContent = await result.text()
513
514
t.assert.strictEqual(result.status, 200)
515
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
516
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
517
518
t.assert.strictEqual('<p>text</p>', responseContent)
519
520
await fastify.close()
521
})
522
523
test('reply.view should return 500 if function return sync error', async t => {
524
t.plan(1)
525
const fastify = Fastify()
526
527
const data = { text: 'text' }
528
529
fastify.register(pointOfView, {
530
engine: {
531
squirrelly: Sqrl
532
},
533
root: path.join(__dirname, '../templates')
534
})
535
536
fastify.get('/', (_req, reply) => {
537
reply.view(() => { throw new Error('kaboom') }, data)
538
})
539
540
const address = await fastify.listen({ port: 0 })
541
542
const result = await fetch(address)
543
544
t.assert.strictEqual(result.status, 500)
545
546
await fastify.close()
547
})
548
549
test('reply.view should return 500 if function return async error', async t => {
550
t.plan(1)
551
const fastify = Fastify()
552
553
const data = { text: 'text' }
554
555
fastify.register(pointOfView, {
556
engine: {
557
squirrelly: Sqrl
558
},
559
root: path.join(__dirname, '../templates')
560
})
561
562
fastify.get('/', (_req, reply) => {
563
reply.view(() => Promise.reject(new Error('kaboom')), data)
564
})
565
566
const address = await fastify.listen({ port: 0 })
567
568
const result = await fetch(address)
569
570
t.assert.strictEqual(result.status, 500)
571
572
await fastify.close()
573
})
574
575
test('reply.view should return 500 if template not found', async t => {
576
t.plan(1)
577
const fastify = Fastify()
578
579
fastify.register(pointOfView, {
580
engine: {
581
squirrelly: Sqrl
582
},
583
templates: 'templates'
584
})
585
586
fastify.get('/', (_req, reply) => {
587
reply.view('non-existing.squirrelly', { text: 'text' })
588
})
589
590
const address = await fastify.listen({ port: 0 })
591
592
const result = await fetch(address)
593
594
t.assert.strictEqual(result.status, 500)
595
596
await fastify.close()
597
})
598
599
test('fastify.view with squirrelly engine', async t => {
600
t.plan(2)
601
const fastify = Fastify()
602
603
const data = { text: 'text' }
604
605
fastify.register(pointOfView, {
606
engine: {
607
squirrelly: Sqrl
608
},
609
templates: 'templates'
610
})
611
612
await fastify.ready()
613
614
const result = await fastify.view('index.squirrelly', data)
615
const expected = Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data)
616
617
t.assert.ok(typeof result === 'string')
618
t.assert.strictEqual(result, expected)
619
620
await fastify.close()
621
})
622
623
test('reply.viewAsync with squirrelly engine', async t => {
624
t.plan(4)
625
const fastify = Fastify()
626
627
const data = { text: 'text' }
628
629
fastify.register(pointOfView, {
630
engine: {
631
squirrelly: Sqrl
632
},
633
templates: 'templates'
634
})
635
636
fastify.get('/', async (_req, reply) => {
637
return reply.viewAsync('index.squirrelly', data)
638
})
639
640
const address = await fastify.listen({ port: 0 })
641
642
const result = await fetch(address)
643
const responseContent = await result.text()
644
645
t.assert.strictEqual(result.status, 200)
646
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
647
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
648
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
649
650
await fastify.close()
651
})
652
653
test('reply.view with squirrelly engine and root option', async t => {
654
t.plan(4)
655
const fastify = Fastify()
656
657
const data = { text: 'text' }
658
659
fastify.register(pointOfView, {
660
engine: {
661
squirrelly: Sqrl
662
},
663
root: path.join(__dirname, '../templates')
664
})
665
666
fastify.get('/', (_req, reply) => {
667
reply.view('index.squirrelly', data)
668
})
669
670
const address = await fastify.listen({ port: 0 })
671
672
const result = await fetch(address)
673
const responseContent = await result.text()
674
675
t.assert.strictEqual(result.status, 200)
676
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
677
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
678
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
679
680
await fastify.close()
681
})
682
683
test('reply.view with squirrelly engine and custom property name', async t => {
684
t.plan(4)
685
const fastify = Fastify()
686
687
const data = { text: 'text' }
688
689
fastify.register(pointOfView, {
690
engine: {
691
squirrelly: Sqrl
692
},
693
templates: 'templates',
694
propertyName: 'render'
695
})
696
697
fastify.get('/', (_req, reply) => {
698
reply.render('index.squirrelly', data)
699
})
700
701
const address = await fastify.listen({ port: 0 })
702
703
const result = await fetch(address)
704
const responseContent = await result.text()
705
706
t.assert.strictEqual(result.status, 200)
707
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
708
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
709
t.assert.strictEqual(Sqrl.render(fs.readFileSync('./templates/index.squirrelly', 'utf8'), data), responseContent)
710
711
await fastify.close()
712
})
713
714