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

Specifically fail when encoding JSON for undefined #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions helpers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const factory = () => {
return function(data) {
if(typeof data === 'undefined') {
throw new Error("Handlebars Helper 'json' does not allow value of 'undefined'");
}
return JSON.stringify(data);
};
};
Expand Down
22 changes: 20 additions & 2 deletions spec/helpers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ const Lab = require('lab'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it,
testRunner = require('../spec-helpers').testRunner;
expect = require('code').expect,
testRunner = require('../spec-helpers').testRunner,
buildRenderer = require('../spec-helpers').buildRenderer,
RenderError = require('../../index').errors.RenderError;

describe('json helper', function() {
const context = {
object: { a: 1, b: "hello" }
object: { a: 1, b: "hello" },
undef: undefined
};

const runTestCases = testRunner({context});
Expand All @@ -19,4 +23,18 @@ describe('json helper', function() {
},
], done);
});

it('should fail when provided undefined', function(done) {
const render = buildRenderer();

render.renderString('{{{json undef}}}', context).then((result) => {
return Promise.resolve(null);
}, (reason) => {
expect(reason).to.be.an.instanceof(RenderError);
return Promise.resolve(reason.message);
}).then((reason) => {
expect(reason).to.equal("Handlebars Helper 'json' does not allow value of 'undefined'");
done();
});
});
});