Skip to content

Commit

Permalink
Bug fix in emit functions. More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanickrochon committed Jun 8, 2015
1 parent 40c0b8a commit cace172
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 16 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function addListener(type, listener) {
// Adding the second element, need to change to array.
existing = events[type] = [existing, listener];
} else {
// If we\'ve already got an array, just append.
// If we've already got an array, just append.
existing.push(listener);
}
// Check for listener leak
Expand Down Expand Up @@ -411,7 +411,7 @@ function emitNone(handler, isFn, emitter) {
promiseList = [];

for (i = 0, len = handler.length; i < len; ++i) {
promiseList.push(handler.call(emitter));
promiseList[i] = handler[i].call(emitter);
}
}

Expand All @@ -430,7 +430,7 @@ function emitOne(handler, isFn, emitter, arg) {
promiseList = new Array(len);

for (i = 0; i < len; ++i) {
promiseList[i] = handler.call(emitter, arg);
promiseList[i] = handler[i].call(emitter, arg);
}
}

Expand All @@ -449,7 +449,7 @@ function emitTwo(handler, isFn, emitter, arg1, arg2) {
promiseList = new Array(len);

for (i = 0; i < len; ++i) {
promiseList[i] = handler.call(emitter, arg1, arg2);
promiseList[i] = handler[i].call(emitter, arg1, arg2);
}
}

Expand All @@ -468,7 +468,7 @@ function emitThree(handler, isFn, emitter, arg1, arg2, arg3) {
promiseList = new Array(len);

for (i = 0; i < len; ++i) {
promiseList[i] = handler.call(emitter, arg1, arg2, arg3);
promiseList[i] = handler[i].call(emitter, arg1, arg2, arg3);
}
}

Expand All @@ -487,7 +487,7 @@ function emitMany(handler, isFn, emitter, args) {
promiseList = new Array(len);

for (i = 0; i < len; ++i) {
promiseList[i] = handler.apply(emitter, args);
promiseList[i] = handler[i].apply(emitter, args);
}
}

Expand Down
192 changes: 182 additions & 10 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ describe("Test Promise Events Emitter", function () {

events.hasOwnProperty('_events');
events.hasOwnProperty('_maxListeners');


});

it("should add and remove listeners", function (done) {
Expand Down Expand Up @@ -63,7 +61,6 @@ describe("Test Promise Events Emitter", function () {
events._events.should.eql({});

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

});

it("should emit 'newListener'", function (done) {
Expand All @@ -84,7 +81,6 @@ describe("Test Promise Events Emitter", function () {

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

});

it("should emit 'removeListener'", function (done) {
Expand All @@ -105,7 +101,6 @@ describe("Test Promise Events Emitter", function () {

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

});

it("should use domain events");
Expand All @@ -115,15 +110,192 @@ describe("Test Promise Events Emitter", function () {

describe("Emitting events", function () {

it("should emit with no arguments");
it("should emit with no arguments", function (done) {
var events = new Emitter();
var fn = function () {
arguments.should.have.lengthOf(0);
};

it("should emit with one argument");
this.timeout(1000);

it("should emit with two argument");
events.addListener('foo', fn).then(function () {
return events.emit('foo').then(function (results) {

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

it("should emit with three argument");
}).then(events.addListener('foo', fn)).then(function () {
return events.emit('foo').then(function (results) {

it("should emit with many argument");
results.should.be.an.Array.of.length(2);
should(results[0]).be.undefined;
should(results[1]).be.undefined;
});
});
}).then(done).catch(done);
});

it("should emit with one argument", function (done) {
var events = new Emitter();
var a = 'Hello';
var fn = function (arg1) {
arguments.should.have.lengthOf(1);

arg1.should.equal(a);

return arg1;
};

this.timeout(1000);

events.addListener('foo', fn).then(function () {
return events.emit('foo', a).then(function (results) {

results.should.be.an.Array.of.length(1);
should(results[0]).equal(a);

}).then(events.addListener('foo', fn)).then(function () {
return events.emit('foo', a).then(function (results) {

results.should.be.an.Array.of.length(2);
should(results[0]).equal(a);
should(results[1]).equal(a);
});
});
}).then(done).catch(done);
});

it("should emit with two argument", function (done) {
var events = new Emitter();
var a = 'Hello';
var b = 'World';
var fn1 = function (arg1, arg2) {
arguments.should.have.lengthOf(2);

arg1.should.equal(a);
arg2.should.equal(b);

return arg1;
};
var fn2 = function (arg1, arg2) {
arguments.should.have.lengthOf(2);

arg1.should.equal(a);
arg2.should.equal(b);

return arg2;
};

this.timeout(1000);

events.addListener('foo', fn1).then(function () {
return events.emit('foo', a, b).then(function (results) {

results.should.be.an.Array.of.length(1);
should(results[0]).equal(a);

}).then(events.addListener('foo', fn2)).then(function () {
return events.emit('foo', a, b).then(function (results) {

results.should.be.an.Array.of.length(2);
should(results[0]).equal(a);
should(results[1]).equal(b);
});
});
}).then(done).catch(done);
});

it("should emit with three argument", function (done) {
var events = new Emitter();
var a = 'Hello';
var b = 'World';
var c = '!!';
var fn1 = function (arg1, arg2, arg3) {
arguments.should.have.lengthOf(3);

arg1.should.equal(a);
arg2.should.equal(b);
arg3.should.equal(c);

return arg1;
};
var fn2 = function (arg1, arg2, arg3) {
arguments.should.have.lengthOf(3);

arg1.should.equal(a);
arg2.should.equal(b);
arg3.should.equal(c);

return arg2;
};
var fn3 = function (arg1, arg2, arg3) {
arguments.should.have.lengthOf(3);

arg1.should.equal(a);
arg2.should.equal(b);
arg3.should.equal(c);

return arg3;
};

this.timeout(1000);

events.addListener('foo', fn1).then(function () {
return events.emit('foo', a, b, c).then(function (results) {

results.should.be.an.Array.of.length(1);
should(results[0]).equal(a);

}).then(events.addListener('foo', fn2)).then(function () {
return events.emit('foo', a, b, c).then(function (results) {

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

}).then(events.addListener('foo', fn3)).then(function () {
return events.emit('foo', a, b, c).then(function (results) {

results.should.be.an.Array.of.length(3);
should(results[0]).equal(a);
should(results[1]).equal(b);
should(results[2]).equal(c);

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

it("should emit with many argument", function (done) {
var events = new Emitter();
var args = ['a', 'b', 'c', 'd'];
function fnGenerator(retVal) {
return function () {
arguments.should.have.lengthOf(args.length);
Array.prototype.slice.call(arguments).should.eql(args);

return retVal;
}
}

events.addListener('foo', fnGenerator(1)).then(function () {
return events.emit('foo', 'a', 'b', 'c', 'd').then(function (results) {

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

}).then(events.addListener('foo', fnGenerator(2))).then(function () {
return events.emit('foo', 'a', 'b', 'c', 'd').then(function (results) {

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

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

});

Expand Down

0 comments on commit cace172

Please sign in to comment.