index.js
4.7 KB
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
var Emitter = require('../index');
var emitter = require('../instance');
var test = require('tape');
test('subscribes to an event', function (t) {
var emitter = new Emitter();
emitter.on('test', function () {});
t.equal(emitter.e.test.length, 1, 'subscribed to event');
t.end();
});
test('subscribes to an event with context', function (t) {
var emitter = new Emitter();
var context = {
contextValue: true
};
emitter.on('test', function () {
t.ok(this.contextValue, 'is in context');
t.end();
}, context);
emitter.emit('test');
});
test('subscibes only once to an event', function (t) {
var emitter = new Emitter();
emitter.once('test', function () {
t.notOk(emitter.e.test, 'removed event from list');
t.end();
});
emitter.emit('test');
});
test('keeps context when subscribed only once', function (t) {
var emitter = new Emitter();
var context = {
contextValue: true
};
emitter.once('test', function () {
t.ok(this.contextValue, 'is in context');
t.notOk(emitter.e.test, 'not subscribed anymore');
t.end();
}, context);
emitter.emit('test');
});
test('emits an event', function (t) {
var emitter = new Emitter();
emitter.on('test', function () {
t.ok(true, 'triggered event');
t.end();
});
emitter.emit('test');
});
test('passes all arguments to event listener', function (t) {
var emitter = new Emitter();
emitter.on('test', function (arg1, arg2) {
t.equal(arg1, 'arg1', 'passed the first argument');
t.equal(arg2, 'arg2', 'passed the second argument');
t.end();
});
emitter.emit('test', 'arg1', 'arg2');
});
test('unsubscribes from all events with name', function (t) {
var emitter = new Emitter();
emitter.on('test', function () {
t.fail('should not get called');
});
emitter.off('test');
emitter.emit('test')
process.nextTick(function () {
t.end();
});
});
test('unsubscribes single event with name and callback', function (t) {
var emitter = new Emitter();
var fn = function () {
t.fail('should not get called');
}
emitter.on('test', fn);
emitter.off('test', fn);
emitter.emit('test')
process.nextTick(function () {
t.end();
});
});
// Test added by https://github.com/lazd
// From PR: https://github.com/scottcorgan/tiny-emitter/pull/6
test('unsubscribes single event with name and callback when subscribed twice', function (t) {
var emitter = new Emitter();
var fn = function () {
t.fail('should not get called');
};
emitter.on('test', fn);
emitter.on('test', fn);
emitter.off('test', fn);
emitter.emit('test');
process.nextTick(function () {
t.notOk(emitter.e['test'], 'removes all events');
t.end();
});
});
test('unsubscribes single event with name and callback when subscribed twice out of order', function (t) {
var emitter = new Emitter();
var calls = 0;
var fn = function () {
t.fail('should not get called');
};
var fn2 = function () {
calls++;
};
emitter.on('test', fn);
emitter.on('test', fn2);
emitter.on('test', fn);
emitter.off('test', fn);
emitter.emit('test');
process.nextTick(function () {
t.equal(calls, 1, 'callback was called');
t.end();
});
});
test('removes an event inside another event', function (t) {
var emitter = new Emitter();
emitter.on('test', function () {
t.equal(emitter.e.test.length, 1, 'event is still in list');
emitter.off('test');
t.notOk(emitter.e.test, 0, 'event is gone from list');
t.end();
});
emitter.emit('test');
});
test('event is emitted even if unsubscribed in the event callback', function (t) {
var emitter = new Emitter();
var calls = 0;
var fn = function () {
calls += 1;
emitter.off('test', fn);
};
emitter.on('test', fn);
emitter.on('test', function () {
calls += 1;
});
emitter.on('test', function () {
calls += 1;
});
process.nextTick(function () {
t.equal(calls, 3, 'all callbacks were called');
t.end();
});
emitter.emit('test');
});
test('calling off before any events added does nothing', function (t) {
var emitter = new Emitter();
emitter.off('test', function () {});
t.end();
});
test('emitting event that has not been subscribed to yet', function (t) {
var emitter = new Emitter();
emitter.emit('some-event', 'some message');
t.end();
});
test('unsubscribes single event with name and callback which was subscribed once', function (t) {
var emitter = new Emitter();
var fn = function () {
t.fail('event not unsubscribed');
}
emitter.once('test', fn);
emitter.off('test', fn);
emitter.emit('test');
t.end();
});
test('exports an instance', function (t) {
t.ok(emitter, 'exports an instance')
t.ok(emitter instanceof Emitter, 'an instance of the Emitter class');
t.end();
});