-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
263 lines (199 loc) · 6.21 KB
/
index.test.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* eslint-env node, mocha */
'use strict';
const assert = require('chai').assert
, eventStore = require('./components/store')
, emitter = require('./index');
let test;
describe('Event Tests', () => {
beforeEach(() => {
test = true;
});
afterEach(() => {
Object.keys(eventStore).forEach((key) => {
emitter.off(key);
});
});
describe('.addListener tests', () => {
it('should be the same function as .on', () => {
assert.strictEqual(emitter.on, emitter.addListener);
});
});
describe('.on tests', () => {
it('should have an "on" function', () => {
assert.isFunction(emitter.on);
});
it('should execute the callback passed to "on" when an event is triggered', (done) => {
emitter.on('some-event', () => {
assert.strictEqual(true, true);
done();
});
emitter.emit('some-event');
});
it('executes the callback passed to "on" when an event triggers and recieves data', (done) => {
emitter.on('some-event', (data) => {
assert.isObject(data);
done();
});
emitter.emit('some-event', { name: 'Tom Sawyer' });
});
it('should execute the callback passed to "on" with the scope passed in', (done) => {
emitter.on('some-event', function (data) {
// eslint-disable-next-line no-invalid-this
assert.strictEqual(data.name, this.name);
done();
}, { name: 'Tom Sawyer' });
emitter.emit('some-event', { name: 'Tom Sawyer' });
});
it('executes the callback passed to "on" with the scope passed only once when once', (done) => {
let count = 0;
emitter.on('some-event', () => {
count++;
}, {}, true);
emitter.on('some-other-event', () => {
assert.strictEqual(count, 1);
done();
});
emitter.emit('some-event');
emitter.emit('some-event');
emitter.emit('some-other-event');
});
it('should allow an object with named keys instead of function params', (done) => {
let count = 0;
emitter.on({
eventName: 'some-event'
, handler: function () {
count++;
}
, scope: {}
, once: true
});
emitter.on('some-other-event', () => {
assert.strictEqual(count, 1);
done();
});
emitter.emit('some-event');
emitter.emit('some-event');
emitter.emit('some-other-event');
});
it('allows an object with keys instead of function params for once and handler', (done) => {
let count = 0;
emitter.on({
eventName: 'some-event'
, handler: function () {
count++;
}
, once: true
});
emitter.on('some-other-event', () => {
assert.strictEqual(count, 1);
done();
});
emitter.emit('some-event');
emitter.emit('some-event');
emitter.emit('some-other-event');
});
it('should allow an object with named keys instead of function params for handler', (done) => {
let count = 0;
emitter.on({
eventName: 'some-event'
, handler: function () {
count++;
}
});
emitter.on('some-other-event', () => {
assert.strictEqual(count, 2);
done();
});
emitter.emit('some-event');
emitter.emit('some-event');
emitter.emit('some-other-event');
});
});
describe('.once tests', () => {
it('should have a "once" function', () => {
assert.isFunction(emitter.once);
});
it('should add an item to the eventStore with once set when called with params', () => {
emitter.once('test', () => {}, {});
assert.strictEqual(eventStore.test.length, 1);
assert.strictEqual(eventStore.test[0].once, true);
});
it('should add an item to the eventStore with once set when called with object hash', () => {
emitter.once({
eventName: 'tests'
, call: function () {}
, scope: {}
});
assert.strictEqual(eventStore.tests.length, 1);
assert.strictEqual(eventStore.tests[0].once, true);
});
});
it('should have an "removeListener" function', () => {
assert.isFunction(emitter.removeListener);
});
it('should have an "removeAllListeners" function', () => {
assert.isFunction(emitter.removeAllListeners);
});
it('should have an "emit" function', () => {
assert.isFunction(emitter.emit);
});
describe('listeners function tests', () => {
it('should have an "listeners" function', () => {
assert.isFunction(emitter.listeners);
});
it('should return an array of all listeners', () => {
emitter.on('some-event', () => {
test = false;
});
assert.isArray(emitter.listeners('some-event'));
assert.lengthOf(emitter.listeners('some-event'), 1);
});
});
it('should have an "addListener" function', () => {
assert.isFunction(emitter.addListener);
});
it('should have a "required" function', () => {
assert.isFunction(emitter.required);
});
describe('.off tests', () => {
it('should have an "off" function', () => {
assert.isFunction(emitter.off);
});
it('should eliminate all listeners to an event when called without function', (done) => {
emitter.on('some-event', () => {
test = false;
});
emitter.off('some-event');
emitter.emit('some-event');
setTimeout(() => {
assert.strictEqual(test, true);
done();
}, 1);
});
});
describe('.removeListener tests', () => {
it('should be the same function as .off', () => {
assert.strictEqual(emitter.off, emitter.removeListener);
});
});
describe('RemoveAllListeners tests', () => {
it('should have a "removeAllListeners" function', () => {
assert.isFunction(emitter.removeAllListeners);
});
it('should eliminate all listeners to an event when called', (done) => {
test = true;
emitter.on('some-event', () => {
test = false;
});
emitter.on('some-event', () => {
test = !!test;
});
emitter.removeAllListeners('some-event');
emitter.emit('some-event');
setTimeout(() => {
assert.strictEqual(test, true);
done();
}, 0);
});
});
});