Ghost/test/unit/helpers/content_spec.js
Rish 1272968848 Updated {{content}} helper partial template name
no issue
refs e3a0bb535f

Previously, {{content}} helper was updated use default or custom template to show a CTA when trying to use helper without access to content.

While the change was expected to not affect any existing themes as `{{content}}` helper is not supposed to be used on member sites without `access` check, we found quite a few themes use a pattern of using `content.hbs` with `{{content}}` helper inside them as default, which causes infinite loop in how content helper works in certain cases.

Whilst this followed the pattern of the other helpers using a partial with the same name, there are 2 key differences:

- this partial template pattern is being introduced way after the inception of themes with a fairly generic name “content”
- this partial template isn’t used to render the helper all the time - just the CTA part under certain circumstances.

This change updates the template name to `content-cta.hbs` to which makes it less likely to clash, and makes more sense as to what it is.
2021-02-09 13:59:52 +05:30

132 lines
3.8 KiB
JavaScript

const should = require('should');
const hbs = require('../../../core/frontend/services/themes/engine');
const configUtils = require('../../utils/configUtils');
const path = require('path');
const runHelper = data => helpers.navigation.call({}, data);
// Stuff we are testing
const helpers = require('../../../core/frontend/helpers');
describe('{{content}} helper', function () {
before(function (done) {
hbs.express4({partialsDir: [configUtils.config.get('paths').helperTemplates]});
hbs.cachePartials(function () {
done();
});
});
it('renders empty string when null', function () {
const html = null;
const rendered = helpers.content.call({html: html});
should.exist(rendered);
rendered.string.should.equal('');
});
it('can render content', function () {
const html = 'Hello World';
const rendered = helpers.content.call({html: html});
should.exist(rendered);
rendered.string.should.equal(html);
});
it('can truncate html by word', function () {
const html = '<p>Hello <strong>World! It\'s me!</strong></p>';
const rendered = (
helpers.content
.call(
{html: html},
{hash: {words: 2}}
)
);
should.exist(rendered);
rendered.string.should.equal('<p>Hello <strong>World!</strong></p>');
});
it('can truncate html to 0 words', function () {
const html = '<p>Hello <strong>World! It\'s me!</strong></p>';
const rendered = (
helpers.content
.call(
{html: html},
{hash: {words: '0'}}
)
);
should.exist(rendered);
rendered.string.should.equal('');
});
it('can truncate html by character', function () {
const html = '<p>Hello <strong>World! It\'s me!</strong></p>';
const rendered = (
helpers.content
.call(
{html: html},
{hash: {characters: 8}}
)
);
should.exist(rendered);
rendered.string.should.equal('<p>Hello <strong>Wo</strong></p>');
});
});
describe('{{content}} helper with no access', function () {
let optionsData;
before(function (done) {
hbs.express4({partialsDir: [configUtils.config.get('paths').helperTemplates]});
hbs.cachePartials(function () {
done();
});
});
beforeEach(function () {
optionsData = {
data: {
site: {
accent_color: '#abcdef'
}
}
};
});
it('can render default template', function () {
const html = 'Hello World';
const rendered = helpers.content.call({html: html, access: false}, optionsData);
rendered.string.should.containEql('gh-post-upgrade-cta');
rendered.string.should.containEql('gh-post-upgrade-cta-content');
rendered.string.should.containEql('"background-color: #abcdef"');
should.exist(rendered);
});
});
describe('{{content}} helper with no access', function () {
let optionsData;
before(function (done) {
hbs.express4({partialsDir: [path.resolve(__dirname, './test_tpl')]});
hbs.cachePartials(function () {
done();
});
});
it('can render custom template', function () {
const html = 'Hello World';
const rendered = helpers.content.call({html: html, access: false}, optionsData);
rendered.string.should.not.containEql('gh-post-upgrade-cta');
rendered.string.should.containEql('custom-post-upgrade-cta');
rendered.string.should.containEql('custom-post-upgrade-cta-content');
should.exist(rendered);
});
});