Skip to content

Commit

Permalink
Update: Improve tests & remove nextTick code
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed May 14, 2018
1 parent ce23391 commit 7a03563
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,39 @@ function getEmitter(namespace){
var removeListeners = ee.listeners('removeListener');
var newListeners = ee.listeners('newListener');

// exit if we have events other than
// `removeListener` and `newListener`
if(events.length > 2){
return;
}

if(removeListeners.length !== 1){
// exit if someone attached another
// listener to `removeListener`
if(removeListeners.length > 1){
return;
}

if(newListeners.length !== 1 && events.length !== 1){
// exit if someone attached another
// listener to `newListener`
if(newListeners.length > 1){
return;
}

delete global[namespace];

process.nextTick(rewire);
}

function rewire(){
var removeListeners = ee.listeners('removeListener');
var newListeners = ee.listeners('newListener');

// if we know our `removeListener` isn't
// attached, we add it
if(!removeListeners.length){
ee.on('removeListener', detach);
}

// if we know our `newListener` isn't
// attached, we add it
if(!newListeners.length){
ee.on('newListener', attach);
}
Expand Down
57 changes: 46 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,33 @@ describe('sparkles', function(){

var sparkles;

before(function(done){
beforeEach(function(done){
sparkles = require('../')();
expect(global.__sparklesEventEmitter).to.not.exist();
done();
});

afterEach(function(done){
sparkles.removeAllListeners();
done();
});

it('will attach an event emitter to global upon the first `on` call', function(done){
expect(global.__sparklesEventEmitter).to.not.exist();
sparkles.on('test', noop);
expect(global.__sparklesEventEmitter).to.exist();
expect(global.__sparklesEventEmitter.on).to.be.a.function();
done();
});

it('removes the event emitter from global if no more listeners exist it', function(done){
sparkles.on('test', noop);
expect(global.__sparklesEventEmitter).to.exist();
sparkles.removeListener('test', noop);
expect(global.__sparklesEventEmitter).to.not.exist();
done();
});

it('even works with removeAllListeners', function(done){
expect(global.__sparklesEventEmitter).to.not.exist();
sparkles.on('test1', noop);
sparkles.on('test2', noop2);
expect(global.__sparklesEventEmitter).to.exist();
Expand All @@ -47,17 +52,32 @@ describe('sparkles', function(){
done();
});

it('gracefully handles newListeners being added', function(done){
expect(global.__sparklesEventEmitter).to.not.exist();
it('handles removing all newListeners', function(done){
sparkles.on('newListener', noop);
expect(global.__sparklesEventEmitter).to.exist();
sparkles.removeAllListeners('newListener');
expect(global.__sparklesEventEmitter).to.not.exist();
done();
});

it('gracefully handles removeListeners being added', function(done){
it('gracefully handles newListeners being added and removed', function(done){
sparkles.on('newListener', noop);
sparkles.on('newListener', noop2);
expect(global.__sparklesEventEmitter).to.exist();
sparkles.removeListener('newListener', noop);
expect(global.__sparklesEventEmitter).to.exist();
done();
});

it('handles removing all removeListeners', function(done){
sparkles.on('removeListener', noop);
expect(global.__sparklesEventEmitter).to.exist();
sparkles.removeAllListeners('removeListener');
expect(global.__sparklesEventEmitter).to.not.exist();
done();
});

it('gracefully handles removeListeners being added and removed', function(done){
sparkles.on('removeListener', noop);
sparkles.on('removeListener', noop2);
expect(global.__sparklesEventEmitter).to.exist();
Expand All @@ -66,11 +86,26 @@ describe('sparkles', function(){
done();
});

it('recovers from removeAllListeners on removeListener', function(done){
it('recovers from removeAllListeners on removeListener upon new sparkles', function(done){
sparkles.on('test', noop);
sparkles.removeAllListeners('removeListener');
process.nextTick(function(){
expect(sparkles.listeners('removeListener')).to.have.length(1);
done();
});
expect(sparkles.listeners('removeListener')).to.have.length(0);
var sparkles2 = require('../')();
expect(sparkles2.listeners('removeListener')).to.have.length(1);
done();
});

it('does not add attach and detach more than once', function(done){
sparkles.on('test', noop);
expect(sparkles.listeners('removeListener')).to.have.length(1);
var sparkles2 = require('../')();
expect(sparkles2.listeners('removeListener')).to.have.length(1);
done();
});

it('will not attach to global if the detach listener is added more than once', function(done){
sparkles.on('removeListener', sparkles._events.removeListener);
expect(global.__sparklesEventEmitter).to.not.exist();
done();
});
});

0 comments on commit 7a03563

Please sign in to comment.