eventric-testing
Version:
Testing helpers for eventric.js
778 lines (662 loc) • 24.8 kB
JavaScript
require.register("eventric-testing/index", function(exports, require, module){
module.exports = require('./src');
});
require.register("eventric-testing/src/aggregate_factory", function(exports, require, module){
var Aggregate, AggregateFactory, Context, eventric, stubFactory;
eventric = require('eventric');
Aggregate = eventric.Aggregate;
Context = eventric.Context;
stubFactory = require('./stub_factory');
AggregateFactory = (function() {
function AggregateFactory() {}
AggregateFactory.prototype.fakeAggregate = function(AggregateClass) {
var aggregate;
aggregate = new AggregateClass;
aggregate.$emitDomainEvent = stubFactory.stub();
return aggregate;
};
AggregateFactory.prototype.wiredAggregate = function(AggregateClass, domainEvents) {
var aggregate;
aggregate = this.instantiateAggregateWithFakeContext(AggregateClass, domainEvents);
return aggregate.instance;
};
AggregateFactory.prototype.instantiateAggregateWithFakeContext = function(AggregateClass, domainEvents) {
var fakeContext;
fakeContext = this._createFakeContext(domainEvents);
return new Aggregate(fakeContext, eventric, 'Aggregate', AggregateClass);
};
AggregateFactory.prototype._createFakeContext = function(domainEvents) {
var contextFake;
contextFake = {
_eventric: eventric,
name: 'eventric-testing'
};
return {
name: contextFake.name,
getDomainEvent: function(name) {
return domainEvents[name];
},
createDomainEvent: function() {
return Context.prototype.createDomainEvent.apply(contextFake, arguments);
}
};
};
return AggregateFactory;
})();
module.exports = new AggregateFactory;
});
require.register("eventric-testing/src/command_query_factory", function(exports, require, module){
var CommandQueryFactory, fakePromise, projectionFactory, stubFactory;
stubFactory = require('./stub_factory');
fakePromise = require('./fake_promise');
projectionFactory = require('./projection_factory');
CommandQueryFactory = (function() {
function CommandQueryFactory() {}
CommandQueryFactory.prototype.wiredCommandHandler = function(commandHandler) {
return this._wireHandler(commandHandler);
};
CommandQueryFactory.prototype.wiredQueryHandler = function(queryHandler) {
return this._wireHandler(queryHandler);
};
CommandQueryFactory.prototype._wireHandler = function(handler) {
var di, key;
di = {
$adapter: stubFactory.stub(),
$aggregate: {
create: stubFactory.stub(),
load: stubFactory.stub()
},
$domainService: stubFactory.stub(),
$query: stubFactory.stub(),
$projectionStore: stubFactory.stub(),
$emitDomainEvent: stubFactory.stub()
};
stubFactory.configureReturnValue(di.$aggregate.create, this.aggregateStub());
stubFactory.configureReturnValue(di.$aggregate.load, this.aggregateStub());
stubFactory.configureReturnValue(di.$projectionStore, projectionFactory.mongoDbStoreStub());
handler = handler.bind(di);
for (key in di) {
handler[key] = di[key];
}
return handler;
};
CommandQueryFactory.prototype.aggregateStub = function() {
var saveStub;
saveStub = stubFactory.stub();
stubFactory.configureReturnValue(saveStub, fakePromise.resolve());
return {
$save: saveStub
};
};
CommandQueryFactory.prototype.waitForQueryToReturnResult = function(context, queryName, params, timeout) {
if (timeout == null) {
timeout = 5000;
}
return this.waitForResult(function() {
return context.query(queryName, params);
}, timeout)["catch"](function(error) {
var ref;
if ((error != null ? (ref = error.message) != null ? ref.indexOf('waitForResult') : void 0 : void 0) > -1) {
throw new Error("waitForQueryToReturnResult timed out for query '" + queryName + "' on context '" + context.name + "'\nwith params " + (JSON.stringify(params)));
} else {
throw error;
}
});
};
CommandQueryFactory.prototype.waitForCommandToResolve = function(context, commandName, params, timeout) {
if (timeout == null) {
timeout = 5000;
}
return this.waitForResult(function() {
return context.command(commandName, params).then(function(result) {
return result || true;
})["catch"](function() {
return void 0;
});
}, timeout)["catch"](function() {
throw new Error("waitForCommandToResolve timed out for command '" + commandName + "' on context '" + context.name + "'\nwith params " + (JSON.stringify(params)));
});
};
CommandQueryFactory.prototype.waitForResult = function(promiseFactory, timeout) {
if (timeout == null) {
timeout = 5000;
}
return new Promise(function(resolve, reject) {
var pollPromise, startTime;
startTime = new Date();
pollPromise = function() {
return promiseFactory().then(function(result) {
var timeoutExceeded;
if (result != null) {
resolve(result);
return;
}
timeoutExceeded = (new Date() - startTime) >= timeout;
if (!timeoutExceeded) {
setTimeout(pollPromise, 15);
return;
}
return reject(new Error("waitForResult timed out for '" + (promiseFactory.toString()) + "'"));
})["catch"](reject);
};
return pollPromise();
});
};
return CommandQueryFactory;
})();
module.exports = new CommandQueryFactory;
});
require.register("eventric-testing/src/domain_event_factory", function(exports, require, module){
var DomainEvent, DomainEventFactory, eventric;
eventric = require('eventric');
DomainEvent = eventric.DomainEvent;
DomainEventFactory = (function() {
function DomainEventFactory() {}
DomainEventFactory.prototype.createDomainEvent = function(contextName, domainEventName, DomainEventClass, aggregateId, domainEventPayload) {
var payload;
payload = {};
DomainEventClass.apply(payload, [domainEventPayload]);
return new DomainEvent({
id: eventric.generateUid(),
name: domainEventName,
aggregate: {
id: aggregateId,
name: 'eventric-testing'
},
context: contextName,
payload: payload
});
};
return DomainEventFactory;
})();
module.exports = new DomainEventFactory;
});
require.register("eventric-testing/src/fake_promise", function(exports, require, module){
var FakePromise, _isPromise,
slice = [].slice;
_isPromise = function(promise) {
return promise && typeof promise.then === 'function' && typeof promise["catch"] === 'function';
};
FakePromise = (function() {
function FakePromise() {}
FakePromise.prototype.resolve = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return {
then: function(callback) {
var promise;
if (callback == null) {
callback = function() {};
}
promise = callback.apply(this, args);
if (_isPromise(promise)) {
return promise;
} else {
return this;
}
},
"catch": function() {
return this;
}
};
};
FakePromise.prototype.reject = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return {
then: function() {
return this;
},
"catch": function(callback) {
var promise;
if (callback == null) {
callback = function() {};
}
promise = callback.apply(this, args);
if (_isPromise(promise)) {
promise;
} else {
this;
}
return this;
}
};
};
FakePromise.prototype.resolveAsync = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return {
then: function(callback) {
if (callback == null) {
callback = function() {};
}
setTimeout(function() {
return callback.apply(this, args);
}, 0);
return this;
},
"catch": function() {
return this;
}
};
};
FakePromise.prototype.rejectAsync = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return {
then: function() {
return this;
},
"catch": function(callback) {
if (callback == null) {
callback = function() {};
}
setTimeout(function() {
return callback.apply(this, args);
}, 0);
return this;
}
};
};
return FakePromise;
})();
module.exports = new FakePromise;
});
require.register("eventric-testing/src/index", function(exports, require, module){
var aggregateFactory, commandQueryFactory, domainEventFactory, eventric, fakePromise, projectionFactory, remoteFactory, stubFactory, wiredRemotes,
slice = [].slice;
eventric = require('eventric');
stubFactory = require('./stub_factory');
aggregateFactory = require('./aggregate_factory');
fakePromise = require('./fake_promise');
commandQueryFactory = require('./command_query_factory');
projectionFactory = require('./projection_factory');
remoteFactory = require('./remote_factory');
domainEventFactory = require('./domain_event_factory');
wiredRemotes = [];
eventric.testing = {
setStubMethods: function(stubMethod, configureReturnValueMethod) {
stubFactory.setStubMethod(stubMethod);
return stubFactory.setConfigureReturnValueMethod(configureReturnValueMethod);
},
resolve: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return fakePromise.resolve.apply(fakePromise, args);
},
reject: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return fakePromise.reject.apply(fakePromise, args);
},
resolveAsync: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return fakePromise.resolveAsync.apply(fakePromise, args);
},
rejectAsync: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return fakePromise.rejectAsync.apply(fakePromise, args);
},
fakeAggregate: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return aggregateFactory.fakeAggregate.apply(aggregateFactory, args);
},
wiredAggregate: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return aggregateFactory.wiredAggregate.apply(aggregateFactory, args);
},
wiredCommandHandler: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return commandQueryFactory.wiredCommandHandler.apply(commandQueryFactory, args);
},
wiredQueryHandler: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return commandQueryFactory.wiredQueryHandler.apply(commandQueryFactory, args);
},
wiredProjection: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return projectionFactory.wiredProjection.apply(projectionFactory, args);
},
aggregateStub: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return commandQueryFactory.aggregateStub.apply(commandQueryFactory, args);
},
wiredRemote: function() {
var args, wiredRemote;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
wiredRemote = remoteFactory.wiredRemote.apply(remoteFactory, args);
wiredRemotes.push(wiredRemote);
return wiredRemote;
},
projectionStoreMongoDbStub: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return projectionFactory.mongoDbStoreStub.apply(projectionFactory, args);
},
createDomainEvent: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return domainEventFactory.createDomainEvent.apply(domainEventFactory, args);
},
destroy: function() {
var i, len, wiredRemote;
for (i = 0, len = wiredRemotes.length; i < len; i++) {
wiredRemote = wiredRemotes[i];
wiredRemote.$destroy();
}
return wiredRemotes = [];
},
waitForQueryToReturnResult: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return commandQueryFactory.waitForQueryToReturnResult.apply(commandQueryFactory, args);
},
waitForCommandToResolve: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return commandQueryFactory.waitForCommandToResolve.apply(commandQueryFactory, args);
},
waitForResult: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return commandQueryFactory.waitForResult.apply(commandQueryFactory, args);
}
};
module.exports = eventric.testing;
});
require.register("eventric-testing/src/projection_factory", function(exports, require, module){
var ProjectionFactory, aggregateFactory, stubFactory,
slice = [].slice;
aggregateFactory = require('./aggregate_factory');
stubFactory = require('./stub_factory');
ProjectionFactory = (function() {
function ProjectionFactory() {}
ProjectionFactory.prototype.wiredProjection = function() {
var ProjectionClass, arg, domainEvents, i, projection, projectionParams;
ProjectionClass = arguments[0], arg = 3 <= arguments.length ? slice.call(arguments, 1, i = arguments.length - 1) : (i = 1, []), domainEvents = arguments[i++];
projectionParams = arg[0];
projection = this._instantiateProjection(ProjectionClass);
this._initializeProjection(projection, projectionParams);
this._wireProjection(projection, domainEvents);
return projection;
};
ProjectionFactory.prototype._instantiateProjection = function(ProjectionClass) {
var projection;
projection = new ProjectionClass;
projection.$store = {
mongodb: this.mongoDbStoreStub()
};
projection.$adapter = stubFactory.stub();
return projection;
};
ProjectionFactory.prototype._initializeProjection = function(projection, params) {
if (projection.initialize) {
projection.$subscribeHandlersWithAggregateId = function(aggregateId) {
return projection.__subscribedAggregateId = aggregateId;
};
return projection.initialize(params, function() {});
}
};
ProjectionFactory.prototype._wireProjection = function(projection, domainEvents) {
var FakeAggregateClass, fakeAggregate, originalHandleDomainEvent;
FakeAggregateClass = (function() {
function FakeAggregateClass() {}
return FakeAggregateClass;
})();
fakeAggregate = aggregateFactory.instantiateAggregateWithFakeContext(FakeAggregateClass, domainEvents);
originalHandleDomainEvent = fakeAggregate._handleDomainEvent;
fakeAggregate._handleDomainEvent = function() {
return originalHandleDomainEvent.apply({
instance: projection
}, arguments);
};
projection.$emitDomainEvent = function(eventName, aggregateId, payload) {
fakeAggregate.id = aggregateId;
if (projection.__subscribedAggregateId && projection.__subscribedAggregateId !== aggregateId) {
return;
}
if (!projection["handle" + eventName]) {
throw new Error("Domain Event Handler has not subscribed to domain event " + eventName);
}
return fakeAggregate.emitDomainEvent.call(fakeAggregate, eventName, payload);
};
return projection;
};
ProjectionFactory.prototype.mongoDbStoreStub = function() {
var key, projectionStoreMongoDb;
projectionStoreMongoDb = {
find: stubFactory.stub(),
findOne: stubFactory.stub(),
update: stubFactory.stub(),
upsert: stubFactory.stub(),
insert: stubFactory.stub(),
count: stubFactory.stub(),
save: stubFactory.stub(),
remove: stubFactory.stub()
};
for (key in projectionStoreMongoDb) {
stubFactory.configureReturnValue(projectionStoreMongoDb[key], null);
}
return projectionStoreMongoDb;
};
return ProjectionFactory;
})();
module.exports = new ProjectionFactory;
});
require.register("eventric-testing/src/remote_factory", function(exports, require, module){
var RemoteFactory, _, domainEventFactory, eventric, fakePromise, stubFactory;
_ = require('lodash');
eventric = require('eventric');
domainEventFactory = require('./domain_event_factory');
stubFactory = require('./stub_factory');
fakePromise = require('./fake_promise');
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.addClient('inmemory', eventric.RemoteInMemory.client);
wiredRemote.set('default client', 'inmemory');
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 = eventric.RemoteInMemory.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 domainEventFactory.createDomainEvent(contextName, domainEventName, DomainEventClass, aggregateId, domainEventPayload);
};
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;
});
require.register("eventric-testing/src/spec_setup", function(exports, require, module){
var root, sinonChai;
require('es6-promise').polyfill();
if (typeof window !== 'undefined') {
root = window;
} else {
root = global;
}
if (!root._spec_setup) {
root.sinon = require('sinon');
root.mockery = require('mockery');
root.chai = require('chai');
root.expect = chai.expect;
root.sandbox = sinon.sandbox.create();
sinonChai = require('sinon-chai');
chai.use(sinonChai);
root._spec_setup = true;
}
afterEach(function() {
return sandbox.restore();
});
});
require.register("eventric-testing/src/stub_factory", function(exports, require, module){
var StubFactory;
StubFactory = (function() {
function StubFactory() {
this.stub = function() {
return function() {};
};
this.configureReturnValue = function() {
throw new Error('configureReturnValue not configured');
};
}
StubFactory.prototype.setStubMethod = function(stub) {
this.stub = stub;
};
StubFactory.prototype.setConfigureReturnValueMethod = function(configureReturnValue) {
this.configureReturnValue = configureReturnValue;
};
return StubFactory;
})();
module.exports = new StubFactory;
});