Ghost/test/unit/helpers/price.test.js
Hannah Wolfe f08a55c21f
Renamed tests to .test.js & updated commands
refs: https://github.com/TryGhost/Team/issues/856
refs: https://github.com/TryGhost/Team/issues/756

- The .test.js extension is better than _spec.js as it's more obvious that it's an extension
- It also meaans we can use the --extension parameter in mocha, which should result in a better default behaviour for `yarn test`
- It also highlights that some of our tests were named incorrectly and were not (and still will not be) run (see https://github.com/TryGhost/Team/issues/856)
- Note: even with this change, `yarn test` is throwing errors, I believe because of this issue https://github.com/TryGhost/Team/issues/756
2021-07-06 20:45:01 +01:00

105 lines
3.5 KiB
JavaScript

const should = require('should');
const helpers = require('../../../core/frontend/helpers');
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;
}
describe('{{price}} helper', function () {
before(function () {
handlebars.registerHelper('price', helpers.price);
});
it('throws an error for no provided parameters', function () {
(function compileWith() {
compile('{{price}}')
.with({});
}).should.throw();
});
it('throws an error for undefined parameter', function () {
(function compileWith() {
compile('{{price @dont.exist}}')
.with({});
}).should.throw();
});
it('throws if argument is not a number', function () {
(function compileWith() {
compile('{{price "not_a_number"}}')
.with({});
}).should.throw();
});
it('will format decimal adjusted amount', function () {
compile('{{price 2000}}')
.with({})
.should.equal('20');
});
it('will format with plan object', function () {
const plan = {
nickname: 'Monthly',
amount: 500,
interval: 'month',
currency: 'USD',
currency_symbol: '$'
};
const rendered = helpers.price.call({}, plan, {});
rendered.should.be.equal('$5');
});
it('will format with plan object with number format', function () {
const plan = {
nickname: 'Monthly',
amount: 500,
interval: 'month',
currency: 'USD',
currency_symbol: '$'
};
const rendered = helpers.price.call({}, plan, {hash: {numberFormat: 'long'}});
rendered.should.be.equal('$5.00');
});
it('will format symbol if only currency - USD', function () {
const rendered = helpers.price.call({}, {hash: {currency: 'USD'}});
rendered.should.be.equal('$');
});
it('will format symbol if only currency - EUR', function () {
const rendered = helpers.price.call({}, {hash: {currency: 'EUR'}});
rendered.should.be.equal('€');
});
it('will format with amount and currency', function () {
const rendered = helpers.price.call({}, 500, {hash: {currency: 'USD'}});
rendered.should.be.equal('$5');
});
it('will format with long number format', function () {
const rendered = helpers.price.call({}, 500, {hash: {currency: 'USD', numberFormat: 'long'}});
rendered.should.be.equal('$5.00');
});
it('will format with short number format with decimal value', function () {
const rendered = helpers.price.call({}, 505, {hash: {currency: 'EUR', numberFormat: 'short'}});
rendered.should.be.equal('€5.05');
});
it('will format with short number format without decimal value', function () {
const rendered = helpers.price.call({}, 500, {hash: {currency: 'EUR', numberFormat: 'short'}});
rendered.should.be.equal('€5');
});
it('will format with name currency format', function () {
const rendered = helpers.price.call({}, 500, {hash: {currency: 'USD', currencyFormat: 'name'}});
rendered.should.be.equal('5 US dollars');
});
});