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

Region helper MERC-2432 #119

Closed
Closed
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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"modules": true
},
"env": {
"node": true,
"node": true
},
"globals": {

Expand Down
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: node_js
sudo: false
node_js:
- 4.4.7
- 4
- 6

script:
- npm install
- npm install -g gulp-cli
Expand Down
20 changes: 20 additions & 0 deletions helpers/region.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

function helper(paper) {
paper.handlebars.registerHelper('region', function (params) {
let regionId = params.hash.name;
let content = '';

if (!paper.renderingContext || !paper.renderingContext.regions) {
return '';
}

if (paper.renderingContext.regions[regionId]) {
content = `<div data-content-region="${regionId}">${paper.renderingContext.regions[regionId]}</div>`;
}

return new paper.handlebars.SafeString(content);
});
}

module.exports = helper;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"accept-language-parser": "^1.0.2",
"async": "^1.4.2",
"handlebars": "^3.0.1",
"handlebars": "^4.0.10",
"handlebars-helpers": "^0.8.0",
"lodash": "^3.6.0",
"messageformat": "^0.2.2",
Expand Down
59 changes: 59 additions & 0 deletions test/helpers/region.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const Code = require('code');
const Lab = require('lab');
const Paper = require('../../index');

const lab = exports.lab = Lab.script();
const before = lab.before;
const describe = lab.experiment;
const expect = Code.expect;
const it = lab.it;

function compile(paper, template, context) {
context = context || {};
return paper.loadTemplatesSync({
template: template
}).render('template', context);
}

describe('Region Helper', () => {
let context;
let paper;

before(done => {
context = {
regions: {
'banner-top': "hello world"
}
};

paper = new Paper();
paper.renderingContext = context;

done();
});

it('should return an empty string if no content service data (missing renderingContext) on page context', done => {
let noPaperContext = new Paper();
expect(compile(noPaperContext, '{{region name="banner-bottom"}}', context))
.to.be.equal('');

done();
});

it('should return an empty string if no matching region on context object', done => {
expect(compile(paper, '{{region name="banner-bottom"}}', context))
.to.be.equal('');

done();
});

it('should return Hello World', done => {
expect(compile(paper, '{{region name="banner-top"}}', context))
.to.be.equal('<div data-content-region="banner-top">hello world</div>');

done();
});

});