Skip to content

Commit

Permalink
Fundamental tests complete
Browse files Browse the repository at this point in the history
  • Loading branch information
yanickrochon committed Jun 8, 2015
1 parent aee7c93 commit 4b88923
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 14 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,11 @@ function listenerCount(emitter, type) {
var events = emitter._events;
if (events) {
evlistener = events[type];
if (typeof evlistener === 'function')
if (typeof evlistener === 'function') {
ret = 1;
else if (evlistener)
} else if (evlistener) {
ret = evlistener.length;
}
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "promise-events",
"version": "0.0.2",
"version": "0.0.3",
"description": "A promise-based events emitter",
"author": "Yanick Rochon <yanick.rochon@gmail.com>",
"keywords": [
Expand Down
109 changes: 98 additions & 11 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("Test Promise Events Emitter", function () {
events.hasOwnProperty('_maxListeners');
});


it("should add and remove listeners", function (done) {
var events = new Emitter();
var fn = function () {};
Expand Down Expand Up @@ -63,6 +64,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should not add invalid listeners", function (done) {
var events = new Emitter();

Expand All @@ -86,7 +88,24 @@ describe("Test Promise Events Emitter", function () {
})).then(function () {
done();
}).catch(done);
})
});


it("should not remove invalid listeners", function (done) {
var events = new Emitter();

Promise.all([undefined, null, false, true, -1, 0, 1, '', {}, [], /./].map(function (invalid) {
try {
events.removeListener('foo', invalid).then(function () {
throw new Error("Should not allow removing invalid listener : " + invalid);
});
} catch (e) {
// all good!
}
})).then(function () {
done();
}).catch(done);
});


it("should remove all listeners", function (done) {
Expand All @@ -104,31 +123,49 @@ describe("Test Promise Events Emitter", function () {
return events.removeAllListeners('foo').then(function () {
events._events.should.not.have.ownProperty('foo');
events._eventsCount.should.equal(0);
events.listeners('foo').should.be.an.Array.lengthOf(0);

return events.addListener('foo', fn).then(function () {
events._eventsCount.should.equal(1);
events._events['foo'].should.be.a.Function;
events.listeners('foo').should.be.an.Array.lengthOf(1);

return events.removeAllListeners('foo').then(function () {
events._events.should.not.have.ownProperty('foo');
events._eventsCount.should.equal(0);

return events.removeAllListeners('foo');
});
return events.removeAllListeners('foo').then(function () {

});
Emitter.listenerCount(events, 'bar').should.equal(0);

});
return events.removeListener('bar', fn).then(function () {

}).then(done).catch(done);
events._events = undefined;
events._eventsCount = 0;

events.listeners('foo').should.be.an.Array.lengthOf(0);
Emitter.listenerCount(events, 'foo').should.equal(0);

return events.removeListener('foo', fn).then(events.removeAllListeners());
});
});
});
});
});
}).then(done).catch(done);
});



it("should use domain events");


it("should detect memory leak");


it("should handle emit errors");


describe("Emitting events", function () {

it("should emit 'newListener'", function (done) {
Expand All @@ -151,6 +188,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit 'removeListener'", function (done) {
var events = new Emitter();
var fn = function () {};
Expand All @@ -173,6 +211,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit with no arguments", function (done) {
var events = new Emitter();
var fn = function () {
Expand All @@ -187,17 +226,26 @@ describe("Test Promise Events Emitter", function () {
results.should.be.an.Array.of.length(1);
should(results[0]).be.undefined;

}).then(events.on('foo', fn)).then(function () {
return events.emit('foo').then(function (results) {
console.log(events._events);
Emitter.listenerCount(events, 'foo').should.equal(1);

results.should.be.an.Array.of.length(2);
should(results[0]).be.undefined;
should(results[1]).be.undefined;
return events.on('foo', fn).then(function () {
return events.emit('foo').then(function (results) {

results.should.be.an.Array.of.length(2);
should(results[0]).be.undefined;
should(results[1]).be.undefined;

events.listeners('foo').should.be.an.Array.lengthOf(2);
Emitter.listenerCount(events, 'foo').should.equal(2);

});
});
});
}).then(done).catch(done);
});


it("should emit with one argument", function (done) {
var events = new Emitter();
var a = 'Hello';
Expand Down Expand Up @@ -228,6 +276,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit with two argument", function (done) {
var events = new Emitter();
var a = 'Hello';
Expand Down Expand Up @@ -268,6 +317,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit with three argument", function (done) {
var events = new Emitter();
var a = 'Hello';
Expand Down Expand Up @@ -330,6 +380,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit with many argument", function (done) {
var events = new Emitter();
var args = ['a', 'b', 'c', 'd'];
Expand Down Expand Up @@ -385,6 +436,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit 'removeListener'", function (done) {
var events = new Emitter();
var fn = function () {};
Expand All @@ -407,6 +459,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit 'newListener' only once", function (done) {
var events = new Emitter();
var fn = function () {};
Expand All @@ -430,6 +483,7 @@ describe("Test Promise Events Emitter", function () {

});


it("should emit with no arguments", function (done) {
var events = new Emitter();
var fn = function () {
Expand Down Expand Up @@ -464,6 +518,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit with one argument", function (done) {
var events = new Emitter();
var a = 'Hello';
Expand Down Expand Up @@ -500,6 +555,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit with two argument", function (done) {
var events = new Emitter();
var a = 'Hello';
Expand Down Expand Up @@ -546,6 +602,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit with three argument", function (done) {
var events = new Emitter();
var a = 'Hello';
Expand Down Expand Up @@ -616,6 +673,7 @@ describe("Test Promise Events Emitter", function () {
}).then(done).catch(done);
});


it("should emit with many argument", function (done) {
var events = new Emitter();
var args = ['a', 'b', 'c', 'd'];
Expand Down Expand Up @@ -653,4 +711,33 @@ describe("Test Promise Events Emitter", function () {
});


describe("Test subclassing", function () {

it("should create valid instance", function (done) {
var util = require('util');
var SubEmitter = function () {};
var events;

util.inherits(SubEmitter, Emitter);

events = new SubEmitter();

events.should.be.instanceOf(Emitter);

events.should.not.have.ownProperty('_events');
events.should.not.have.ownProperty('_eventsCount');

events.on('foo', function () {}).then(function () {

events.should.have.ownProperty('_events').and.have.ownProperty('foo').be.a.Function;
events.should.have.ownProperty('_eventsCount').equal(1);

}).then(done).catch(done);

});



});

});

0 comments on commit 4b88923

Please sign in to comment.