eventric
Version:
Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.
111 lines (110 loc) • 4.42 kB
JavaScript
describe('EventBus', function() {
var eventBus, pubSubStub;
eventBus = null;
pubSubStub = null;
beforeEach(function() {
var EventBus;
pubSubStub = {
subscribe: sandbox.stub(),
subscribeAsync: sandbox.stub(),
publish: sandbox.stub(),
publishAsync: sandbox.stub()
};
mockery.registerMock('eventric/src/pub_sub', sandbox.stub().returns(pubSubStub));
EventBus = require('eventric/src/event_bus');
return eventBus = new EventBus;
});
describe('#subscribeToDomainEvent', function() {
return it('should subscribe to the event with given event name', function() {
var subscriberFn;
subscriberFn = function() {};
eventBus.subscribeToDomainEvent('SomeEvent', subscriberFn);
return expect(pubSubStub.subscribe).to.have.been.calledWith('SomeEvent', subscriberFn);
});
});
describe('#subscribeToDomainEventWithAggregateId', function() {
return it('should subscribe to the event with given event name and aggregate id', function() {
var subscriberFn;
subscriberFn = function() {};
eventBus.subscribeToDomainEventWithAggregateId('SomeEvent', 12345, subscriberFn);
return expect(pubSubStub.subscribe).to.have.been.calledWith('SomeEvent/12345', subscriberFn);
});
});
describe('#subscribeToAllDomainEvents', function() {
return it('should subscribe to the generic event "DomainEvent"', function() {
var subscriberFn;
subscriberFn = function() {};
eventBus.subscribeToAllDomainEvents(subscriberFn);
return expect(pubSubStub.subscribe).to.have.been.calledWith('DomainEvent', subscriberFn);
});
});
describe('#publishDomainEvent', function() {
beforeEach(function() {});
it('should publish a generic "DomainEvent" event', function() {
var domainEvent;
domainEvent = {
name: 'SomeEvent'
};
eventBus.publishDomainEvent(domainEvent, function() {});
return expect(pubSubStub.publish).to.have.been.calledWith('DomainEvent', domainEvent);
});
it('should then publish the given event', function() {
var domainEvent;
pubSubStub.publish.withArgs('DomainEvent').yields();
domainEvent = {
name: 'SomeEvent'
};
eventBus.publishDomainEvent(domainEvent, function() {});
return expect(pubSubStub.publish).to.have.been.calledWith('SomeEvent', domainEvent);
});
return describe('given an event with an aggregate id', function() {
return it('should publish an aggregate id specific event', function() {
var domainEvent;
pubSubStub.publish.withArgs('DomainEvent').yields();
pubSubStub.publish.withArgs('SomeEvent').yields();
domainEvent = {
name: 'SomeEvent',
aggregate: {
id: 12345
}
};
eventBus.publishDomainEvent(domainEvent, function() {});
return expect(pubSubStub.publish).to.have.been.calledWith('SomeEvent/12345', domainEvent);
});
});
});
return describe('#publishDomainEventAndWait', function() {
it('should publish a generic "DomainEvent" event asynchronously', function() {
var domainEvent;
domainEvent = {
name: 'SomeEvent'
};
eventBus.publishDomainEventAndWait(domainEvent, function() {});
return expect(pubSubStub.publishAsync).to.have.been.calledWith('DomainEvent', domainEvent);
});
it('should then publish the given event asynchronously', function() {
var domainEvent;
pubSubStub.publishAsync.withArgs('DomainEvent').yields();
domainEvent = {
name: 'SomeEvent'
};
eventBus.publishDomainEventAndWait(domainEvent, function() {});
return expect(pubSubStub.publishAsync).to.have.been.calledWith('SomeEvent', domainEvent);
});
return describe('given an event with an aggregate id', function() {
return it('should publish an aggregate id specific event', function() {
var domainEvent;
pubSubStub.publishAsync.withArgs('DomainEvent').yields();
pubSubStub.publishAsync.withArgs('SomeEvent').yields();
domainEvent = {
name: 'SomeEvent',
aggregate: {
id: 12345
}
};
eventBus.publishDomainEventAndWait(domainEvent, function() {});
return expect(pubSubStub.publishAsync).to.have.been.calledWith('SomeEvent/12345', domainEvent);
});
});
});
});