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
This commit is contained in:
Naz 2023-01-23 15:29:26 +08:00
parent 714a6f6900
commit 4aacd50fee
No known key found for this signature in database

View File

@ -1,7 +1,11 @@
const should = require('should'); const should = require('should');
const sinon = require('sinon'); const sinon = require('sinon');
const assert = require('assert');
// Stuff we are testing // 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 events = require('../../../../../core/server/lib/common/events');
const SiteMapManager = require('../../../../../core/frontend/services/sitemap/manager'); 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}); return new SiteMapManager({posts: posts, pages: pages, tags: tags, authors: authors});
}; };
beforeEach(function () { before(function () {
eventsToRemember = {}; 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) { sinon.stub(events, 'on').callsFake(function (eventName, callback) {
eventsToRemember[eventName] = callback; eventsToRemember[eventName] = callback;
}); });
@ -43,25 +49,15 @@ describe('Unit: sitemap/manager', function () {
sinon.stub(IndexGenerator.prototype, 'getXml'); sinon.stub(IndexGenerator.prototype, 'getXml');
}); });
afterEach(function () { after(function () {
sinon.restore(); sinon.restore();
}); });
describe('SiteMapManager', function () { describe('SiteMapManager', function () {
let manager; let manager;
let fake;
beforeEach(function () { before(function () {
manager = makeStubManager(); 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 () { it('can create a SiteMapManager instance', function () {
@ -107,6 +103,17 @@ describe('Unit: sitemap/manager', function () {
PostGenerator.prototype.removeUrl.calledOnce.should.be.true(); 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 () { it('fn: getSiteMapXml', function () {