UNPKG

eventric

Version:

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

107 lines (102 loc) 3.92 kB
describe('Waiting Command Aggregate Feature', function() { return describe('given we created and initialized a context with an aggregate', function() { var exampleContext; exampleContext = null; beforeEach(function() { var Example; Example = (function() { function Example() {} Example.prototype.create = function(callback) { this.$emitDomainEvent('ExampleCreated', {}); return callback(); }; Example.prototype.handleExampleCreated = function(domainEvent) {}; return Example; })(); exampleContext = eventric.context('exampleContext'); exampleContext.defineDomainEvent('ExampleCreated', function() {}); 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); }); } }); return exampleContext.enableWaitingMode(); }); describe('and an according projection and query handler', function() { beforeEach(function(done) { exampleContext.addProjection('ExampleProjection', function() { return { stores: ['inmemory'], handleExampleCreated: function(domainEvent, done) { return setTimeout((function(_this) { return function() { _this.$store.inmemory.exampleCreated = true; return done(); }; })(this), 500); } }; }); exampleContext.addQueryHandler('getExample', function(params, callback) { return this.$projectionStore('inmemory', 'ExampleProjection', function(err, projectionStore) { return callback(null, projectionStore); }); }); return exampleContext.initialize(function() { return done(); }); }); return describe('when we enable waiting mode and send a command', function() { return it('should wait for the projection to be updated before returning from the command', function(done) { return exampleContext.command('CreateExample', {}).then(function() { return exampleContext.query('getExample', {}); }).then(function(projectionStore) { return setTimeout(function() { expect(projectionStore.exampleCreated).to.be["true"]; return done(); }, 0); }); }); }); }); return describe('and an async domain event handler', function() { var asyncOperation; asyncOperation = null; beforeEach(function(done) { var handler; asyncOperation = sandbox.spy(); handler = function(domainEvent, done) { return setTimeout(function() { asyncOperation(); return done(); }, 500); }; exampleContext.subscribeToDomainEvent('ExampleCreated', handler, { isAsync: true }); return exampleContext.initialize(function() { return done(); }); }); return describe('when we enable waiting mode and send a command', function() { return it('should wait for the domain event handler to be finished before returning from the command', function(done) { return exampleContext.command('CreateExample', {}).then(function() { return setTimeout(function() { expect(asyncOperation).to.have.been.called; return done(); }, 0); }); }); }); }); }); });