0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/test/unit/helpers/match.test.js

128 lines
4 KiB
JavaScript
Raw Normal View History

const should = require('should');
const sinon = require('sinon');
const _ = require('lodash');
const helpers = require('../../../core/frontend/helpers');
const labs = require('../../../core/shared/labs');
const handlebars = require('../../../core/frontend/services/theme-engine/engine').handlebars;
describe('Match helper', function () {
before(function () {
handlebars.registerHelper('match', helpers.match);
});
afterEach(function () {
sinon.restore();
});
beforeEach(function () {
sinon.stub(labs, 'isSet').returns(true);
});
function shouldCompileToExpected(templateString, hash, expected) {
const template = handlebars.compile(templateString);
const result = template(hash);
result.should.eql(expected);
}
describe('{{#match}} (block)', function () {
before(function () {
handlebars.registerHelper('foreach', helpers.foreach);
});
it('{{#match title "=" "Hello World"}} works when true', function () {
const templateString = '{{#match title "=" "Hello World"}}true{{else}}false{{/match}}';
const hash = {
title: 'Hello World'
};
const expected = 'true';
shouldCompileToExpected(templateString, hash, expected);
});
it('{{#match title "=" "Hello World"}} works when false', function () {
const templateString = '{{#match title "=" "Hello World"}}true{{else}}false{{/match}}';
const hash = {
title: 'Goodbye World'
};
const expected = 'false';
shouldCompileToExpected(templateString, hash, expected);
});
it('{{#match title "!=" "Hello World"}} works when true', function () {
const templateString = '{{#match title "!=" "Hello World"}}true{{else}}false{{/match}}';
const hash = {
title: 'Goodbye World'
};
const expected = 'true';
shouldCompileToExpected(templateString, hash, expected);
});
it('{{#match title "!=" "Hello World"}} works when false', function () {
const templateString = '{{#match title "!=" "Hello World"}}true{{else}}false{{/match}}';
const hash = {
title: 'Hello World'
};
const expected = 'false';
shouldCompileToExpected(templateString, hash, expected);
});
});
describe('{{match}} (inline)', function () {
before(function () {
handlebars.registerHelper('foreach', helpers.foreach);
});
it('{{match title "=" "Hello World"}} works when true', function () {
const templateString = '{{match title "=" "Hello World"}}';
const hash = {
title: 'Hello World'
};
const expected = 'true';
shouldCompileToExpected(templateString, hash, expected);
});
it('{{match title "=" "Hello World"}} works when false', function () {
const templateString = '{{match title "=" "Hello World"}}';
const hash = {
title: 'Goodbye World'
};
const expected = 'false';
shouldCompileToExpected(templateString, hash, expected);
});
it('{{match title "!=" "Hello World"}} works when true', function () {
const templateString = '{{match title "!=" "Hello World"}}';
const hash = {
title: 'Goodbye World'
};
const expected = 'true';
shouldCompileToExpected(templateString, hash, expected);
});
it('{{match title "!=" "Hello World"}} works when false', function () {
const templateString = '{{match title "!=" "Hello World"}}';
const hash = {
title: 'Hello World'
};
const expected = 'false';
shouldCompileToExpected(templateString, hash, expected);
});
});
});