2020-01-27 14:41:12 +03:00
|
|
|
const should = require('should');
|
2021-10-06 12:52:46 +03:00
|
|
|
const price = require('../../../../core/frontend/helpers/price');
|
|
|
|
const handlebars = require('../../../../core/frontend/services/theme-engine/engine').handlebars;
|
2020-01-27 14:41:12 +03:00
|
|
|
|
|
|
|
function compile(templateString) {
|
|
|
|
const template = handlebars.compile(templateString);
|
|
|
|
template.with = (locals = {}, globals) => {
|
|
|
|
return template(locals, globals);
|
|
|
|
};
|
|
|
|
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('{{price}} helper', function () {
|
|
|
|
before(function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
handlebars.registerHelper('price', price);
|
2020-01-27 14:41:12 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
2021-02-23 13:55:28 +03:00
|
|
|
|
|
|
|
it('will format with plan object', function () {
|
|
|
|
const plan = {
|
|
|
|
nickname: 'Monthly',
|
|
|
|
amount: 500,
|
|
|
|
interval: 'month',
|
|
|
|
currency: 'USD',
|
|
|
|
currency_symbol: '$'
|
|
|
|
};
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, plan, {});
|
2021-02-23 13:55:28 +03:00
|
|
|
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: '$'
|
|
|
|
};
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, plan, {hash: {numberFormat: 'long'}});
|
2021-02-23 13:55:28 +03:00
|
|
|
rendered.should.be.equal('$5.00');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will format symbol if only currency - USD', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, {hash: {currency: 'USD'}});
|
2021-02-23 13:55:28 +03:00
|
|
|
rendered.should.be.equal('$');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will format symbol if only currency - EUR', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, {hash: {currency: 'EUR'}});
|
2021-02-23 13:55:28 +03:00
|
|
|
rendered.should.be.equal('€');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will format with amount and currency', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, 500, {hash: {currency: 'USD'}});
|
2021-02-23 13:55:28 +03:00
|
|
|
rendered.should.be.equal('$5');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will format with long number format', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, 500, {hash: {currency: 'USD', numberFormat: 'long'}});
|
2021-02-23 13:55:28 +03:00
|
|
|
rendered.should.be.equal('$5.00');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will format with short number format with decimal value', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, 505, {hash: {currency: 'EUR', numberFormat: 'short'}});
|
2021-02-23 13:55:28 +03:00
|
|
|
rendered.should.be.equal('€5.05');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will format with short number format without decimal value', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, 500, {hash: {currency: 'EUR', numberFormat: 'short'}});
|
2021-02-23 13:55:28 +03:00
|
|
|
rendered.should.be.equal('€5');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('will format with name currency format', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = price.call({}, 500, {hash: {currency: 'USD', currencyFormat: 'name'}});
|
2021-02-23 13:55:28 +03:00
|
|
|
rendered.should.be.equal('5 US dollars');
|
|
|
|
});
|
2020-01-27 14:41:12 +03:00
|
|
|
});
|