Skip to content

Commit

Permalink
feat: (strf-8630) fix template engine values
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc committed Sep 21, 2020
1 parent fc62b19 commit 0af2cb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 9 additions & 1 deletion server/plugins/renderer/responses/pencil-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down
11 changes: 10 additions & 1 deletion server/plugins/renderer/responses/pencil-response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ 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);
await pencilResponse.respond(request, h);

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);
});
});

0 comments on commit 0af2cb5

Please sign in to comment.