Ghost/test/unit/helpers/match_spec.js
Hannah Wolfe 7069cc1221 Added initial match helper
refs: https://github.com/TryGhost/Team/issues/759

- wired up a matchHelper feature flag & used the labsEnabledHelper tool to gate the helper
- added a first version of the match helper, which is intended to replace the has helper
- this is an experimental helper and may or may not make it to GA
- match is a simple comparison helper, right now it does a very basic equals or not equals comparison
- much more functionality is needed to reach parity with has
2021-06-07 21:15:05 +01:00

128 lines
4.0 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const _ = require('lodash');
const helpers = require('../../../core/frontend/helpers');
const labs = require('../../../core/server/services/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);
});
});
});