From 0af2cb589ca0dff294a8ccac85f3d68ceda630b9 Mon Sep 17 00:00:00 2001 From: Jairo Panduro Date: Wed, 16 Sep 2020 18:55:20 +0300 Subject: [PATCH] feat: (strf-8630) fix template engine values --- server/plugins/renderer/responses/pencil-response.js | 10 +++++++++- .../renderer/responses/pencil-response.spec.js | 11 ++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/server/plugins/renderer/responses/pencil-response.js b/server/plugins/renderer/responses/pencil-response.js index 32bcfe25..9e035319 100644 --- a/server/plugins/renderer/responses/pencil-response.js +++ b/server/plugins/renderer/responses/pencil-response.js @@ -60,6 +60,14 @@ const getTemplatePath = (request, data) => { */ const isSupportedHandlebarsVersion = version => ['handlebars-v3', 'handlebars-v4'].includes(version); +/** + * Node.js projects are using variables in format: handlebars-v3, handlebars-v4; + * Storefront and db are using format: handlebars_v3, handlebars_v4; + * This function converts _v3 to -v3 + * + * @param {String} version + */ +const compatibilizeTemplateEngine = version => version.replace('_', '-'); /** * Output post-processing @@ -93,7 +101,7 @@ const makeDecorator = (request, context) => content => { module.exports = function (data, assembler) { this.respond = function (request, h) { - const templateEngine = data.context.template_engine || "handlebars-v3"; + const templateEngine = compatibilizeTemplateEngine(data.context.template_engine || "handlebars-v3"); if (!isSupportedHandlebarsVersion(templateEngine)) { throw new Error('Provided Handlebars version is not supported! Please use:handlebars-v3, handlebars-v4'); diff --git a/server/plugins/renderer/responses/pencil-response.spec.js b/server/plugins/renderer/responses/pencil-response.spec.js index 35806283..a41f86cb 100644 --- a/server/plugins/renderer/responses/pencil-response.spec.js +++ b/server/plugins/renderer/responses/pencil-response.spec.js @@ -58,7 +58,7 @@ describe('PencilResponse', () => { expect(h.response).toHaveBeenCalledTimes(1); }); - it('should default to handlebars_v3 when the template_engine doesn\'t exist', async () => { + it('should default to handlebars-v3 when the template_engine doesn\'t exist', async () => { delete data.context.template_engine; const pencilResponse = new PencilResponse(data, assembler); @@ -66,4 +66,13 @@ describe('PencilResponse', () => { expect(h.response).toHaveBeenCalledTimes(1); }); + + it('should make compatible handlbers_v3 variable', async () => { + data.context.template_engine = 'handlebars_v3'; + + const pencilResponse = new PencilResponse(data, assembler); + await pencilResponse.respond(request, h); + + expect(h.response).toHaveBeenCalledTimes(1); + }); });