UNPKG

eventric

Version:

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

87 lines (86 loc) 3.25 kB
describe('Projection Feature', function() { return describe('given we created and initialized some example context including a Projection', function() { var exampleContext; exampleContext = null; beforeEach(function() { exampleContext = eventric.context('exampleContext'); exampleContext.defineDomainEvents({ ExampleCreated: function() {}, SomethingHappened: function(params) { return this.specific = params.whateverFoo; } }); exampleContext.addProjection('ExampleProjection', function() { return { stores: ['inmemory'], handleSomethingHappened: function(domainEvent, done) { this.$store.inmemory.totallyDenormalized = domainEvent.payload.specific; return done(); } }; }); exampleContext.addAggregate('Example', function() { return { create: function(callback) { this.$emitDomainEvent('ExampleCreated'); return callback(); }, handleExampleCreated: function(domainEvent) { return this.whatever = 'bar'; }, doSomething: function() { if (this.whatever === 'bar') { return this.$emitDomainEvent('SomethingHappened', { whateverFoo: 'foo' }); } }, handleSomethingHappened: function(domainEvent) { return this.whatever = domainEvent.payload.whateverFoo; } }; }); 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); }); }, doSomethingWithExample: 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().then(function() { return exampleContext.enableWaitingMode(); }); }); return describe('when DomainEvents got emitted which the Projection subscribed to', function() { return it('then the Projection should call the projectionStore with the denormalized state', function() { return exampleContext.command('CreateExample').then(function(exampleId) { return exampleContext.command('doSomethingWithExample', { id: exampleId }); }).then(function() { return exampleContext.getProjectionStore('inmemory', 'ExampleProjection', function(err, projectionStore) { return expect(projectionStore).to.deep.equal({ totallyDenormalized: 'foo' }); }); }); }); }); }); });