0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Added shouldCompileToError test util

refs: a8b1676734

- Extended the newly created handlebars test utils with a shouldCompileToError method
- Updated the price helper tests tp use shouldCompileToExpected and shouldCompileToError
- This allows us to test our handlebars helpers in a much more conisstent way
This commit is contained in:
Hannah Wolfe 2022-10-30 16:55:53 +00:00
parent a24eb06179
commit 25d5839e96
No known key found for this signature in database
GPG key ID: AB586C3B5AE5C037
2 changed files with 29 additions and 26 deletions

View file

@ -1,46 +1,40 @@
const should = require('should'); const should = require('should');
const price = require('../../../../core/frontend/helpers/price'); const price = require('../../../../core/frontend/helpers/price');
const handlebars = require('../../../../core/frontend/services/theme-engine/engine').handlebars;
function compile(templateString) { const {registerHelper, shouldCompileToError, shouldCompileToExpected} = require('./utils/handlebars');
const template = handlebars.compile(templateString);
template.with = (locals = {}, globals) => {
return template(locals, globals);
};
return template;
}
describe('{{price}} helper', function () { describe('{{price}} helper', function () {
before(function () { before(function () {
handlebars.registerHelper('price', price); registerHelper('price');
}); });
it('throws an error for no provided parameters', function () { it('throws an error for no provided parameters', function () {
(function compileWith() { const templateString = '{{price}}';
compile('{{price}}')
.with({}); shouldCompileToError(templateString, {}, {
}).should.throw(); name: 'IncorrectUsageError'
});
}); });
it('throws an error for undefined parameter', function () { it('throws an error for undefined parameter', function () {
(function compileWith() { const templateString = '{{price @dont.exist}}';
compile('{{price @dont.exist}}')
.with({}); shouldCompileToError(templateString, {}, {
}).should.throw(); name: 'IncorrectUsageError'
});
}); });
it('throws if argument is not a number', function () { it('throws if argument is not a number', function () {
(function compileWith() { const templateString = '{{price "not_a_number"}}';
compile('{{price "not_a_number"}}') shouldCompileToError(templateString, {}, {
.with({}); name: 'IncorrectUsageError'
}).should.throw(); });
}); });
it('will format decimal adjusted amount', function () { it('will format decimal adjusted amount', function () {
compile('{{price 2000}}') const templateString = '{{price 2000}}';
.with({})
.should.equal('20'); shouldCompileToExpected(templateString, {}, '20');
}); });
it('will format with plan object', function () { it('will format with plan object', function () {

View file

@ -1,10 +1,19 @@
const assert = require('assert');
const handlebars = require('../../../../../core/frontend/services/theme-engine/engine').handlebars; const handlebars = require('../../../../../core/frontend/services/theme-engine/engine').handlebars;
module.exports.shouldCompileToExpected = (templateString, hash, expected) => { module.exports.shouldCompileToExpected = (templateString, hash, expected) => {
const template = handlebars.compile(templateString); const template = handlebars.compile(templateString);
const result = template(hash); const result = template(hash);
result.should.eql(expected); assert.equal(result, expected);
};
module.exports.shouldCompileToError = (templateString, hash, error) => {
const template = handlebars.compile(templateString);
assert.throws(() => {
return template(hash);
}, error);
}; };
module.exports.registerHelper = (name) => { module.exports.registerHelper = (name) => {