2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
2021-10-06 12:52:46 +03:00
|
|
|
const hbs = require('../../../../core/frontend/services/theme-engine/engine');
|
|
|
|
const configUtils = require('../../../utils/configUtils');
|
2020-04-29 18:44:27 +03:00
|
|
|
const path = require('path');
|
2021-10-06 12:52:46 +03:00
|
|
|
const page_url = require('../../../../core/frontend/helpers/page_url');
|
|
|
|
const pagination = require('../../../../core/frontend/helpers/pagination');
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
describe('{{pagination}} helper', function () {
|
|
|
|
before(function (done) {
|
2018-08-30 19:17:27 +03:00
|
|
|
hbs.express4({partialsDir: [configUtils.config.get('paths').helperTemplates]});
|
2015-03-10 18:52:00 +03:00
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
hbs.cachePartials(function () {
|
|
|
|
done();
|
|
|
|
});
|
2017-03-23 22:00:58 +03:00
|
|
|
|
|
|
|
// The pagination partial expects this helper
|
|
|
|
// @TODO: change to register with Ghost's own registration tools
|
2021-10-04 18:30:54 +03:00
|
|
|
hbs.registerHelper('page_url', page_url);
|
2014-10-10 18:54:07 +04:00
|
|
|
});
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const paginationRegex = /class="pagination"/;
|
|
|
|
const newerRegex = /class="newer-posts"/;
|
|
|
|
const olderRegex = /class="older-posts"/;
|
|
|
|
const pageRegex = /class="page-number"/;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
|
|
|
it('should throw if pagination data is incorrect', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const runHelper = function (data) {
|
|
|
|
return function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
pagination.call(data);
|
2020-04-29 18:44:27 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-01-19 23:59:45 +03:00
|
|
|
const expectedMessage = 'The {{pagination}} helper was used outside of a paginated context. See https://ghost.org/docs/themes/helpers/pagination/.';
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-11-28 15:15:05 +03:00
|
|
|
runHelper('not an object').should.throwError(expectedMessage);
|
2017-03-21 11:24:11 +03:00
|
|
|
runHelper(function () {
|
2017-11-28 15:15:05 +03:00
|
|
|
}).should.throwError(expectedMessage);
|
2014-10-10 18:54:07 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can render single page with no pagination necessary', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = pagination.call({
|
2014-10-10 18:54:07 +04:00
|
|
|
pagination: {page: 1, prev: null, next: null, limit: 15, total: 8, pages: 1},
|
|
|
|
tag: {slug: 'slug'}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
// strip out carriage returns and compare.
|
|
|
|
rendered.string.should.match(paginationRegex);
|
|
|
|
rendered.string.should.match(pageRegex);
|
|
|
|
rendered.string.should.match(/Page 1 of 1/);
|
|
|
|
rendered.string.should.not.match(newerRegex);
|
|
|
|
rendered.string.should.not.match(olderRegex);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can render first page of many with older posts link', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = pagination.call({
|
2014-10-10 18:54:07 +04:00
|
|
|
pagination: {page: 1, prev: null, next: 2, limit: 15, total: 8, pages: 3}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(paginationRegex);
|
|
|
|
rendered.string.should.match(pageRegex);
|
|
|
|
rendered.string.should.match(olderRegex);
|
|
|
|
rendered.string.should.match(/Page 1 of 3/);
|
|
|
|
rendered.string.should.not.match(newerRegex);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can render middle pages of many with older and newer posts link', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = pagination.call({
|
2014-10-10 18:54:07 +04:00
|
|
|
pagination: {page: 2, prev: 1, next: 3, limit: 15, total: 8, pages: 3}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(paginationRegex);
|
|
|
|
rendered.string.should.match(pageRegex);
|
|
|
|
rendered.string.should.match(olderRegex);
|
|
|
|
rendered.string.should.match(newerRegex);
|
|
|
|
rendered.string.should.match(/Page 2 of 3/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can render last page of many with newer posts link', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = pagination.call({
|
2014-10-10 18:54:07 +04:00
|
|
|
pagination: {page: 3, prev: 2, next: null, limit: 15, total: 8, pages: 3}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(paginationRegex);
|
|
|
|
rendered.string.should.match(pageRegex);
|
|
|
|
rendered.string.should.match(newerRegex);
|
|
|
|
rendered.string.should.match(/Page 3 of 3/);
|
|
|
|
rendered.string.should.not.match(olderRegex);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('validates values', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const runErrorTest = function (data) {
|
2014-10-10 18:54:07 +04:00
|
|
|
return function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
pagination.call(data);
|
2014-10-10 18:54:07 +04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
runErrorTest({pagination: {page: 3, prev: true, next: null, limit: 15, total: 8, pages: 3}})
|
|
|
|
.should.throwError('Invalid value, Next/Prev must be a number');
|
|
|
|
runErrorTest({pagination: {page: 3, prev: 2, next: true, limit: 15, total: 8, pages: 3}})
|
|
|
|
.should.throwError('Invalid value, Next/Prev must be a number');
|
|
|
|
|
|
|
|
runErrorTest({pagination: {limit: 15, total: 8, pages: 3}})
|
|
|
|
.should.throwError('All values must be defined for page, pages, limit and total');
|
|
|
|
runErrorTest({pagination: {page: 3, total: 8, pages: 3}})
|
|
|
|
.should.throwError('All values must be defined for page, pages, limit and total');
|
|
|
|
runErrorTest({pagination: {page: 3, limit: 15, pages: 3}})
|
|
|
|
.should.throwError('All values must be defined for page, pages, limit and total');
|
|
|
|
runErrorTest({pagination: {page: 3, limit: 15, total: 8}})
|
|
|
|
.should.throwError('All values must be defined for page, pages, limit and total');
|
|
|
|
|
|
|
|
runErrorTest({pagination: {page: null, prev: null, next: null, limit: 15, total: 8, pages: 3}})
|
|
|
|
.should.throwError('Invalid value, check page, pages, limit and total are numbers');
|
|
|
|
runErrorTest({pagination: {page: 1, prev: null, next: null, limit: null, total: 8, pages: 3}})
|
|
|
|
.should.throwError('Invalid value, check page, pages, limit and total are numbers');
|
|
|
|
runErrorTest({pagination: {page: 1, prev: null, next: null, limit: 15, total: null, pages: 3}})
|
|
|
|
.should.throwError('Invalid value, check page, pages, limit and total are numbers');
|
|
|
|
runErrorTest({pagination: {page: 1, prev: null, next: null, limit: 15, total: 8, pages: null}})
|
|
|
|
.should.throwError('Invalid value, check page, pages, limit and total are numbers');
|
|
|
|
});
|
|
|
|
});
|
2015-03-28 19:00:57 +03:00
|
|
|
|
|
|
|
describe('{{pagination}} helper with custom template', function () {
|
|
|
|
before(function (done) {
|
2020-03-30 18:26:47 +03:00
|
|
|
hbs.express4({partialsDir: [path.resolve(__dirname, './test_tpl')]});
|
2015-03-28 19:00:57 +03:00
|
|
|
|
|
|
|
hbs.cachePartials(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-09-10 12:37:04 +03:00
|
|
|
it('can render single page with @site.title', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = pagination.call({
|
2015-03-28 19:00:57 +03:00
|
|
|
pagination: {page: 1, prev: null, next: null, limit: 15, total: 8, pages: 1},
|
|
|
|
tag: {slug: 'slug'}
|
|
|
|
}, {
|
|
|
|
data: {
|
2019-09-10 12:37:04 +03:00
|
|
|
site: {
|
2015-03-28 19:00:57 +03:00
|
|
|
title: 'Chaos is a ladder.'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
// strip out carriage returns and compare.
|
|
|
|
rendered.string.should.match(/Page 1 of 1/);
|
|
|
|
rendered.string.should.containEql('Chaos is a ladder');
|
2019-03-09 22:35:19 +03:00
|
|
|
rendered.string.should.not.containEql('isHeader is set');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can pass attributes through', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = pagination.call({
|
2019-03-09 22:35:19 +03:00
|
|
|
pagination: {page: 1, prev: null, next: null, limit: 15, total: 8, pages: 1},
|
|
|
|
tag: {slug: 'slug'}
|
|
|
|
}, {
|
|
|
|
hash: {isHeader: true},
|
|
|
|
data: {
|
2019-09-10 12:37:04 +03:00
|
|
|
site: {}
|
2019-03-09 22:35:19 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
// strip out carriage returns and compare.
|
|
|
|
rendered.string.should.match(/Page 1 of 1/);
|
|
|
|
rendered.string.should.not.containEql('Chaos is a ladder');
|
|
|
|
rendered.string.should.containEql('isHeader is set');
|
2015-03-28 19:00:57 +03:00
|
|
|
});
|
|
|
|
});
|