eventric
Version:
Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.
54 lines (53 loc) • 2.35 kB
JavaScript
describe('Global Domain Event Handler Feature', function() {
return describe('given we created and initialized some example context and added a global domain event handler', function() {
var allHandlerStub, allOncontextHandlerStub, exampleContext, specificOncontextHandlerStub;
exampleContext = null;
specificOncontextHandlerStub = null;
allOncontextHandlerStub = null;
allHandlerStub = null;
beforeEach(function() {
specificOncontextHandlerStub = sandbox.stub();
eventric.subscribeToDomainEvent('exampleContext', 'ExampleCreated', specificOncontextHandlerStub);
allOncontextHandlerStub = sandbox.stub();
eventric.subscribeToDomainEvent('exampleContext', allOncontextHandlerStub);
allHandlerStub = sandbox.stub();
eventric.subscribeToDomainEvent(allHandlerStub);
exampleContext = eventric.context('exampleContext');
exampleContext.defineDomainEvent('ExampleCreated', function() {});
exampleContext.addAggregate('Example', function() {
return {
create: function(callback) {
this.$emitDomainEvent('ExampleCreated');
return callback();
}
};
});
return exampleContext.addCommandHandler('createExample', function(params, done) {
return this.$repository('Example').create().then((function(_this) {
return function(exampleId) {
return _this.$repository('Example').save(exampleId);
};
})(this)).then((function(_this) {
return function() {
return done(null);
};
})(this));
});
});
return describe('when DomainEvents got emitted which the handler subscribed to', function() {
return it('then it should execute the registered global domain event handler', function(done) {
return exampleContext.initialize((function(_this) {
return function() {
exampleContext.subscribeToDomainEvent('ExampleCreated', function() {
expect(specificOncontextHandlerStub).to.have.been.called;
expect(allOncontextHandlerStub).to.have.been.called;
expect(allHandlerStub).to.have.been.called;
return done();
});
return exampleContext.command('createExample');
};
})(this));
});
});
});
});