Ghost/test/unit/helpers/match.test.js
Hannah Wolfe c29c118fcf
Moved labs utlity to shared
- This isn't really a "service" - it's a set of utilities for working with labs flags
- It's also required all over the place, and doesn't require anything that isn't shared
- Therefore, it should live in shared
2021-07-08 09:05:41 +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/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);
});
});
});