Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/test/ejs-with-snapshot.test.js
107 views
1
'use strict'
2
3
const { test } = require('node:test')
4
const Fastify = require('fastify')
5
const path = require('node:path')
6
const ejs = require('ejs')
7
const templatesFolder = 'templates'
8
const options = {
9
filename: path.resolve(templatesFolder),
10
views: [path.join(__dirname, '..')]
11
}
12
13
test('reply.view with ejs engine, template folder specified, include files (ejs and html) used in template, includeViewExtension property as true', async t => {
14
t.plan(6)
15
const fastify = Fastify()
16
17
const data = { text: 'text' }
18
19
fastify.register(require('../index'), {
20
engine: {
21
ejs
22
},
23
includeViewExtension: true,
24
templates: templatesFolder,
25
options
26
})
27
28
fastify.get('/', (_req, reply) => {
29
reply.type('text/html; charset=utf-8').view('index-linking-other-pages', data) // sample for specifying with type
30
})
31
32
await fastify.listen({ port: 0 })
33
34
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
35
36
const responseContent = await result.text()
37
38
t.assert.strictEqual(result.status, 200)
39
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
40
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
41
42
let content = null
43
44
await new Promise(resolve => {
45
ejs.renderFile(path.join(templatesFolder, 'index-linking-other-pages.ejs'), data, options, function (err, str) {
46
content = str
47
t.assert.ifError(err)
48
t.assert.strictEqual(content.length, responseContent.length)
49
resolve()
50
})
51
})
52
const expectedContent = '<!DOCTYPE html><html lang="en"> <head></head> <body> <p>text</p> <br/> <div> <p>Other EJS pages with includes:</p> <ul> <li>Normal page, <a href="/include-test">here</a></li> <li>One include not exist, <a href="/include-one-include-missing-test">here</a> (to raise errors) </li> <li>One attribute not exist, <a href="/include-one-attribute-missing-test">here</a> (to raise errors) </li> </ul> <div> </body></html>'
53
t.assert.strictEqual(content.replace(/\r?\n/g, ''), expectedContent) // normalize new lines for cross-platform
54
55
await fastify.close()
56
})
57
58
test('reply.view with ejs engine, templates with folder specified, include files and attributes; home', async t => {
59
t.plan(6)
60
const fastify = Fastify()
61
62
const data = { text: 'Hello from EJS Templates' }
63
64
fastify.register(require('../index'), {
65
engine: {
66
ejs
67
},
68
includeViewExtension: true,
69
templates: templatesFolder,
70
options
71
})
72
73
fastify.get('/', (_req, reply) => {
74
reply.type('text/html; charset=utf-8').view('index', data)
75
})
76
77
await fastify.listen({ port: 0 })
78
79
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port)
80
81
const responseContent = await result.text()
82
83
t.assert.strictEqual(result.status, 200)
84
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
85
t.assert.strictEqual(result.headers.get('content-type'), 'text/html; charset=utf-8')
86
87
let content = null
88
await new Promise(resolve => {
89
ejs.renderFile(path.join(templatesFolder, 'index.ejs'), data, options, function (err, str) {
90
content = str
91
t.assert.ifError(err)
92
t.assert.strictEqual(content.length, responseContent.length)
93
resolve()
94
})
95
})
96
const expectedContent = '<!DOCTYPE html><html lang="en"> <head></head> <body> <p>Hello from EJS Templates</p> <br/> </body></html>'
97
t.assert.strictEqual(content.replace(/\r?\n/g, ''), expectedContent) // normalize new lines for cross-platform
98
99
await fastify.close()
100
})
101
102
test('reply.view with ejs engine, templates with folder specified, include files and attributes; page with includes', async t => {
103
t.plan(6)
104
const fastify = Fastify()
105
106
const data = { text: 'Hello from EJS Templates' }
107
108
fastify.register(require('../index'), {
109
engine: {
110
ejs
111
},
112
includeViewExtension: true,
113
templates: templatesFolder,
114
options
115
})
116
117
fastify.get('/include-test', (_req, reply) => {
118
reply.type('text/html; charset=utf-8').view('index-with-includes', data)
119
})
120
121
await fastify.listen({ port: 0 })
122
123
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/include-test')
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
131
let content = null
132
await new Promise(resolve => {
133
ejs.renderFile(path.join(templatesFolder, 'index-with-includes.ejs'), data, options, function (err, str) {
134
content = str
135
t.assert.ifError(err)
136
t.assert.strictEqual(content.length, responseContent.length)
137
resolve()
138
})
139
})
140
141
const expectedContent = '<!DOCTYPE html><html lang="en"> <head></head> <body> <header> Sample header (ejs) </header> <p>Hello from EJS Templates</p> <footer> Sample footer (html) - Back to <a href="/">Home</a> </footer> </body></html>'
142
t.assert.strictEqual(content.replace(/\r?\n/g, ''), expectedContent) // normalize new lines for cross-platform
143
144
await fastify.close()
145
})
146
147
test('reply.view with ejs engine, templates with folder specified, include files and attributes; page with one include missing', async t => {
148
t.plan(6)
149
const fastify = Fastify()
150
151
const data = { text: 'Hello from EJS Templates' }
152
153
fastify.register(require('../index'), {
154
engine: {
155
ejs
156
},
157
includeViewExtension: true,
158
templates: templatesFolder,
159
options
160
})
161
162
fastify.get('/include-one-include-missing-test', (_req, reply) => {
163
reply.type('text/html; charset=utf-8').view('index-with-includes-one-missing', data)
164
})
165
166
await fastify.listen({ port: 0 })
167
168
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/include-one-include-missing-test')
169
170
const responseContent = await result.text()
171
172
t.assert.strictEqual(result.status, 500)
173
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
174
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
175
176
let content = null
177
await new Promise(resolve => {
178
ejs.renderFile(path.join(templatesFolder, 'index-with-includes-one-missing.ejs'), data, options, function (err, str) {
179
content = str
180
t.assert.ok(err instanceof Error)
181
t.assert.strictEqual(content, undefined)
182
resolve()
183
})
184
})
185
t.assert.strictEqual(content, undefined)
186
187
await fastify.close()
188
})
189
190
test('reply.view with ejs engine, templates with folder specified, include files and attributes; page with one attribute missing', async t => {
191
t.plan(6)
192
const fastify = Fastify()
193
194
const data = { text: 'Hello from EJS Templates' }
195
196
fastify.register(require('../index'), {
197
engine: {
198
ejs
199
},
200
includeViewExtension: true,
201
templates: templatesFolder,
202
options
203
})
204
205
fastify.get('/include-one-attribute-missing-test', (_req, reply) => {
206
reply.type('text/html; charset=utf-8').view('index-with-includes-and-attribute-missing', data)
207
})
208
209
await fastify.listen({ port: 0 })
210
211
const result = await fetch('http://127.0.0.1:' + fastify.server.address().port + '/include-one-attribute-missing-test')
212
213
const responseContent = await result.text()
214
215
t.assert.strictEqual(result.status, 500)
216
t.assert.strictEqual(result.headers.get('content-length'), '' + responseContent.length)
217
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
218
219
let content = null
220
await new Promise(resolve => {
221
ejs.renderFile(path.join(templatesFolder, 'index-with-includes-and-attribute-missing.ejs'), data, options, function (err, str) {
222
content = str
223
t.assert.ok(err instanceof Error)
224
t.assert.strictEqual(content, undefined)
225
resolve()
226
})
227
})
228
229
t.assert.strictEqual(content, undefined)
230
231
await fastify.close()
232
})
233
234