From cf2b429436885a7d0132a509981dff8ceaa46e56 Mon Sep 17 00:00:00 2001 From: Steve Larson <9larsons@gmail.com> Date: Wed, 16 Oct 2024 10:31:57 -0500 Subject: [PATCH] Stubbed ghost core unit test errors (#21324) no ref Stubbed expected test errors. In general, we should be expecting these errors in the tests as we write them as that is the expected behavior (or that behavior should change). --- .../unit/frontend/helpers/ghost_head.test.js | 21 +++++++++++++++++++ .../frontend/helpers/readable_url.test.js | 19 ++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/ghost/core/test/unit/frontend/helpers/ghost_head.test.js b/ghost/core/test/unit/frontend/helpers/ghost_head.test.js index 2241abb0a9..f314785434 100644 --- a/ghost/core/test/unit/frontend/helpers/ghost_head.test.js +++ b/ghost/core/test/unit/frontend/helpers/ghost_head.test.js @@ -10,6 +10,7 @@ const models = require('../../../../core/server/models'); const imageLib = require('../../../../core/server/lib/image'); const routing = require('../../../../core/frontend/services/routing'); const urlService = require('../../../../core/server/services/url'); +const logging = require('@tryghost/logging'); const ghost_head = require('../../../../core/frontend/helpers/ghost_head'); const proxy = require('../../../../core/frontend/services/proxy'); @@ -393,12 +394,19 @@ describe('{{ghost_head}} helper', function () { }); describe('without Code Injection', function () { + let loggingErrorStub; // assert # of calls if test throws errors, do not globally stub + beforeEach(function () { configUtils.set({url: 'http://localhost:65530/'}); }); + afterEach(function () { + sinon.restore(); + }); + it('returns meta tag string on paginated index page without structured data and schema', async function () { // @TODO: later we can extend this fn with an `meta` object e.g. locals.meta + loggingErrorStub = sinon.stub(logging, 'error'); await testGhostHead(testUtils.createHbsResponse({ locals: { relativeUrl: '/page/2/', @@ -406,6 +414,7 @@ describe('{{ghost_head}} helper', function () { safeVersion: '0.3' } })); + sinon.assert.calledOnce(loggingErrorStub); }); it('returns structured data on first index page', async function () { @@ -771,19 +780,27 @@ describe('{{ghost_head}} helper', function () { }); it('disallows indexing for preview pages', async function () { + loggingErrorStub = sinon.stub(logging, 'error'); await testGhostHead(testUtils.createHbsResponse({ locals: { context: ['preview', 'post'] } })); + // Unknown Request error for favico + // TypeError for primary_author being undefined + sinon.assert.calledOnce(loggingErrorStub); }); it('implicit indexing settings for non-preview pages', async function () { + loggingErrorStub = sinon.stub(logging, 'error'); await testGhostHead(testUtils.createHbsResponse({ locals: { context: ['featured', 'paged', 'index', 'post', 'amp', 'home', 'unicorn'] } })); + // Unknown Request error for favico + // TypeError for primary_author being undefined + sinon.assert.calledOnce(loggingErrorStub); }); it('outputs structured data but not schema for custom collection', async function () { @@ -951,6 +968,8 @@ describe('{{ghost_head}} helper', function () { }); it('does not contain amphtml link', async function () { + let loggingErrorStub = sinon.stub(logging, 'error'); + const renderObject = { post: posts[1] }; @@ -963,6 +982,8 @@ describe('{{ghost_head}} helper', function () { safeVersion: '0.3' } })); + + sinon.assert.calledOnce(loggingErrorStub); }); }); diff --git a/ghost/core/test/unit/frontend/helpers/readable_url.test.js b/ghost/core/test/unit/frontend/helpers/readable_url.test.js index 9adb8a9b66..ba86ed0955 100644 --- a/ghost/core/test/unit/frontend/helpers/readable_url.test.js +++ b/ghost/core/test/unit/frontend/helpers/readable_url.test.js @@ -1,7 +1,21 @@ -const should = require('should'); const readable_url = require('../../../../core/frontend/helpers/readable_url'); +const logging = require('@tryghost/logging'); +const sinon = require('sinon'); +const errors = require('@tryghost/errors'); describe('{{#readable_url}} helper', function () { + let loggingErrorStub; + + beforeEach(function () { + // Stub the logging.error method + loggingErrorStub = sinon.stub(logging, 'error'); + }); + + afterEach(function () { + // Restore the original logging.error method + loggingErrorStub.restore(); + }); + it('renders a short URL, without protocol, www, query params nor hash fragments', function () { const readable = readable_url.call( {}, @@ -17,6 +31,9 @@ describe('{{#readable_url}} helper', function () { {foo: 'bar'} ); + sinon.assert.calledOnce(loggingErrorStub); + sinon.assert.calledWith(loggingErrorStub, sinon.match.instanceOf(errors.IncorrectUsageError)); + readable.string.should.equal(''); });