eventric
Version:
Build JavaScript applications with Behaviour-driven Domain Design. Based on DDD, BDD, CQRS and EventSourcing.
148 lines (147 loc) • 5.29 kB
JavaScript
describe('PubSub', function() {
var PubSub, pubSub;
PubSub = require('eventric/src/pub_sub');
pubSub = null;
beforeEach(function() {
return pubSub = new PubSub;
});
describe('#subscribe', function() {
it('should return an unique subscriber id', function() {
var subscriberId1, subscriberId2;
subscriberId1 = pubSub.subscribe('SomeEvent', function() {});
subscriberId2 = pubSub.subscribe('SomeEvent', function() {});
expect(subscriberId1).to.be.a('number');
expect(subscriberId2).to.be.a('number');
return expect(subscriberId1).not.to.equal(subscriberId2);
});
return it('should subscribe to the event with given name', function(done) {
var publishedEvent;
publishedEvent = {};
pubSub.subscribe('SomeEvent', function(event) {
expect(event).to.equal(publishedEvent);
return done();
});
return pubSub.publish('SomeEvent', publishedEvent);
});
});
describe('#subscribeAsync', function() {
it('should return an unique subscriber id', function() {
var subscriberId1, subscriberId2;
subscriberId1 = pubSub.subscribeAsync('SomeEvent', function() {});
subscriberId2 = pubSub.subscribeAsync('SomeEvent', function() {});
expect(subscriberId1).to.be.a('number');
expect(subscriberId2).to.be.a('number');
return expect(subscriberId1).not.to.equal(subscriberId2);
});
return it('should subscribe to the event with given name', function(done) {
var publishedEvent;
publishedEvent = {};
pubSub.subscribeAsync('SomeEvent', function(event) {
expect(event).to.equal(publishedEvent);
return done();
});
return pubSub.publish('SomeEvent', publishedEvent);
});
});
describe('#publish', function() {
it('should notify all subscribers in registration order', function(done) {
var executedSubscriber;
executedSubscriber = [];
pubSub.subscribe('SomeEvent', function() {
return executedSubscriber.push('first');
});
pubSub.subscribe('SomeEvent', function() {
executedSubscriber.push('second');
expect(executedSubscriber).to.deep.equal(['first', 'second']);
return done();
});
return pubSub.publish('SomeEvent');
});
return it('should immediately call back even though subscribers may be asynchronous', function(done) {
var handler, spy;
spy = sandbox.spy();
handler = function(event, done) {
return setTimeout(spy, 50);
};
pubSub.subscribeAsync('SomeEvent', handler);
return pubSub.publish('SomeEvent', {}, function() {
expect(spy).not.to.have.been.called;
return done();
});
});
});
describe('#publishAsync', function() {
it('should wait for async subscribers to invoke the done callback before executing the next handler', function(done) {
var greeting, handler1, handler2;
greeting = '';
handler1 = function(event, done) {
return setTimeout(function() {
greeting += 'Hello ';
return done();
}, 50);
};
handler2 = function() {
return greeting += 'World';
};
pubSub.subscribeAsync('SomeEvent', handler1);
pubSub.subscribe('SomeEvent', handler2);
return pubSub.publishAsync('SomeEvent', {}, function() {
expect(greeting).to.equal('Hello World');
return done();
});
});
it('should execute synchronous handlers in series', function(done) {
var handler1, handler2, spy1, spy2;
spy1 = sandbox.spy();
spy2 = sandbox.spy();
handler1 = function() {
return spy1();
};
handler2 = function() {
return spy2();
};
pubSub.subscribeAsync('SomeEvent', handler1);
pubSub.subscribeAsync('SomeEvent', handler2);
return pubSub.publish('SomeEvent', {}, function() {
expect(spy1).to.have.been.called;
expect(spy2).to.have.been.called;
return done();
});
});
return it('should only call back when all handlers have finished', function(done) {
var callCount, handler1, handler2;
callCount = 0;
handler1 = function(event, done) {
return setTimeout(function() {
callCount++;
return done();
}, 25);
};
handler2 = function(event, done) {
return setTimeout(function() {
callCount++;
return done();
}, 25);
};
pubSub.subscribeAsync('SomeEvent', handler1);
pubSub.subscribeAsync('SomeEvent', handler2);
return pubSub.publishAsync('SomeEvent', {}, function() {
expect(callCount).to.equal(2);
return done();
});
});
});
return describe('#unsubscribe', function() {
return it('should unsubscribe the subscriber and not notify it anymore', function(done) {
var publishedEvent, subscriberFn, subscriberId;
publishedEvent = {};
subscriberFn = sandbox.spy();
subscriberId = pubSub.subscribe('SomeEvent', subscriberFn);
pubSub.unsubscribe(subscriberId);
return pubSub.publishAsync('SomeEvent', publishedEvent, function() {
expect(subscriberFn).not.to.have.been.called;
return done();
});
});
});
});