eventric-testing
Version:
Testing helpers for eventric.js
175 lines (165 loc) • 7.14 kB
JavaScript
var RemoteFactory, _, eventric, fakePromise, inmemoryRemote;
_ = require('lodash');
eventric = require('eventric');
fakePromise = require('./fake_promise');
inmemoryRemote = require('eventric-remote-inmemory');
RemoteFactory = (function() {
function RemoteFactory() {}
RemoteFactory.prototype.wiredRemote = function(contextName, domainEvents) {
var originalCommand, originalSubscribeToAllDomainEvents, originalSubscribeToDomainEvent, originalSubscribeToDomainEventWithAggregateId, wiredRemote;
wiredRemote = eventric.remote(contextName);
wiredRemote._mostCurrentEmitOperation = fakePromise.resolve();
wiredRemote._domainEvents = [];
wiredRemote._subscriberIds = [];
wiredRemote._commandStubs = [];
wiredRemote._context = eventric.context(contextName);
wiredRemote.setClient(inmemoryRemote.client);
wiredRemote.$populateWithDomainEvent = function(domainEventName, aggregateId, domainEventPayload) {
return this._domainEvents.push(this._createDomainEvent(domainEventName, aggregateId, domainEventPayload));
};
wiredRemote.$emitDomainEvent = function(domainEventName, aggregateId, domainEventPayload) {
var domainEvent, endpoint;
domainEvent = this._createDomainEvent(domainEventName, aggregateId, domainEventPayload);
this._domainEvents.push(domainEvent);
endpoint = inmemoryRemote.endpoint;
return this._mostCurrentEmitOperation = this._mostCurrentEmitOperation.then(function() {
var contextAndNameEventPublish, contextEventPublish, fullEventNamePublish;
contextEventPublish = endpoint.publish(contextName, domainEvent);
contextAndNameEventPublish = endpoint.publish(contextName, domainEvent.name, domainEvent);
if (domainEvent.aggregate) {
fullEventNamePublish = endpoint.publish(contextName, domainEvent.name, domainEvent.aggregate.id, domainEvent);
return Promise.all([contextEventPublish, contextAndNameEventPublish, fullEventNamePublish]);
} else {
return Promise.all([contextEventPublish, contextAndNameEventPublish]);
}
});
};
wiredRemote.$waitForEmitDomainEvent = function() {
return wiredRemote._mostCurrentEmitOperation;
};
wiredRemote._createDomainEvent = function(domainEventName, aggregateId, domainEventPayload) {
var DomainEventClass;
DomainEventClass = domainEvents[domainEventName];
if (!DomainEventClass) {
throw new Error('Trying to populate wired remote with unknown domain event ' + domainEventName);
}
return wiredRemote._context.createDomainEvent(domainEventName, DomainEventClass, domainEventPayload, {
id: aggregateId
});
};
wiredRemote.findDomainEventsByName = function(names) {
if (!(names instanceof Array)) {
names = [names];
}
return fakePromise.resolve(this._domainEvents.filter(function(x) {
return names.indexOf(x.name) > -1;
}));
};
wiredRemote.findDomainEventsByNameAndAggregateId = function(names, aggregateIds) {
if (!(names instanceof Array)) {
names = [names];
}
if (!(aggregateIds instanceof Array)) {
aggregateIds = [aggregateIds];
}
return fakePromise.resolve(this._domainEvents.filter(function(x) {
return names.indexOf(x.name) > -1 && x.aggregate && aggregateIds.indexOf(x.aggregate.id) > -1;
}));
};
originalSubscribeToAllDomainEvents = wiredRemote.subscribeToAllDomainEvents;
wiredRemote.subscribeToAllDomainEvents = function() {
return originalSubscribeToAllDomainEvents.apply(this, arguments).then((function(_this) {
return function(subscriberId) {
_this._subscriberIds.push(subscriberId);
return subscriberId;
};
})(this));
};
originalSubscribeToDomainEvent = wiredRemote.subscribeToDomainEvent;
wiredRemote.subscribeToDomainEvent = function() {
return originalSubscribeToDomainEvent.apply(this, arguments).then((function(_this) {
return function(subscriberId) {
_this._subscriberIds.push(subscriberId);
return subscriberId;
};
})(this));
};
originalSubscribeToDomainEventWithAggregateId = wiredRemote.subscribeToDomainEventWithAggregateId;
wiredRemote.subscribeToDomainEventWithAggregateId = function() {
return originalSubscribeToDomainEventWithAggregateId.apply(this, arguments).then((function(_this) {
return function(subscriberId) {
_this._subscriberIds.push(subscriberId);
return subscriberId;
};
})(this));
};
wiredRemote.$destroy = function() {
this._domainEvents = [];
this._commandStubs = [];
return this._mostCurrentEmitOperation.then((function(_this) {
return function() {
var i, len, ref, subscriberId, subscriptionRemovals;
_this._mostCurrentEmitOperation = fakePromise.resolve();
subscriptionRemovals = [];
ref = _this._subscriberIds;
for (i = 0, len = ref.length; i < len; i++) {
subscriberId = ref[i];
subscriptionRemovals.push(wiredRemote.unsubscribeFromDomainEvent(subscriberId));
}
_this._subscriberIds = [];
return Promise.all(subscriptionRemovals);
};
})(this));
};
wiredRemote.$onCommand = function(command, payload) {
var commandStub;
commandStub = {
command: command,
payload: payload,
domainEvents: [],
yieldsDomainEvent: function(eventName, aggregateId, payload) {
this.domainEvents.push({
eventName: eventName,
aggregateId: aggregateId,
payload: payload
});
return this;
}
};
this._commandStubs.push(commandStub);
return commandStub;
};
originalCommand = wiredRemote.command;
wiredRemote.command = function(command, payload) {
var domainEvent, emitDomainEventAsync, filteredCommandStub, filteredCommandStubs, i, j, len, len1, ref;
filteredCommandStubs = this._commandStubs.filter(function(commandStub) {
if (command !== commandStub.command) {
return false;
}
return _.isEqual(payload, commandStub.payload);
});
if (!filteredCommandStubs.length) {
return originalCommand.apply(this, arguments);
}
emitDomainEventAsync = (function(_this) {
return function(domainEvent) {
return setTimeout(function() {
return _this.$emitDomainEvent(domainEvent.eventName, domainEvent.aggregateId, domainEvent.payload);
});
};
})(this);
for (i = 0, len = filteredCommandStubs.length; i < len; i++) {
filteredCommandStub = filteredCommandStubs[i];
ref = filteredCommandStub.domainEvents;
for (j = 0, len1 = ref.length; j < len1; j++) {
domainEvent = ref[j];
emitDomainEventAsync(domainEvent);
}
}
return fakePromise.resolve();
};
return wiredRemote;
};
return RemoteFactory;
})();
module.exports = new RemoteFactory;