2023-06-28 13:22:13 +03:00
|
|
|
import assert from 'assert/strict';
|
|
|
|
import events from 'events';
|
2023-07-19 15:26:30 +03:00
|
|
|
import sinon from 'sinon';
|
2023-06-28 13:22:13 +03:00
|
|
|
import DomainEvents from '@tryghost/domain-events';
|
2023-07-26 13:39:33 +03:00
|
|
|
import {
|
2023-09-15 08:08:38 +03:00
|
|
|
PostDeletedEvent
|
|
|
|
} from '@tryghost/post-events';
|
|
|
|
import {
|
2023-06-28 13:22:13 +03:00
|
|
|
PostEditedEvent,
|
2023-07-26 12:17:59 +03:00
|
|
|
PostAddedEvent,
|
|
|
|
TagDeletedEvent
|
2023-07-26 13:39:33 +03:00
|
|
|
} from '@tryghost/collections';
|
2023-06-28 13:22:13 +03:00
|
|
|
|
|
|
|
import {ModelToDomainEventInterceptor} from '../src';
|
|
|
|
|
|
|
|
class EventRegistry extends events.EventEmitter {
|
|
|
|
hasRegisteredListener(eventName: string, listenerName: string) {
|
|
|
|
return !!(this.listeners(eventName).find(listener => (listener.name === listenerName)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('ModelToDomainEventInterceptor', function () {
|
|
|
|
it('Can instantiate a ModelToDomainEventInterceptor', function () {
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: new EventRegistry(),
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.ok(modelToDomainEventInterceptor);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Starts event listeners after initialization', function () {
|
|
|
|
let eventRegistry = new EventRegistry();
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: eventRegistry,
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
|
|
|
modelToDomainEventInterceptor.init();
|
|
|
|
|
|
|
|
assert.ok(eventRegistry.hasRegisteredListener('post.added', 'post.added.domainEventInterceptorListener'), 'post.added listener is registered');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Intercepts post.added Model event and dispatches PostAddedEvent Domain event', async function () {
|
|
|
|
let eventRegistry = new EventRegistry();
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: eventRegistry,
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
|
|
|
modelToDomainEventInterceptor.init();
|
|
|
|
|
|
|
|
let interceptedEvent;
|
|
|
|
DomainEvents.subscribe(PostAddedEvent, (event: any) => {
|
|
|
|
assert.equal(event.id, '1234-added');
|
|
|
|
interceptedEvent = event;
|
|
|
|
});
|
|
|
|
|
|
|
|
eventRegistry.emit('post.added', {
|
|
|
|
id: '1234-added',
|
|
|
|
attributes: {
|
|
|
|
status: 'draft',
|
|
|
|
featured: false,
|
|
|
|
published_at: new Date()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await DomainEvents.allSettled();
|
|
|
|
|
|
|
|
assert.ok(interceptedEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Intercepts post.edited Model event and dispatches PostEditedEvent Domain event', async function () {
|
|
|
|
let eventRegistry = new EventRegistry();
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: eventRegistry,
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
|
|
|
modelToDomainEventInterceptor.init();
|
|
|
|
|
|
|
|
let interceptedEvent;
|
|
|
|
DomainEvents.subscribe(PostEditedEvent, async (event: any) => {
|
|
|
|
assert.equal(event.id, '1234-edited');
|
|
|
|
assert.ok(event.data);
|
|
|
|
assert.ok(event.data.current);
|
|
|
|
assert.equal(event.data.current.status, 'draft');
|
2023-07-18 11:50:07 +03:00
|
|
|
assert.equal(event.data.previous.status, 'published');
|
2023-06-28 13:22:13 +03:00
|
|
|
|
2023-07-24 16:49:43 +03:00
|
|
|
assert.deepEqual(event.data.current.tags[0], {slug: 'tag-current-slug'});
|
|
|
|
assert.deepEqual(event.data.previous.tags[0], {slug: 'tag-previous-slug'});
|
2023-06-28 13:22:13 +03:00
|
|
|
interceptedEvent = event;
|
|
|
|
});
|
|
|
|
|
|
|
|
eventRegistry.emit('post.edited', {
|
|
|
|
id: '1234-edited',
|
|
|
|
attributes: {
|
|
|
|
status: 'draft',
|
|
|
|
featured: false,
|
|
|
|
published_at: new Date()
|
2023-07-18 11:50:07 +03:00
|
|
|
},
|
|
|
|
_previousAttributes: {
|
|
|
|
status: 'published',
|
|
|
|
featured: true
|
|
|
|
},
|
|
|
|
relations: {
|
|
|
|
tags: {
|
|
|
|
models: [{
|
|
|
|
get: function (key: string) {
|
|
|
|
return `tag-current-${key}`;
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_previousRelations: {
|
|
|
|
tags: {
|
|
|
|
models: [{
|
|
|
|
get: function (key: string) {
|
|
|
|
return `tag-previous-${key}`;
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
2023-06-28 13:22:13 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await DomainEvents.allSettled();
|
|
|
|
|
|
|
|
assert.ok(interceptedEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Intercepts post.deleted Model event and dispatches PostAddedEvent Domain event', async function () {
|
|
|
|
let eventRegistry = new EventRegistry();
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: eventRegistry,
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
|
|
|
modelToDomainEventInterceptor.init();
|
|
|
|
|
|
|
|
let interceptedEvent;
|
|
|
|
DomainEvents.subscribe(PostDeletedEvent, (event: any) => {
|
|
|
|
assert.equal(event.id, '1234-deleted');
|
|
|
|
interceptedEvent = event;
|
|
|
|
});
|
|
|
|
|
|
|
|
eventRegistry.emit('post.deleted', {
|
|
|
|
id: '1234-deleted'
|
|
|
|
});
|
|
|
|
|
|
|
|
await DomainEvents.allSettled();
|
|
|
|
|
|
|
|
assert.ok(interceptedEvent);
|
|
|
|
});
|
|
|
|
|
2023-09-14 08:18:38 +03:00
|
|
|
it('Intercepts post.deleted Model event without an id property and dispatches PostAddedEvent Domain event', async function () {
|
|
|
|
let eventRegistry = new EventRegistry();
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: eventRegistry,
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
|
|
|
modelToDomainEventInterceptor.init();
|
|
|
|
|
|
|
|
let interceptedEvent;
|
|
|
|
DomainEvents.subscribe(PostDeletedEvent, (event: any) => {
|
|
|
|
assert.equal(event.id, '1234-deleted');
|
|
|
|
interceptedEvent = event;
|
|
|
|
});
|
|
|
|
|
|
|
|
eventRegistry.emit('post.deleted', {
|
|
|
|
_previousAttributes: {
|
|
|
|
id: '1234-deleted'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await DomainEvents.allSettled();
|
|
|
|
|
|
|
|
assert.ok(interceptedEvent);
|
|
|
|
});
|
|
|
|
|
2023-07-26 12:17:59 +03:00
|
|
|
it('Intercepts tag.deleted Model event and dispatches TagDeletedEvent Domain event', async function () {
|
|
|
|
let eventRegistry = new EventRegistry();
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: eventRegistry,
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
|
|
|
modelToDomainEventInterceptor.init();
|
|
|
|
|
|
|
|
let interceptedEvent;
|
|
|
|
DomainEvents.subscribe(TagDeletedEvent, (event: TagDeletedEvent) => {
|
2023-09-14 08:45:41 +03:00
|
|
|
assert.equal(event.id, '1234-deleted');
|
|
|
|
assert.equal(event.data.slug, 'tag-slug');
|
|
|
|
interceptedEvent = event;
|
|
|
|
});
|
|
|
|
|
|
|
|
eventRegistry.emit('tag.deleted', {
|
|
|
|
_previousAttributes: {
|
|
|
|
id: '1234-deleted',
|
|
|
|
slug: 'tag-slug'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await DomainEvents.allSettled();
|
|
|
|
|
|
|
|
assert.ok(interceptedEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Intercepts tag.deleted Model event without an id property and dispatches TagDeletedEvent Domain event', async function () {
|
|
|
|
let eventRegistry = new EventRegistry();
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: eventRegistry,
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
|
|
|
modelToDomainEventInterceptor.init();
|
|
|
|
|
|
|
|
let interceptedEvent;
|
|
|
|
DomainEvents.subscribe(TagDeletedEvent, (event: TagDeletedEvent) => {
|
2023-07-26 12:17:59 +03:00
|
|
|
assert.equal(event.id, '1234-deleted');
|
|
|
|
assert.equal(event.data.slug, 'tag-slug');
|
|
|
|
interceptedEvent = event;
|
|
|
|
});
|
|
|
|
|
|
|
|
eventRegistry.emit('tag.deleted', {
|
|
|
|
id: '1234-deleted',
|
|
|
|
attributes: {
|
|
|
|
slug: 'tag-slug'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await DomainEvents.allSettled();
|
|
|
|
|
|
|
|
assert.ok(interceptedEvent);
|
|
|
|
});
|
|
|
|
|
2023-07-19 15:26:30 +03:00
|
|
|
it('Intercepts unmapped Model event and dispatches nothing', async function () {
|
2023-06-28 13:22:13 +03:00
|
|
|
let eventRegistry = new EventRegistry();
|
|
|
|
const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({
|
|
|
|
ModelEvents: eventRegistry,
|
|
|
|
DomainEvents: DomainEvents
|
|
|
|
});
|
|
|
|
|
2023-07-19 15:26:30 +03:00
|
|
|
const domainEventsSpy = sinon.spy(DomainEvents, 'dispatch');
|
2023-06-28 13:22:13 +03:00
|
|
|
|
2023-07-19 15:26:30 +03:00
|
|
|
modelToDomainEventInterceptor.init();
|
2023-06-28 13:22:13 +03:00
|
|
|
|
2023-07-19 15:26:30 +03:00
|
|
|
eventRegistry.emit('tag.added', {
|
|
|
|
id: '1234-tag'
|
2023-06-28 13:22:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
await DomainEvents.allSettled();
|
|
|
|
|
2023-07-19 15:26:30 +03:00
|
|
|
assert.equal(domainEventsSpy.called, false);
|
2023-06-28 13:22:13 +03:00
|
|
|
});
|
|
|
|
});
|