-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Use a Symbol for attaching the store to the global (#9)
feat!: Use a Symbol for attaching the default namespace to the store feat: Use Symbol.for so other applications can create the same Symbol Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
- Loading branch information
Showing
3 changed files
with
66 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,77 @@ | ||
'use strict'; | ||
|
||
var expect = require('expect'); | ||
var EventEmitter = require('events').EventEmitter; | ||
|
||
var sparkles = require('../'); | ||
|
||
function noop() {} | ||
|
||
describe('sparkles()', function () { | ||
var ee; | ||
describe('behavior on global', function () { | ||
var ee; | ||
var storeSymbol = Symbol.for('sparkles:store'); | ||
var namespaceSymbol = Symbol.for('sparkles:namespace'); | ||
|
||
beforeEach(function (done) { | ||
ee = sparkles(); | ||
done(); | ||
}); | ||
beforeEach(function (done) { | ||
ee = sparkles(); | ||
done(); | ||
}); | ||
|
||
afterEach(function (done) { | ||
ee.remove(); | ||
done(); | ||
afterEach(function (done) { | ||
ee.remove(); | ||
done(); | ||
}); | ||
|
||
it('will attach the sparkles store namespace to global', function (done) { | ||
expect(global[storeSymbol]).toBeTruthy(); | ||
done(); | ||
}); | ||
|
||
it('will attach an event emitter to the sparkles store default namespace', function (done) { | ||
expect(global[storeSymbol][namespaceSymbol]).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[storeSymbol][namespaceSymbol]).toBeUndefined(); | ||
done(); | ||
}); | ||
|
||
it('does not show up when enumerating the global object', function (done) { | ||
expect(Object.keys(global)).not.toContain(storeSymbol); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('will attach the sparkles store namespace to global', function (done) { | ||
expect(global['store@sparkles']).toBeTruthy(); | ||
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('will attach an event emitter to the sparkles store default namespace', function (done) { | ||
expect(global['store@sparkles']).toHaveProperty('default'); | ||
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('removes the event emitter from the store when remove is called', function (done) { | ||
ee.on('test', noop); | ||
ee.remove(); | ||
expect(global['store@sparkles']).not.toHaveProperty('default'); | ||
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters