From c8fa75b77bf3eea9dd50139594db52f7b9fc8dce Mon Sep 17 00:00:00 2001 From: KidsDontPlay Date: Wed, 9 Sep 2020 22:32:08 +0200 Subject: [PATCH] fix decorator render methods for nunjucks and marko (#177) -added tests --- index.js | 6 ++++-- test/test-marko.js | 32 ++++++++++++++++++++++++++++++++ test/test-nunjucks.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 00a0d1da..6fe17cc9 100644 --- a/index.js +++ b/index.js @@ -326,7 +326,8 @@ function fastifyView (fastify, opts, next) { if (options.useHtmlMinifier && (typeof options.useHtmlMinifier.minify === 'function')) { html = options.useHtmlMinifier.minify(html, options.htmlMinifierOptions || {}) } - this.header('Content-Type', 'text/html; charset=' + charset).send(html) + this.header('Content-Type', 'text/html; charset=' + charset) + this.send(html) }) } @@ -358,7 +359,8 @@ function fastifyView (fastify, opts, next) { if (options.useHtmlMinifier && (typeof options.useHtmlMinifier.minify === 'function')) { html = options.useHtmlMinifier.minify(html, options.htmlMinifierOptions || {}) } - that.header('Content-Type', 'text/html; charset=' + charset).send(html) + that.header('Content-Type', 'text/html; charset=' + charset) + that.send(html) } } } diff --git a/test/test-marko.js b/test/test-marko.js index 435b0ccf..604b2d4e 100644 --- a/test/test-marko.js +++ b/test/test-marko.js @@ -410,3 +410,35 @@ test('reply.view with marko engine, with stream and html-minify-stream', t => { }) }) }) + +test('fastify.view with marko engine', t => { + t.plan(6) + const fastify = Fastify() + const marko = require('marko') + const data = { text: 'text' } + + fastify.register(require('../index'), { + engine: { + marko: marko + } + }) + + fastify.ready(err => { + t.error(err) + + fastify.view('templates/index.marko', data, (err, compiled) => { + t.error(err) + t.strictEqual(marko.load('./templates/index.marko').renderToString(data), compiled) + + fastify.ready(err => { + t.error(err) + + fastify.view('templates/index.marko', data, (err, compiled) => { + t.error(err) + t.strictEqual(marko.load('./templates/index.marko').renderToString(data), compiled) + fastify.close() + }) + }) + }) + }) +}) diff --git a/test/test-nunjucks.js b/test/test-nunjucks.js index 5126520d..98ca0ac7 100644 --- a/test/test-nunjucks.js +++ b/test/test-nunjucks.js @@ -420,3 +420,35 @@ test('reply.view with nunjucks engine using onConfigure callback', t => { }) }) }) + +test('fastify.view with nunjucks engine', t => { + t.plan(6) + const fastify = Fastify() + const nunjucks = require('nunjucks') + const data = { text: 'text' } + + fastify.register(require('../index'), { + engine: { + nunjucks: nunjucks + } + }) + + fastify.ready(err => { + t.error(err) + + fastify.view('templates/index.njk', data, (err, compiled) => { + t.error(err) + t.strictEqual(nunjucks.render('./templates/index.njk', data), compiled) + + fastify.ready(err => { + t.error(err) + + fastify.view('templates/index.njk', data, (err, compiled) => { + t.error(err) + t.strictEqual(nunjucks.render('./templates/index.njk', data), compiled) + fastify.close() + }) + }) + }) + }) +})