UNPKG

eventric-testing

Version:
91 lines (77 loc) 3.17 kB
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;