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