Skip to content

Commit

Permalink
feat: Add module for accessing legacy store (#10)
Browse files Browse the repository at this point in the history
chore: Combine tests into one file
  • Loading branch information
phated authored Mar 23, 2024
1 parent 9f18a7d commit 8a5de25
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 56 deletions.
48 changes: 48 additions & 0 deletions legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

var EventEmitter = require('events').EventEmitter;

var sparklesNamespace = 'store@sparkles';
var defaultNamespace = 'default'

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

if (!store) {
Object.defineProperty(global, sparklesNamespace, {
value: {},
enumerable: false,
});
store = global[sparklesNamespace];
}

return store;
}

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

namespace = namespace || defaultNamespace;

var ee = store[namespace];

if (!ee) {
ee = store[namespace] = new EventEmitter();
ee.setMaxListeners(0);
ee.remove = function remove() {
ee.removeAllListeners();
delete store[namespace];
};
}

return ee;
}

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

return !!store[namespace];
}

module.exports = getEmitter;
module.exports.exists = exists;
16 changes: 0 additions & 16 deletions test/exists.js

This file was deleted.

46 changes: 46 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,49 @@ describe('sparkles()', function () {
done();
});
});

describe('sparkles.exists()', function () {
it('checks if a namespace has been defined', function (done) {
expect(sparkles.exists('test')).toBe(false);
var ee = sparkles('test');
expect(sparkles.exists('test')).toBe(true);
ee.remove();
expect(sparkles.exists('test')).toBe(false);
done();
});
});

describe('namespace', function () {
var storeSymbol = Symbol.for('sparkles:store');
var namespaceSymbol = Symbol.for('sparkles:namespace');

beforeEach(function (done) {
global[storeSymbol] = {};
done();
});

afterEach(function (done) {
delete global[storeSymbol];
done();
});

it('should use an EE from sparkles namespace if it already exists', function (done) {
var ee = (global[storeSymbol][namespaceSymbol] = new EventEmitter());
ee.custom = 'ee';

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

expect(sparkles.custom).toEqual('ee');
done();
});

it('should allow custom namespaces', function (done) {
var ee = (global[storeSymbol].customNamespace = new EventEmitter());
ee.custom = true;

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

expect(sparkles.custom).toEqual(true);
done();
});
});
123 changes: 123 additions & 0 deletions test/legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
'use strict';

var expect = require('expect');
var EventEmitter = require('events').EventEmitter;

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

describe('legacy: sparkles()', function () {
describe('behavior on global', function () {
var ee;
var storeNamespace = 'store@sparkles';
var defaultNamespace = 'default';

beforeEach(function (done) {
ee = sparkles();
done();
});

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

it('will attach the sparkles store namespace to global', function (done) {
expect(global[storeNamespace]).toBeTruthy();
done();
});

it('will attach an event emitter to the sparkles store default namespace', function (done) {
expect(global[storeNamespace][defaultNamespace]).toBeInstanceOf(EventEmitter);
done();
});

it('removes the event emitter from the store when remove is called', function (done) {
ee.on('test', function () {});
ee.remove();
expect(global[storeNamespace][defaultNamespace]).toBeUndefined();
done();
});

it('does not show up when enumerating the global object', function (done) {
expect(Object.keys(global)).not.toContain(storeNamespace);
done();
});
});

it('should get the default emitter if namespace is not specified', function (done) {
var ee = sparkles();
expect(ee).toBeInstanceOf(EventEmitter);

expect(sparkles()).toBe(ee);
done();
});

it('should get an emitter for a specified namespace', function (done) {
var ee = sparkles('ns1');
expect(ee).toBeInstanceOf(EventEmitter);

expect(sparkles()).not.toBe(ee);
expect(sparkles('ns1')).toBe(ee);
expect(sparkles('ns2')).not.toBe(ee);
done();
});

it('should remove and re-create emitter in the store', function (done) {
var ee0 = sparkles();
var ee1 = sparkles('ns1');

ee0.remove();
expect(sparkles()).not.toBe(ee0);
expect(sparkles('ns1')).toBe(ee1);

ee1.remove();
expect(sparkles('ns1')).not.toBe(ee1);
done();
});
});

describe('legacy: sparkles.exists()', function () {
it('checks if a namespace has been defined', function (done) {
expect(sparkles.exists('test')).toBe(false);
var ee = sparkles('test');
expect(sparkles.exists('test')).toBe(true);
ee.remove();
expect(sparkles.exists('test')).toBe(false);
done();
});
});

describe('legacy: namespace', function () {
var storeNamespace = Symbol.for('sparkles:store');
var defaultNamespace = Symbol.for('sparkles:namespace');

beforeEach(function (done) {
global[storeNamespace] = {};
done();
});

afterEach(function (done) {
delete global[storeNamespace];
done();
});

it('should use an EE from sparkles namespace if it already exists', function (done) {
var ee = (global[storeNamespace][defaultNamespace] = new EventEmitter());
ee.custom = 'ee';

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

expect(sparkles.custom).toEqual('ee');
done();
});

it('should allow custom namespaces', function (done) {
var ee = (global[storeNamespace].customNamespace = new EventEmitter());
ee.custom = true;

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

expect(sparkles.custom).toEqual(true);
done();
});
});
40 changes: 0 additions & 40 deletions test/namespace.js

This file was deleted.

0 comments on commit 8a5de25

Please sign in to comment.