Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow no data to be passed #53

Merged
merged 2 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ function fastifyView (fastify, opts, next) {
}

function view (page, data) {
if (!page || !data) {
this.send(new Error('Missing data'))
if (!page) {
this.send(new Error('Missing page'))
return
}

Expand Down
8 changes: 8 additions & 0 deletions templates/index-with-no-data.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<p>No data</p>
<br/>
</body>
</html>
50 changes: 49 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('reply.view exist', t => {
})
})

test('reply.view should return 500 if some data is missing', t => {
test('reply.view should return 500 if page is missing', t => {
t.plan(3)
const fastify = Fastify()

Expand Down Expand Up @@ -629,6 +629,54 @@ test('reply.view with ejs engine, templates with folder specified, include files
})
})

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

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

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

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

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

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

fastify.close()
})
})
})

test('reply.view with ejs engine, templates with folder specified, include files and attributes; page with includes', t => {
t.plan(7)
const fastify = Fastify()
Expand Down