UNPKG

eventric

Version:

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

109 lines (100 loc) 3.91 kB
describe('Command Aggregate 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(params) { this.someId = params.someId; this.someProp = params.someProp; return this.entity = params.entity; }), Example = (function() { function Example() {} Example.prototype.create = function(callback) { this.$emitDomainEvent('ExampleCreated'); return callback(); }; Example.prototype.doSomething = function(someId) { return this.$emitDomainEvent('SomethingHappened', { someId: someId, someProp: 'foo' }); }; Example.prototype.handleExampleCreated = function() { return this.entities = []; }; Example.prototype.handleSomethingHappened = function(domainEvent) { this.someId = domainEvent.payload.someId; return this.someProp = domainEvent.payload.someProp; }; 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([1]); return _this.$repository('Example').save(params.id); }; })(this)).then(function() { return callback(); }); } }); return exampleContext.initialize(function() { return done(); }); }); describe('when we send a command to the context', function() { return it('then it should have triggered the correct DomainEvent', function(done) { exampleContext.subscribeToDomainEvent('SomethingHappened', function(domainEvent) { expect(domainEvent.payload.someProp).to.equal('foo'); expect(domainEvent.name).to.equal('SomethingHappened'); return done(); }); return exampleContext.command('CreateExample').then(function(exampleId) { return exampleContext.command('DoSomething', { id: exampleId }); }); }); }); return describe('when we send multiple commands to the context', function() { return it('then it should execute all commands as expected', function(done) { var commandCount; commandCount = 0; exampleContext.subscribeToDomainEvent('SomethingHappened', function(domainEvent) { commandCount++; if (commandCount === 2) { expect(commandCount).to.equal(2); return done(); } }); return exampleContext.command('CreateExample').then(function(exampleId) { exampleContext.command('DoSomething', { id: exampleId }); return exampleContext.command('DoSomething', { id: exampleId }); }); }); }); }); });