Ghost/ghost/tpl/test/tpl.test.js
Hannah Wolfe 6d3a629690 Added minimal tpl() template helper
- This mini helper wraps lodash template and supports `{...}` as the delimiters
- It's designed to use and support the exact same patterns we already have in our en.json strings
E.g. The {flagName} flag must be enabled in labs if you wish to use the \\{\\{{helperName}\\}\\} helper.
- This allows us to get rid of our old, broken i18n helper and still keep some of the smart messaging we have setup
- It also keeps the refactor surface area minimal
2021-06-09 10:04:49 +01:00

75 lines
2.3 KiB
JavaScript

// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
const tpl = require('../');
describe('tpl', function () {
it('Can handle a plain string', function () {
const string = 'My template';
const result = tpl(string);
result.should.eql('My template');
});
it('Can handle a string with data', function () {
const string = 'Go visit {url}';
const data = {url: 'https://example.com'};
let result = tpl(string, data);
result.should.eql('Go visit https://example.com');
});
it('Can handle mixing with handlebars-related messages', function () {
const string = '{{#get}} helper took {totalMs}ms to complete';
const data = {
totalMs: '500'
};
let result = tpl(string, data);
result.should.eql('{{#get}} helper took 500ms to complete');
});
it('Can handle escaped left braces', function () {
const string = 'The \\{\\{{helperName}}} helper is not available.';
const data = {
helperName: 'get',
totalMs: '500'
};
let result = tpl(string, data);
result.should.eql('The {{get}} helper is not available.');
});
it('Can handle escaped right braces as well', function () {
const string = 'The \\{\\{{helperName}\\}\\} helper is not available.';
const data = {
helperName: 'get',
totalMs: '500'
};
let result = tpl(string, data);
result.should.eql('The {{get}} helper is not available.');
});
it('has a simple bare minimum escaping needed', function () {
const string = 'The {\\{{helperName}}} helper is not available.';
const data = {
helperName: 'get',
totalMs: '500'
};
let result = tpl(string, data);
result.should.eql('The {{get}} helper is not available.');
});
it('Returns a sensible error if data is missing', function () {
const string = '{helperName} helper took {totalMs}ms to complete';
const data = {
totalMs: '500'
};
let resultFn = () => {
tpl(string, data);
};
resultFn.should.throw('helperName is not defined');
});
});