Skip to content

Commit

Permalink
Merge branch 'master' into mweber-master
Browse files Browse the repository at this point in the history
  • Loading branch information
mweberxyz committed Feb 27, 2024
2 parents d478b5a + 77fd561 commit 4b7c5dc
Showing 1 changed file with 0 additions and 133 deletions.
133 changes: 0 additions & 133 deletions test/test-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,139 +475,6 @@ test('reply.view with handlebars engine and defaultContext', t => {
})
})

test('reply.view with ejs engine and includeViewExtension property as true', t => {
t.plan(6)
const fastify = Fastify()
const ejs = require('ejs')
const data = { text: 'text' }

fastify.register(require('../index'), {
engine: {
ejs
},
includeViewExtension: true
})

fastify.get('/', (req, reply) => {
reply.view('./templates/index', data)
})

fastify.listen({ port: 0 }, err => {
t.error(err)

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], '' + body.length)
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
t.equal(ejs.render(fs.readFileSync('./templates/index.ejs', 'utf8'), data), body.toString())
fastify.close()
})
})
})

test('reply.view with ejs engine, template folder specified, include files (ejs and html) used in template, includeViewExtension property as true', t => {
t.plan(7)
const fastify = Fastify()
const ejs = require('ejs')
const resolve = require('node:path').resolve
const templatesFolder = 'templates'
const options = {
filename: resolve(templatesFolder), // needed for include files to be resolved in include directive ...
views: [__dirname] // must be put to make tests (with include files) working ...
}
const data = { text: 'text' }

fastify.register(require('../index'), {
engine: {
ejs
},
includeViewExtension: true,
templates: templatesFolder,
options
})

fastify.get('/', (req, reply) => {
reply.type('text/html; charset=utf-8').view('index-linking-other-pages', data) // sample for specifying with type
// reply.view('index-with-includes', data)
})

fastify.listen({ port: 0 }, err => {
t.error(err)

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
t.equal(response.headers['content-length'], '' + body.length)

let content = null
ejs.renderFile(templatesFolder + '/index-linking-other-pages.ejs', data, options, function (err, str) {
content = str
t.error(err)
t.equal(content.length, body.length)
})

fastify.close()
})
})
})

test('reply.view with ejs engine, templates with folder specified, include files and attributes; home', t => {
t.plan(7)
const fastify = Fastify()
const ejs = require('ejs')
const resolve = require('node:path').resolve
const templatesFolder = 'templates'
const options = {
filename: resolve(templatesFolder),
views: [__dirname]
}
const data = { text: 'Hello from EJS Templates' }

fastify.register(require('../index'), {
engine: {
ejs
},
includeViewExtension: true,
templates: templatesFolder,
options
})

fastify.get('/', (req, reply) => {
reply.type('text/html; charset=utf-8').view('index', data)
})

fastify.listen({ port: 0 }, err => {
t.error(err)

sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-type'], 'text/html; charset=utf-8')
t.equal(response.headers['content-length'], '' + body.length)

let content = null
ejs.renderFile(templatesFolder + '/index.ejs', data, options, function (err, str) {
content = str
t.error(err)
t.equal(content.length, body.length)
})

fastify.close()
})
})
})

test('reply.view with handlebars engine and includeViewExtension property as true', t => {
t.plan(6)
const fastify = Fastify()
Expand Down

0 comments on commit 4b7c5dc

Please sign in to comment.