Skip to content

Commit

Permalink
New: Add exists method & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed May 14, 2018
1 parent 8522589 commit 6094472
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ var EventEmitter = require('events').EventEmitter;
var sparklesNamespace = 'store@sparkles';
var defaultNamespace = 'default';

function getEmitter(namespace){

function getStore(){
var store = global[sparklesNamespace];

if(!store){
store = global[sparklesNamespace] = {};
}

return store;
}

function getEmitter(namespace){

var store = getStore();

namespace = namespace || defaultNamespace;

var ee = store[namespace];
Expand All @@ -27,8 +33,13 @@ function getEmitter(namespace){
}

return ee;
}

function exists(namespace){
var store = getStore();

return !!(store[namespace]);
}

module.exports = getEmitter;
module.exports.exists = exists;
20 changes: 20 additions & 0 deletions test/exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var lab = exports.lab = require('lab').script();
var describe = lab.describe;
var it = lab.it;
var expect = require('code').expect;

var sparkles = require('../');

describe('sparkles.exists()', function(){

it('checks if a namespace has been defined', function(done){
expect(sparkles.exists('test')).to.be.false();
var ee = sparkles('test');
expect(sparkles.exists('test')).to.be.true();
ee.remove();
expect(sparkles.exists('test')).to.be.false();
done();
});
});
18 changes: 8 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
var lab = exports.lab = require('lab').script();
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var beforeEach = lab.beforeEach;
var after = lab.after;
var afterEach = lab.afterEach;
var expect = require('code').expect;

function noop(){}
var sparkles = require('../');

function noop2(){}
function noop(){}

describe('sparkles', function(){
describe('sparkles()', function(){

var sparkles;
var ee;

beforeEach(function(done){
sparkles = require('../')();
ee = sparkles();
done();
});

afterEach(function(done){
sparkles.remove();
ee.remove();
done();
});

Expand All @@ -38,8 +36,8 @@ describe('sparkles', function(){
});

it('removes the event emitter from the store when remove is called', function(done){
sparkles.on('test', noop);
sparkles.remove();
ee.on('test', noop);
ee.remove();
expect(global['store@sparkles']).to.not.include('default');
done();
});
Expand Down
2 changes: 0 additions & 2 deletions test/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
var lab = exports.lab = require('lab').script();
var describe = lab.describe;
var it = lab.it;
var before = lab.before;
var beforeEach = lab.beforeEach;
var after = lab.after;
var afterEach = lab.afterEach;
var expect = require('code').expect;

Expand Down

0 comments on commit 6094472

Please sign in to comment.