mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 10:21:36 +03:00
d2b1e0d4b7
refs #5162 - allow pagination and navigation partial helpers to have attributes passed through to them - e.g. {{navigation header=true}} -> {{#if header}} will now work - allows styling navigation to be done differently for different sections of the page - properly create a data frame, and pass through "this" context - means {{navigation header=true}} is the same as {{> navigation header=true navigation=@site.navigation}} - our partial helpers, have the same behaviour exactly as if the partial was called directly - this is additive, and improves behaviour
241 lines
8.1 KiB
JavaScript
241 lines
8.1 KiB
JavaScript
var should = require('should'),
|
|
hbs = require('../../../server/services/themes/engine'),
|
|
|
|
configUtils = require('../../utils/configUtils'),
|
|
path = require('path'),
|
|
|
|
helpers = require('../../../server/helpers');
|
|
|
|
describe('{{navigation}} helper', function () {
|
|
var runHelper = function (data) {
|
|
return function () {
|
|
helpers.navigation(data);
|
|
};
|
|
},
|
|
optionsData;
|
|
|
|
before(function (done) {
|
|
hbs.express4({
|
|
partialsDir: [configUtils.config.get('paths').helperTemplates]
|
|
});
|
|
|
|
hbs.cachePartials(function () {
|
|
done();
|
|
});
|
|
|
|
// The navigation partial expects this helper
|
|
// @TODO: change to register with Ghost's own registration tools
|
|
hbs.registerHelper('url', helpers.url);
|
|
});
|
|
|
|
beforeEach(function () {
|
|
optionsData = {
|
|
data: {
|
|
blog: {
|
|
navigation: []
|
|
},
|
|
root: {
|
|
relativeUrl: ''
|
|
}
|
|
}
|
|
};
|
|
});
|
|
|
|
it('should throw errors on invalid data', function () {
|
|
// Test 1: navigation = string
|
|
optionsData.data.blog.navigation = 'not an object';
|
|
runHelper(optionsData).should.throwError('navigation data is not an object or is a function');
|
|
|
|
// Test 2: navigation = function
|
|
optionsData.data.blog.navigation = function () {
|
|
};
|
|
runHelper(optionsData).should.throwError('navigation data is not an object or is a function');
|
|
|
|
// Test 3: invalid label
|
|
optionsData.data.blog.navigation = [{label: 1, url: 'bar'}];
|
|
runHelper(optionsData).should.throwError('Invalid value, Url and Label must be strings');
|
|
|
|
// Test 4: invalid url
|
|
optionsData.data.blog.navigation = [{label: 'foo', url: 1}];
|
|
runHelper(optionsData).should.throwError('Invalid value, Url and Label must be strings');
|
|
});
|
|
|
|
it('can render empty nav', function () {
|
|
var rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.be.equal('');
|
|
});
|
|
|
|
it('can handle relativeUrl not being set (e.g. for images/assets)', function () {
|
|
var singleItem = {label: 'Foo', url: '/foo'},
|
|
rendered;
|
|
delete optionsData.data.root.relativeUrl;
|
|
|
|
optionsData.data.blog.navigation = [singleItem];
|
|
rendered = helpers.navigation(optionsData);
|
|
rendered.string.should.containEql('li');
|
|
rendered.string.should.containEql('nav-foo');
|
|
rendered.string.should.containEql('/foo');
|
|
});
|
|
|
|
it('can render one item', function () {
|
|
var singleItem = {label: 'Foo', url: '/foo'},
|
|
testUrl = 'href="' + configUtils.config.get('url') + '/foo"',
|
|
rendered;
|
|
|
|
optionsData.data.blog.navigation = [singleItem];
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.containEql('li');
|
|
rendered.string.should.containEql('nav-foo');
|
|
rendered.string.should.containEql(testUrl);
|
|
});
|
|
|
|
it('can render multiple items', function () {
|
|
var firstItem = {label: 'Foo', url: '/foo'},
|
|
secondItem = {label: 'Bar Baz Qux', url: '/qux'},
|
|
testUrl = 'href="' + configUtils.config.get('url') + '/foo"',
|
|
testUrl2 = 'href="' + configUtils.config.get('url') + '/qux"',
|
|
rendered;
|
|
|
|
optionsData.data.blog.navigation = [firstItem, secondItem];
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.containEql('nav-foo');
|
|
rendered.string.should.containEql('nav-bar-baz-qux');
|
|
rendered.string.should.containEql(testUrl);
|
|
rendered.string.should.containEql(testUrl2);
|
|
});
|
|
|
|
it('can annotate the current url', function () {
|
|
var firstItem = {label: 'Foo', url: '/foo'},
|
|
secondItem = {label: 'Bar', url: '/qux'},
|
|
rendered;
|
|
|
|
optionsData.data.blog.navigation = [firstItem, secondItem];
|
|
optionsData.data.root.relativeUrl = '/foo';
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.containEql('nav-foo');
|
|
rendered.string.should.containEql('nav-current');
|
|
rendered.string.should.containEql('nav-foo nav-current');
|
|
rendered.string.should.containEql('nav-bar"');
|
|
});
|
|
|
|
it('can annotate current url with trailing slash', function () {
|
|
var firstItem = {label: 'Foo', url: '/foo'},
|
|
secondItem = {label: 'Bar', url: '/qux'},
|
|
rendered;
|
|
|
|
optionsData.data.blog.navigation = [firstItem, secondItem];
|
|
optionsData.data.root.relativeUrl = '/foo/';
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.containEql('nav-foo');
|
|
rendered.string.should.containEql('nav-current');
|
|
rendered.string.should.containEql('nav-foo nav-current');
|
|
rendered.string.should.containEql('nav-bar"');
|
|
});
|
|
|
|
it('doesn\'t html-escape URLs', function () {
|
|
var firstItem = {label: 'Foo', url: '/?foo=bar&baz=qux'},
|
|
rendered;
|
|
|
|
optionsData.data.blog.navigation = [firstItem];
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.not.containEql('=');
|
|
rendered.string.should.not.containEql('&');
|
|
rendered.string.should.containEql('/?foo=bar&baz=qux');
|
|
});
|
|
|
|
it('encodes URLs', function () {
|
|
var firstItem = {label: 'Foo', url: '/?foo=space bar&<script>alert("gotcha")</script>'},
|
|
rendered;
|
|
|
|
optionsData.data.blog.navigation = [firstItem];
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.containEql('foo=space%20bar');
|
|
rendered.string.should.not.containEql('<script>alert("gotcha")</script>');
|
|
rendered.string.should.containEql('%3Cscript%3Ealert(%22gotcha%22)%3C/script%3E');
|
|
});
|
|
|
|
it('doesn\'t double-encode URLs', function () {
|
|
var firstItem = {label: 'Foo', url: '/?foo=space%20bar'},
|
|
rendered;
|
|
|
|
optionsData.data.blog.navigation = [firstItem];
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.not.containEql('foo=space%2520bar');
|
|
});
|
|
});
|
|
|
|
describe('{{navigation}} helper with custom template', function () {
|
|
var optionsData;
|
|
|
|
before(function (done) {
|
|
hbs.express4({
|
|
partialsDir: [path.resolve(configUtils.config.get('paths').corePath, 'test/unit/helpers/test_tpl')]
|
|
});
|
|
|
|
hbs.cachePartials(function () {
|
|
done();
|
|
});
|
|
});
|
|
|
|
beforeEach(function () {
|
|
optionsData = {
|
|
data: {
|
|
blog: {
|
|
navigation: [{label: 'Foo', url: '/foo'}]
|
|
},
|
|
root: {
|
|
relativeUrl: ''
|
|
}
|
|
}
|
|
};
|
|
});
|
|
|
|
it('can render one item and @blog title', function () {
|
|
var testUrl = 'href="' + configUtils.config.get('url') + '/foo"',
|
|
rendered;
|
|
|
|
// Set @blog.title
|
|
optionsData.data.blog.title = 'Chaos is a ladder.';
|
|
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.containEql('Chaos is a ladder');
|
|
rendered.string.should.not.containEql('isHeader is set');
|
|
rendered.string.should.containEql(testUrl);
|
|
rendered.string.should.containEql('Foo');
|
|
});
|
|
|
|
it('can pass attributes through', function () {
|
|
var testUrl = 'href="' + configUtils.config.get('url') + '/foo"',
|
|
rendered;
|
|
|
|
// Simulate {{navigation isHeader=true}}
|
|
optionsData.hash = {isHeader: true};
|
|
|
|
rendered = helpers.navigation(optionsData);
|
|
|
|
should.exist(rendered);
|
|
rendered.string.should.not.containEql('Chaos is a ladder');
|
|
rendered.string.should.containEql('isHeader is set');
|
|
rendered.string.should.containEql(testUrl);
|
|
rendered.string.should.containEql('Foo');
|
|
});
|
|
});
|