-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
123 lines (97 loc) · 3.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
var expect = require('expect');
var EventEmitter = require('events').EventEmitter;
var sparkles = require('../');
describe('sparkles()', function () {
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();
});
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('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('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();
});
});