From 08479f3816aa934f4a09a39397dc202cf7ca43c0 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 5 Apr 2022 19:31:49 +0100 Subject: [PATCH] Moved routing helpers to rendering service - The helpers folder was full of things used for rendering pages - It belongs as its own service so that we can see what it really does --- core/frontend/apps/amp/lib/router.js | 4 +- .../apps/private-blogging/lib/router.js | 4 +- .../{routing/helpers => rendering}/context.js | 0 .../{routing/helpers => rendering}/error.js | 0 .../helpers => rendering}/format-response.js | 2 +- .../{routing/helpers => rendering}/index.js | 0 .../helpers => rendering}/render-entries.js | 2 +- .../helpers => rendering}/render-entry.js | 2 +- .../helpers => rendering}/renderer.js | 2 +- .../{routing/helpers => rendering}/secure.js | 0 .../helpers => rendering}/templates.js | 4 +- .../services/routing/CollectionRouter.js | 2 +- .../services/routing/controllers/channel.js | 15 +++--- .../routing/controllers/collection.js | 15 +++--- .../routing/controllers/email-post.js | 11 ++--- .../services/routing/controllers/entry.js | 9 ++-- .../services/routing/controllers/preview.js | 11 ++--- .../services/routing/controllers/rss.js | 4 +- .../services/routing/controllers/static.js | 10 ++-- .../routing/controllers/unsubscribe.js | 4 +- core/frontend/services/routing/index.js | 4 -- core/frontend/web/middleware/error-handler.js | 4 +- test/unit/frontend/apps/amp/router.test.js | 10 ++-- .../helpers => rendering}/context.test.js | 48 +++++++++---------- .../helpers => rendering}/error.test.js | 2 +- .../format-response.test.js | 6 +-- .../helpers => rendering}/templates.test.js | 4 +- .../routing/controllers/channel.test.js | 6 +-- .../routing/controllers/collection.test.js | 6 +-- .../routing/controllers/entry.test.js | 6 +-- .../routing/controllers/preview.test.js | 6 +-- .../routing/controllers/static.test.js | 24 +++++----- 32 files changed, 109 insertions(+), 118 deletions(-) rename core/frontend/services/{routing/helpers => rendering}/context.js (100%) rename core/frontend/services/{routing/helpers => rendering}/error.js (100%) rename core/frontend/services/{routing/helpers => rendering}/format-response.js (96%) rename core/frontend/services/{routing/helpers => rendering}/index.js (100%) rename core/frontend/services/{routing/helpers => rendering}/render-entries.js (85%) rename core/frontend/services/{routing/helpers => rendering}/render-entry.js (85%) rename core/frontend/services/{routing/helpers => rendering}/renderer.js (91%) rename core/frontend/services/{routing/helpers => rendering}/secure.js (100%) rename core/frontend/services/{routing/helpers => rendering}/templates.js (98%) rename test/unit/frontend/services/{routing/helpers => rendering}/context.test.js (88%) rename test/unit/frontend/services/{routing/helpers => rendering}/error.test.js (92%) rename test/unit/frontend/services/{routing/helpers => rendering}/format-response.test.js (95%) rename test/unit/frontend/services/{routing/helpers => rendering}/templates.test.js (99%) diff --git a/core/frontend/apps/amp/lib/router.js b/core/frontend/apps/amp/lib/router.js index ec0ea25eb2..443e25c5a6 100644 --- a/core/frontend/apps/amp/lib/router.js +++ b/core/frontend/apps/amp/lib/router.js @@ -8,7 +8,7 @@ const errors = require('@tryghost/errors'); // Dirty requires const urlService = require('../../../../server/services/url'); const dataService = require('../../../services/data'); -const helpers = require('../../../services/routing/helpers'); +const renderer = require('../../../services/rendering'); const templateName = 'amp'; const messages = { @@ -32,7 +32,7 @@ function _renderer(req, res, next) { } // Render Call - return helpers.renderer(req, res, body); + return renderer.renderer(req, res, body); } // This here is a controller. diff --git a/core/frontend/apps/private-blogging/lib/router.js b/core/frontend/apps/private-blogging/lib/router.js index 80322482da..e687dd6552 100644 --- a/core/frontend/apps/private-blogging/lib/router.js +++ b/core/frontend/apps/private-blogging/lib/router.js @@ -2,7 +2,7 @@ const path = require('path'); const express = require('../../../../shared/express'); const middleware = require('./middleware'); const bodyParser = require('body-parser'); -const routing = require('../../../services/routing'); +const renderer = require('../../../services/rendering'); const web = require('../../../../server/web'); const templateName = 'private'; const privateRouter = express.Router(templateName); @@ -23,7 +23,7 @@ function _renderer(req, res) { } // Render Call - return routing.helpers.renderer(req, res, data); + return renderer.renderer(req, res, data); } // password-protected frontend route diff --git a/core/frontend/services/routing/helpers/context.js b/core/frontend/services/rendering/context.js similarity index 100% rename from core/frontend/services/routing/helpers/context.js rename to core/frontend/services/rendering/context.js diff --git a/core/frontend/services/routing/helpers/error.js b/core/frontend/services/rendering/error.js similarity index 100% rename from core/frontend/services/routing/helpers/error.js rename to core/frontend/services/rendering/error.js diff --git a/core/frontend/services/routing/helpers/format-response.js b/core/frontend/services/rendering/format-response.js similarity index 96% rename from core/frontend/services/routing/helpers/format-response.js rename to core/frontend/services/rendering/format-response.js index eef78ef1f3..6e87d3707f 100644 --- a/core/frontend/services/routing/helpers/format-response.js +++ b/core/frontend/services/rendering/format-response.js @@ -1,5 +1,5 @@ const _ = require('lodash'); -const {prepareContextResource} = require('../../proxy'); +const {prepareContextResource} = require('../proxy'); /** * @description Formats API response into handlebars/theme format. diff --git a/core/frontend/services/routing/helpers/index.js b/core/frontend/services/rendering/index.js similarity index 100% rename from core/frontend/services/routing/helpers/index.js rename to core/frontend/services/rendering/index.js diff --git a/core/frontend/services/routing/helpers/render-entries.js b/core/frontend/services/rendering/render-entries.js similarity index 85% rename from core/frontend/services/routing/helpers/render-entries.js rename to core/frontend/services/rendering/render-entries.js index 64ce061232..1e0a33bb1e 100644 --- a/core/frontend/services/routing/helpers/render-entries.js +++ b/core/frontend/services/rendering/render-entries.js @@ -1,4 +1,4 @@ -const debug = require('@tryghost/debug')('services:routing:helpers:render-entries'); +const debug = require('@tryghost/debug')('services:routing:renderer:render-entries'); const formatResponse = require('./format-response'); const renderer = require('./renderer'); diff --git a/core/frontend/services/routing/helpers/render-entry.js b/core/frontend/services/rendering/render-entry.js similarity index 85% rename from core/frontend/services/routing/helpers/render-entry.js rename to core/frontend/services/rendering/render-entry.js index e6609a70d3..03f9080aee 100644 --- a/core/frontend/services/routing/helpers/render-entry.js +++ b/core/frontend/services/rendering/render-entry.js @@ -1,4 +1,4 @@ -const debug = require('@tryghost/debug')('services:routing:helpers:render-post'); +const debug = require('@tryghost/debug')('services:routing:renderer:render-post'); const formatResponse = require('./format-response'); const renderer = require('./renderer'); /** diff --git a/core/frontend/services/routing/helpers/renderer.js b/core/frontend/services/rendering/renderer.js similarity index 91% rename from core/frontend/services/routing/helpers/renderer.js rename to core/frontend/services/rendering/renderer.js index dc138f08d7..d2072bec8e 100644 --- a/core/frontend/services/routing/helpers/renderer.js +++ b/core/frontend/services/rendering/renderer.js @@ -1,4 +1,4 @@ -const debug = require('@tryghost/debug')('services:routing:helpers:renderer'); +const debug = require('@tryghost/debug')('services:routing:renderer:renderer'); const setContext = require('./context'); const templates = require('./templates'); diff --git a/core/frontend/services/routing/helpers/secure.js b/core/frontend/services/rendering/secure.js similarity index 100% rename from core/frontend/services/routing/helpers/secure.js rename to core/frontend/services/rendering/secure.js diff --git a/core/frontend/services/routing/helpers/templates.js b/core/frontend/services/rendering/templates.js similarity index 98% rename from core/frontend/services/routing/helpers/templates.js rename to core/frontend/services/rendering/templates.js index 5c2f31651d..79e9258f48 100644 --- a/core/frontend/services/routing/helpers/templates.js +++ b/core/frontend/services/rendering/templates.js @@ -6,8 +6,8 @@ const _ = require('lodash'); const path = require('path'); const url = require('url'); -const config = require('../../../../shared/config'); -const themeEngine = require('../../theme-engine'); +const config = require('../../../shared/config'); +const themeEngine = require('../theme-engine'); const _private = {}; /** diff --git a/core/frontend/services/routing/CollectionRouter.js b/core/frontend/services/routing/CollectionRouter.js index 03cb0efa86..70bf2dc063 100644 --- a/core/frontend/services/routing/CollectionRouter.js +++ b/core/frontend/services/routing/CollectionRouter.js @@ -30,7 +30,7 @@ class CollectionRouter extends ParentRouter { value: object.permalink }; - // @NOTE: see helpers/templates - we use unshift to prepend the templates + // @NOTE: see renderer/templates - we use unshift to prepend the templates this.templates = (object.templates || []).reverse(); this.filter = object.filter; diff --git a/core/frontend/services/routing/controllers/channel.js b/core/frontend/services/routing/controllers/channel.js index 43a0188770..1071041f86 100644 --- a/core/frontend/services/routing/controllers/channel.js +++ b/core/frontend/services/routing/controllers/channel.js @@ -5,7 +5,7 @@ const errors = require('@tryghost/errors'); const security = require('@tryghost/security'); const themeEngine = require('../../theme-engine'); const dataService = require('../../data'); -const helpers = require('../helpers'); +const renderer = require('../../rendering'); const messages = { pageNotFound: 'Page not found.' @@ -61,16 +61,15 @@ module.exports = function channelController(req, res, next) { } // Format data 1 - // @TODO: See helpers/secure for explanation. - helpers.secure(req, result.posts); + // @TODO: See renderer/secure for explanation. + renderer.secure(req, result.posts); - // @TODO: See helpers/secure for explanation. + // @TODO: See renderer/secure for explanation. _.each(result.data, function (data) { - helpers.secure(req, data); + renderer.secure(req, data); }); - const renderer = helpers.renderEntries(req, res); - return renderer(result); + return renderer.renderEntries(req, res)(result); }) - .catch(helpers.handleError(next)); + .catch(renderer.handleError(next)); }; diff --git a/core/frontend/services/routing/controllers/collection.js b/core/frontend/services/routing/controllers/collection.js index 04ace71008..e2ddf95fd7 100644 --- a/core/frontend/services/routing/controllers/collection.js +++ b/core/frontend/services/routing/controllers/collection.js @@ -5,7 +5,7 @@ const errors = require('@tryghost/errors'); const security = require('@tryghost/security'); const {routerManager} = require('../'); const themeEngine = require('../../theme-engine'); -const helpers = require('../helpers'); +const renderer = require('../../rendering'); const dataService = require('../../data'); const messages = { @@ -81,16 +81,15 @@ module.exports = function collectionController(req, res, next) { }); // Format data 1 - // @TODO: See helpers/secure for explanation. - helpers.secure(req, result.posts); + // @TODO: See renderer/secure for explanation. + renderer.secure(req, result.posts); - // @TODO: See helpers/secure for explanation. + // @TODO: See renderer/secure for explanation. _.each(result.data, function (data) { - helpers.secure(req, data); + renderer.secure(req, data); }); - const renderer = helpers.renderEntries(req, res); - return renderer(result); + return renderer.renderEntries(req, res)(result); }) - .catch(helpers.handleError(next)); + .catch(renderer.handleError(next)); }; diff --git a/core/frontend/services/routing/controllers/email-post.js b/core/frontend/services/routing/controllers/email-post.js index 24d56d969f..2f4caa03d3 100644 --- a/core/frontend/services/routing/controllers/email-post.js +++ b/core/frontend/services/routing/controllers/email-post.js @@ -2,7 +2,7 @@ const debug = require('@tryghost/debug')('services:routing:controllers:emailpost const config = require('../../../../shared/config'); const {routerManager} = require('../'); const urlUtils = require('../../../../shared/url-utils'); -const helpers = require('../helpers'); +const renderer = require('../../rendering'); /** * @description Email Post Controller. @@ -55,11 +55,10 @@ module.exports = function emailPostController(req, res, next) { post.access = !!post.html; } - // @TODO: See helpers/secure - helpers.secure(req, post); + // @TODO: See renderer/secure + renderer.secure(req, post); - const renderer = helpers.renderEntry(req, res); - return renderer(post); + return renderer.renderEntry(req, res)(post); }) - .catch(helpers.handleError(next)); + .catch(renderer.handleError(next)); }; diff --git a/core/frontend/services/routing/controllers/entry.js b/core/frontend/services/routing/controllers/entry.js index ff9df24b46..036bac56ef 100644 --- a/core/frontend/services/routing/controllers/entry.js +++ b/core/frontend/services/routing/controllers/entry.js @@ -4,7 +4,7 @@ const config = require('../../../../shared/config'); const {routerManager} = require('../'); const urlUtils = require('../../../../shared/url-utils'); const dataService = require('../../data'); -const helpers = require('../helpers'); +const renderer = require('../../rendering'); /** * @description Entry controller. @@ -84,10 +84,9 @@ module.exports = function entryController(req, res, next) { })); } - helpers.secure(req, entry); + renderer.secure(req, entry); - const renderer = helpers.renderEntry(req, res); - return renderer(entry); + return renderer.renderEntry(req, res)(entry); }) - .catch(helpers.handleError(next)); + .catch(renderer.handleError(next)); }; diff --git a/core/frontend/services/routing/controllers/preview.js b/core/frontend/services/routing/controllers/preview.js index 1881f29187..3b90bc13f7 100644 --- a/core/frontend/services/routing/controllers/preview.js +++ b/core/frontend/services/routing/controllers/preview.js @@ -2,7 +2,7 @@ const debug = require('@tryghost/debug')('services:routing:controllers:preview') const config = require('../../../../shared/config'); const {routerManager} = require('../'); const urlUtils = require('../../../../shared/url-utils'); -const helpers = require('../helpers'); +const renderer = require('../../rendering'); /** * @description Preview Controller. @@ -57,11 +57,10 @@ module.exports = function previewController(req, res, next) { post.access = !!post.html; } - // @TODO: See helpers/secure - helpers.secure(req, post); + // @TODO: See renderer/secure + renderer.secure(req, post); - const renderer = helpers.renderEntry(req, res); - return renderer(post); + return renderer.renderEntry(req, res)(post); }) - .catch(helpers.handleError(next)); + .catch(renderer.handleError(next)); }; diff --git a/core/frontend/services/routing/controllers/rss.js b/core/frontend/services/routing/controllers/rss.js index b0e29bc1f4..19761d6876 100644 --- a/core/frontend/services/routing/controllers/rss.js +++ b/core/frontend/services/routing/controllers/rss.js @@ -4,7 +4,7 @@ const url = require('url'); const security = require('@tryghost/security'); const settingsCache = require('../../../../shared/settings-cache'); const rssService = require('../../rss'); -const helpers = require('../helpers'); +const renderer = require('../../rendering'); const dataService = require('../../data'); // @TODO: is this really correct? Should we be using meta data title? @@ -48,5 +48,5 @@ module.exports = function rssController(req, res, next) { .then(function (data) { return rssService.render(res, baseUrl, data); }) - .catch(helpers.handleError(next)); + .catch(renderer.handleError(next)); }; diff --git a/core/frontend/services/routing/controllers/static.js b/core/frontend/services/routing/controllers/static.js index 7009ecccae..eac1c54f44 100644 --- a/core/frontend/services/routing/controllers/static.js +++ b/core/frontend/services/routing/controllers/static.js @@ -1,7 +1,7 @@ const _ = require('lodash'); const Promise = require('bluebird'); const debug = require('@tryghost/debug')('services:routing:controllers:static'); -const helpers = require('../helpers'); +const renderer = require('../../rendering'); function processQuery(query, locals) { const api = require('../../proxy').api[locals.apiVersion]; @@ -60,12 +60,12 @@ module.exports = function staticController(req, res, next) { }); } - // @TODO: See helpers/secure for more context. + // @TODO: See renderer/secure for more context. _.each(response.data, function (data) { - helpers.secure(req, data); + renderer.secure(req, data); }); - helpers.renderer(req, res, helpers.formatResponse.entries(response)); + renderer.renderer(req, res, renderer.formatResponse.entries(response)); }) - .catch(helpers.handleError(next)); + .catch(renderer.handleError(next)); }; diff --git a/core/frontend/services/routing/controllers/unsubscribe.js b/core/frontend/services/routing/controllers/unsubscribe.js index 35a16c55c8..cca2a07fa7 100644 --- a/core/frontend/services/routing/controllers/unsubscribe.js +++ b/core/frontend/services/routing/controllers/unsubscribe.js @@ -1,7 +1,7 @@ const debug = require('@tryghost/debug')('services:routing:controllers:unsubscribe'); const path = require('path'); const megaService = require('../../../../server/services/mega'); -const helpers = require('../../../services/routing/helpers'); +const renderer = require('../../rendering'); module.exports = async function unsubscribeController(req, res) { debug('unsubscribeController'); @@ -22,5 +22,5 @@ module.exports = async function unsubscribeController(req, res) { defaultTemplate: path.resolve(__dirname, '../../../views/', templateName) }; - return helpers.renderer(req, res, data); + return renderer.renderer(req, res, data); }; diff --git a/core/frontend/services/routing/index.js b/core/frontend/services/routing/index.js index 3e26758ed8..6caabf1831 100644 --- a/core/frontend/services/routing/index.js +++ b/core/frontend/services/routing/index.js @@ -7,9 +7,5 @@ module.exports = { get registry() { return registry; - }, - - get helpers() { - return require('./helpers'); } }; diff --git a/core/frontend/web/middleware/error-handler.js b/core/frontend/web/middleware/error-handler.js index fa6b0b2c66..b21bfe5a48 100644 --- a/core/frontend/web/middleware/error-handler.js +++ b/core/frontend/web/middleware/error-handler.js @@ -4,7 +4,7 @@ const tpl = require('@tryghost/tpl'); const sentry = require('../../../shared/sentry'); const config = require('../../../shared/config'); -const helpers = require('../../services/routing/helpers'); +const renderer = require('../../services/rendering'); // @TODO: make this properly shared code const {prepareError, prepareStack} = require('@tryghost/mw-error-handler'); @@ -55,7 +55,7 @@ const themeErrorRenderer = (err, req, res, next) => { // Template // @TODO: very dirty !!!!!! - helpers.templates.setTemplate(req, res); + renderer.templates.setTemplate(req, res); // It can be that something went wrong with the theme or otherwise loading handlebars // This ensures that no matter what res.render will work here diff --git a/test/unit/frontend/apps/amp/router.test.js b/test/unit/frontend/apps/amp/router.test.js index 9c687577aa..ec811d62bb 100644 --- a/test/unit/frontend/apps/amp/router.test.js +++ b/test/unit/frontend/apps/amp/router.test.js @@ -4,7 +4,7 @@ const sinon = require('sinon'); const path = require('path'); const ampController = require('../../../../../core/frontend/apps/amp/lib/router'); const urlService = require('../../../../../core/server/services/url'); -const helpers = require('../../../../../core/frontend/services/routing/helpers'); +const renderer = require('../../../../../core/frontend/services/rendering'); const dataService = require('../../../../../core/frontend/services/data'); const testUtils = require('../../../../utils'); const configUtils = require('../../../../utils/configUtils'); @@ -27,7 +27,7 @@ describe('Unit - apps/amp/lib/router', function () { beforeEach(function () { rendererStub = sinon.stub(); - sinon.stub(helpers, 'renderer').get(function () { + sinon.stub(renderer, 'renderer').get(function () { return rendererStub; }); @@ -59,7 +59,7 @@ describe('Unit - apps/amp/lib/router', function () { describe('fn: renderer', function () { it('should render default amp page when theme has no amp template', function (done) { - helpers.renderer.callsFake(function (_req, _res, data) { + renderer.renderer.callsFake(function (_req, _res, data) { _res.routerOptions.defaultTemplate.should.eql(defaultPath); data.should.eql({post: {title: 'test'}}); done(); @@ -73,7 +73,7 @@ describe('Unit - apps/amp/lib/router', function () { ampController.renderer(req, res, function (err) { (err instanceof errors.NotFoundError).should.be.true(); - helpers.renderer.called.should.be.false(); + renderer.renderer.called.should.be.false(); done(); }); }); @@ -87,7 +87,7 @@ describe('Unit - apps/amp/lib/router', function () { ampController.renderer(req, res, function (err) { (err instanceof errors.NotFoundError).should.be.true(); - helpers.renderer.called.should.be.false(); + renderer.renderer.called.should.be.false(); done(); }); }); diff --git a/test/unit/frontend/services/routing/helpers/context.test.js b/test/unit/frontend/services/rendering/context.test.js similarity index 88% rename from test/unit/frontend/services/routing/helpers/context.test.js rename to test/unit/frontend/services/rendering/context.test.js index 86e9e57228..3f5c9d50ed 100644 --- a/test/unit/frontend/services/routing/helpers/context.test.js +++ b/test/unit/frontend/services/rendering/context.test.js @@ -1,9 +1,9 @@ const should = require('should'); const sinon = require('sinon'); const _ = require('lodash'); -const testUtils = require('../../../../../utils'); -const helpers = require('../../../../../../core/frontend/services/routing/helpers'); -const labs = require('../../../../../../core/shared/labs'); +const testUtils = require('../../../../utils'); +const renderer = require('../../../../../core/frontend/services/rendering'); +const labs = require('../../../../../core/shared/labs'); describe('Contexts', function () { let req; @@ -33,14 +33,14 @@ describe('Contexts', function () { res = {}; data = {}; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(0); }); it('should return empty array with no error with basic parameters', function () { - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(0); @@ -52,7 +52,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/does/not/matter/'; res.routerOptions.context = ['index']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -64,7 +64,7 @@ describe('Contexts', function () { res.routerOptions.context = ['index']; // Execute test - helpers.context(req, res, data); + renderer.context(req, res, data); // Check context should.exist(res.locals.context); @@ -78,7 +78,7 @@ describe('Contexts', function () { res.routerOptions.context = []; // Execute test - helpers.context(req, res, data); + renderer.context(req, res, data); // Check context should.exist(res.locals.context); @@ -90,7 +90,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/page/2/'; res.routerOptions.context = ['index']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -102,7 +102,7 @@ describe('Contexts', function () { req.params.page = 2; res.routerOptions.context = ['index']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(2); @@ -116,7 +116,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/tag/getting-started/'; res.routerOptions.context = ['tag']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -127,7 +127,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/tag/getting-started/'; res.routerOptions.context = []; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(0); @@ -137,7 +137,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/tag/getting-started/page/2/'; res.routerOptions.context = ['tag']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -149,7 +149,7 @@ describe('Contexts', function () { req.params.page = 2; res.routerOptions.context = ['tag']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(2); @@ -163,7 +163,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/author/pat/'; res.routerOptions.context = ['author']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -174,7 +174,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/author/pat/'; res.routerOptions.context = []; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(0); @@ -184,7 +184,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/author/pat/page/2/'; res.routerOptions.context = ['author']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -196,7 +196,7 @@ describe('Contexts', function () { req.params.page = 2; res.routerOptions.context = ['author']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(2); @@ -210,7 +210,7 @@ describe('Contexts', function () { res.locals.relativeUrl = 'anything'; res.routerOptions.context = ['custom-context', 'test']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(2); @@ -224,7 +224,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/welcome-to-ghost/'; res.routerOptions.context = ['post']; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -237,7 +237,7 @@ describe('Contexts', function () { res.locals.relativeUrl = '/private/?r='; delete res.routerOptions; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -252,7 +252,7 @@ describe('Contexts', function () { data.post = testUtils.DataGenerator.forKnex.createPost(); delete res.routerOptions; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(1); @@ -266,7 +266,7 @@ describe('Contexts', function () { data.post = testUtils.DataGenerator.forKnex.createPost(); delete res.routerOptions; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(2); @@ -279,7 +279,7 @@ describe('Contexts', function () { data.page = testUtils.DataGenerator.forKnex.createPost({page: true}); delete res.routerOptions; - helpers.context(req, res, data); + renderer.context(req, res, data); should.exist(res.locals.context); res.locals.context.should.be.an.Array().with.lengthOf(2); diff --git a/test/unit/frontend/services/routing/helpers/error.test.js b/test/unit/frontend/services/rendering/error.test.js similarity index 92% rename from test/unit/frontend/services/routing/helpers/error.test.js rename to test/unit/frontend/services/rendering/error.test.js index 2631d1e065..d3ac47630f 100644 --- a/test/unit/frontend/services/routing/helpers/error.test.js +++ b/test/unit/frontend/services/rendering/error.test.js @@ -1,7 +1,7 @@ const should = require('should'); const sinon = require('sinon'); const errors = require('@tryghost/errors'); -const helpers = require('../../../../../../core/frontend/services/routing/helpers'); +const helpers = require('../../../../../core/frontend/services/rendering'); describe('handleError', function () { let next; diff --git a/test/unit/frontend/services/routing/helpers/format-response.test.js b/test/unit/frontend/services/rendering/format-response.test.js similarity index 95% rename from test/unit/frontend/services/routing/helpers/format-response.test.js rename to test/unit/frontend/services/rendering/format-response.test.js index 5364fab020..597a263d55 100644 --- a/test/unit/frontend/services/routing/helpers/format-response.test.js +++ b/test/unit/frontend/services/rendering/format-response.test.js @@ -1,7 +1,7 @@ const should = require('should'); -const testUtils = require('../../../../../utils'); -const helpers = require('../../../../../../core/frontend/services/routing/helpers'); -const {SafeString} = require('../../../../../../core/frontend/services/handlebars'); +const testUtils = require('../../../../utils'); +const helpers = require('../../../../../core/frontend/services/rendering'); +const {SafeString} = require('../../../../../core/frontend/services/handlebars'); describe('Unit - services/routing/helpers/format-response', function () { let posts; diff --git a/test/unit/frontend/services/routing/helpers/templates.test.js b/test/unit/frontend/services/rendering/templates.test.js similarity index 99% rename from test/unit/frontend/services/routing/helpers/templates.test.js rename to test/unit/frontend/services/rendering/templates.test.js index c12f5f48b8..dd8df83c8c 100644 --- a/test/unit/frontend/services/routing/helpers/templates.test.js +++ b/test/unit/frontend/services/rendering/templates.test.js @@ -1,8 +1,8 @@ const should = require('should'); const sinon = require('sinon'); const rewire = require('rewire'); -const templates = rewire('../../../../../../core/frontend/services/routing/helpers/templates'); -const themeEngine = require('../../../../../../core/frontend/services/theme-engine'); +const templates = rewire('../../../../../core/frontend/services/rendering/templates'); +const themeEngine = require('../../../../../core/frontend/services/theme-engine'); describe('templates', function () { let getActiveThemeStub; diff --git a/test/unit/frontend/services/routing/controllers/channel.test.js b/test/unit/frontend/services/routing/controllers/channel.test.js index bf87782c2f..4ba53eafe4 100644 --- a/test/unit/frontend/services/routing/controllers/channel.test.js +++ b/test/unit/frontend/services/routing/controllers/channel.test.js @@ -5,7 +5,7 @@ const testUtils = require('../../../../../utils'); const security = require('@tryghost/security'); const themeEngine = require('../../../../../../core/frontend/services/theme-engine'); const controllers = require('../../../../../../core/frontend/services/routing/controllers'); -const helpers = require('../../../../../../core/frontend/services/routing/helpers'); +const renderer = require('../../../../../../core/frontend/services/rendering'); const dataService = require('../../../../../../core/frontend/services/data'); function failTest(done) { @@ -41,7 +41,7 @@ describe('Unit - services/routing/controllers/channel', function () { sinon.stub(security.string, 'safe').returns('safe'); - sinon.stub(helpers, 'secure').get(function () { + sinon.stub(renderer, 'secure').get(function () { return secureStub; }); @@ -53,7 +53,7 @@ describe('Unit - services/routing/controllers/channel', function () { } }); - sinon.stub(helpers, 'renderEntries').returns(renderStub); + sinon.stub(renderer, 'renderEntries').returns(renderStub); req = { path: '/', diff --git a/test/unit/frontend/services/routing/controllers/collection.test.js b/test/unit/frontend/services/routing/controllers/collection.test.js index 04d69ab91b..99b5187b14 100644 --- a/test/unit/frontend/services/routing/controllers/collection.test.js +++ b/test/unit/frontend/services/routing/controllers/collection.test.js @@ -6,7 +6,7 @@ const security = require('@tryghost/security'); const themeEngine = require('../../../../../../core/frontend/services/theme-engine'); const routerManager = require('../../../../../../core/frontend/services/routing/').routerManager; const controllers = require('../../../../../../core/frontend/services/routing/controllers'); -const helpers = require('../../../../../../core/frontend/services/routing/helpers'); +const renderer = require('../../../../../../core/frontend/services/rendering'); const dataService = require('../../../../../../core/frontend/services/data'); function failTest(done) { @@ -43,7 +43,7 @@ describe('Unit - services/routing/controllers/collection', function () { sinon.stub(security.string, 'safe').returns('safe'); - sinon.stub(helpers, 'secure').get(function () { + sinon.stub(renderer, 'secure').get(function () { return secureStub; }); @@ -55,7 +55,7 @@ describe('Unit - services/routing/controllers/collection', function () { } }); - sinon.stub(helpers, 'renderEntries').returns(renderStub); + sinon.stub(renderer, 'renderEntries').returns(renderStub); ownsStub = sinon.stub(routerManager, 'owns'); ownsStub.withArgs('identifier', posts[0].id).returns(true); diff --git a/test/unit/frontend/services/routing/controllers/entry.test.js b/test/unit/frontend/services/routing/controllers/entry.test.js index 44aa3557ff..1f23a9a26d 100644 --- a/test/unit/frontend/services/routing/controllers/entry.test.js +++ b/test/unit/frontend/services/routing/controllers/entry.test.js @@ -5,7 +5,7 @@ const configUtils = require('../../../../../utils/configUtils'); const urlUtils = require('../../../../../../core/shared/url-utils'); const routerManager = require('../../../../../../core/frontend/services/routing/').routerManager; const controllers = require('../../../../../../core/frontend/services/routing/controllers'); -const helpers = require('../../../../../../core/frontend/services/routing/helpers'); +const renderer = require('../../../../../../core/frontend/services/rendering'); const dataService = require('../../../../../../core/frontend/services/data'); const EDITOR_URL = `/#/editor/post/`; @@ -32,11 +32,11 @@ describe('Unit - services/routing/controllers/entry', function () { return entryLookUpStub; }); - sinon.stub(helpers, 'secure').get(function () { + sinon.stub(renderer, 'secure').get(function () { return secureStub; }); - sinon.stub(helpers, 'renderEntry').get(function () { + sinon.stub(renderer, 'renderEntry').get(function () { return renderStub; }); diff --git a/test/unit/frontend/services/routing/controllers/preview.test.js b/test/unit/frontend/services/routing/controllers/preview.test.js index ffc61ef6d5..7a935a02d9 100644 --- a/test/unit/frontend/services/routing/controllers/preview.test.js +++ b/test/unit/frontend/services/routing/controllers/preview.test.js @@ -5,7 +5,7 @@ const testUtils = require('../../../../../utils'); const configUtils = require('../../../../../utils/configUtils'); const api = require('../../../../../../core/server/api').canary; const controllers = require('../../../../../../core/frontend/services/routing/controllers'); -const helpers = require('../../../../../../core/frontend/services/routing/helpers'); +const renderer = require('../../../../../../core/frontend/services/rendering'); const urlService = require('../../../../../../core/server/services/url'); const urlUtils = require('../../../../../../core/shared/url-utils'); @@ -64,12 +64,12 @@ describe('Unit - services/routing/controllers/preview', function () { sinon.stub(urlUtils, 'redirect301'); sinon.stub(urlService, 'getUrlByResourceId'); - sinon.stub(helpers, 'secure').get(function () { + sinon.stub(renderer, 'secure').get(function () { return secureStub; }); renderStub = sinon.stub(); - sinon.stub(helpers, 'renderEntry').get(function () { + sinon.stub(renderer, 'renderEntry').get(function () { return function () { return renderStub; }; diff --git a/test/unit/frontend/services/routing/controllers/static.test.js b/test/unit/frontend/services/routing/controllers/static.test.js index 46b8d48526..d594ed6e24 100644 --- a/test/unit/frontend/services/routing/controllers/static.test.js +++ b/test/unit/frontend/services/routing/controllers/static.test.js @@ -1,10 +1,10 @@ const should = require('should'); const sinon = require('sinon'); -const testUtils = require('../../../../../utils'); + const API_VERSION = 'canary'; const api = require('../../../../../../core/server/api')[API_VERSION]; const themeEngine = require('../../../../../../core/frontend/services/theme-engine'); -const helpers = require('../../../../../../core/frontend/services/routing/helpers'); +const renderer = require('../../../../../../core/frontend/services/rendering'); const controllers = require('../../../../../../core/frontend/services/routing/controllers'); function failTest(done) { @@ -40,11 +40,11 @@ describe('Unit - services/routing/controllers/static', function () { }; }); - sinon.stub(helpers, 'secure').get(function () { + sinon.stub(renderer, 'secure').get(function () { return secureStub; }); - sinon.stub(helpers, 'handleError').get(function () { + sinon.stub(renderer, 'handleError').get(function () { return handleErrorStub; }); @@ -56,11 +56,11 @@ describe('Unit - services/routing/controllers/static', function () { } }); - sinon.stub(helpers, 'renderer').get(function () { + sinon.stub(renderer, 'renderer').get(function () { return renderStub; }); - sinon.stub(helpers, 'formatResponse').get(function () { + sinon.stub(renderer, 'formatResponse').get(function () { return formatResponseStub; }); @@ -85,10 +85,10 @@ describe('Unit - services/routing/controllers/static', function () { }); it('no extra data to fetch', function (done) { - helpers.renderer.callsFake(function () { - helpers.formatResponse.entries.calledOnce.should.be.true(); + renderer.renderer.callsFake(function () { + renderer.formatResponse.entries.calledOnce.should.be.true(); tagsReadStub.called.should.be.false(); - helpers.secure.called.should.be.false(); + renderer.secure.called.should.be.false(); done(); }); @@ -109,10 +109,10 @@ describe('Unit - services/routing/controllers/static', function () { tagsReadStub = sinon.stub().resolves({tags: [{slug: 'bacon'}]}); - helpers.renderer.callsFake(function () { + renderer.renderer.callsFake(function () { tagsReadStub.called.should.be.true(); - helpers.formatResponse.entries.calledOnce.should.be.true(); - helpers.secure.calledOnce.should.be.true(); + renderer.formatResponse.entries.calledOnce.should.be.true(); + renderer.secure.calledOnce.should.be.true(); done(); });