From 4aacd50fee0a66cfe1ea67585c5078eb48d2e7b0 Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 23 Jan 2023 15:29:26 +0800 Subject: [PATCH] Added coverage for URLResourceUpdatedEvent refs https://github.com/TryGhost/Toolbox/issues/503 - The listener was not covered during quick and dirty implementation. While in the area did some cleanup to the sitemap manager test - One of the problems I've stumbled upon when adding a test is having multiple instances of SiteManager in the test, which in turn created multiple "subscribe" events and repeat handle executions. Fixed it by having just one site manager instance (a singleton) as that's the pattern that used in main codebase --- .../frontend/services/sitemap/manager.test.js | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/ghost/core/test/unit/frontend/services/sitemap/manager.test.js b/ghost/core/test/unit/frontend/services/sitemap/manager.test.js index 224726ece7..dd16c4b352 100644 --- a/ghost/core/test/unit/frontend/services/sitemap/manager.test.js +++ b/ghost/core/test/unit/frontend/services/sitemap/manager.test.js @@ -1,7 +1,11 @@ const should = require('should'); const sinon = require('sinon'); +const assert = require('assert'); // Stuff we are testing +const DomainEvents = require('@tryghost/domain-events'); +const {URLResourceUpdatedEvent} = require('@tryghost/dynamic-routing-events'); + const events = require('../../../../../core/server/lib/common/events'); const SiteMapManager = require('../../../../../core/frontend/services/sitemap/manager'); @@ -30,9 +34,11 @@ describe('Unit: sitemap/manager', function () { return new SiteMapManager({posts: posts, pages: pages, tags: tags, authors: authors}); }; - beforeEach(function () { + before(function () { eventsToRemember = {}; + // @NOTE: the pattern of faking event call is not great, we should be + // ideally tasting on real events instead of faking them sinon.stub(events, 'on').callsFake(function (eventName, callback) { eventsToRemember[eventName] = callback; }); @@ -43,25 +49,15 @@ describe('Unit: sitemap/manager', function () { sinon.stub(IndexGenerator.prototype, 'getXml'); }); - afterEach(function () { + after(function () { sinon.restore(); }); describe('SiteMapManager', function () { let manager; - let fake; - beforeEach(function () { + before(function () { manager = makeStubManager(); - fake = sinon.stub(); - }); - - it('create SiteMapManager with defaults', function () { - const siteMapManager = new SiteMapManager(); - should.exist(siteMapManager.posts); - should.exist(siteMapManager.pages); - should.exist(siteMapManager.users); - should.exist(siteMapManager.tags); }); it('can create a SiteMapManager instance', function () { @@ -107,6 +103,17 @@ describe('Unit: sitemap/manager', function () { PostGenerator.prototype.removeUrl.calledOnce.should.be.true(); }); + + it('Listens to URLResourceUpdatedEvent event', async function () { + sinon.stub(PostGenerator.prototype, 'updateURL').resolves(true); + DomainEvents.dispatch(URLResourceUpdatedEvent.create({ + id: 'post_id', + resourceType: 'posts' + })); + await DomainEvents.allSettled(); + + assert.ok(PostGenerator.prototype.updateURL.calledOnce); + }); }); it('fn: getSiteMapXml', function () {