UNPKG

eventric

Version:

Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.

87 lines (80 loc) 3.33 kB
describe('Subscribe to event with aggregate id Feature', function() { return describe('given we created and initialized some example context including an aggregate', function() { var exampleContext; exampleContext = null; beforeEach(function(done) { var Example; exampleContext = eventric.context('exampleContext'); exampleContext.addAggregate('Example', Example = (function() { function Example() {} return Example; })(), exampleContext.defineDomainEvent('ExampleCreated', function() {}), exampleContext.defineDomainEvent('SomethingHappened', function() {}), Example = (function() { function Example() {} Example.prototype.create = function(callback) { this.$emitDomainEvent('ExampleCreated'); return callback(); }; Example.prototype.doSomething = function() { return this.$emitDomainEvent('SomethingHappened'); }; return Example; })()); exampleContext.addAggregate('Example', Example); exampleContext.addCommandHandlers({ CreateExample: function(params, callback) { var exampleId; exampleId = null; return this.$repository('Example').create().then((function(_this) { return function(exampleId) { return _this.$repository('Example').save(exampleId); }; })(this)).then(function(exampleId) { return callback(null, exampleId); }); }, DoSomething: function(params, callback) { return this.$repository('Example').findById(params.id).then((function(_this) { return function(example) { example.doSomething(); return _this.$repository('Example').save(params.id); }; })(this)).then(function() { return callback(); }); } }); return exampleContext.initialize(function() { exampleContext.enableWaitingMode(); return done(); }); }); return describe('when we subscribe to an event with a specific aggregate id', function() { it('should notify the domain event subscriber if the aggregate id matches', function() { var handlerFn; handlerFn = sandbox.spy(); return exampleContext.command('CreateExample').then(function(exampleId) { exampleContext.subscribeToDomainEventWithAggregateId('SomethingHappened', exampleId, handlerFn); return exampleContext.command('DoSomething', { id: exampleId }); }).then(function() { return expect(handlerFn).to.have.been.called; }); }); return it('should not notify the domain event subscriber if the aggregate does not match', function() { var handlerFn; handlerFn = sandbox.spy(); return exampleContext.command('CreateExample').then(function(exampleId) { var anotherId; anotherId = exampleId + '12345'; exampleContext.subscribeToDomainEventWithAggregateId('SomethingHappened', anotherId, handlerFn); return exampleContext.command('DoSomething', { id: exampleId }); }).then(function() { return expect(handlerFn).not.to.have.been.called; }); }); }); }); });