| 1 |
var Emitter = require('../index'); |
| 2 |
var emitter = require('../instance'); |
| 3 |
var test = require('tape'); |
| 4 |
|
| 5 |
test('subscribes to an event', function (t) { |
| 6 |
var emitter = new Emitter(); |
| 7 |
emitter.on('test', function () {}); |
| 8 |
|
| 9 |
t.equal(emitter.e.test.length, 1, 'subscribed to event'); |
| 10 |
t.end(); |
| 11 |
}); |
| 12 |
|
| 13 |
test('subscribes to an event with context', function (t) { |
| 14 |
var emitter = new Emitter(); |
| 15 |
var context = { |
| 16 |
contextValue: true |
| 17 |
}; |
| 18 |
|
| 19 |
emitter.on('test', function () { |
| 20 |
t.ok(this.contextValue, 'is in context'); |
| 21 |
t.end(); |
| 22 |
}, context); |
| 23 |
|
| 24 |
emitter.emit('test'); |
| 25 |
}); |
| 26 |
|
| 27 |
test('subscibes only once to an event', function (t) { |
| 28 |
var emitter = new Emitter(); |
| 29 |
|
| 30 |
emitter.once('test', function () { |
| 31 |
t.notOk(emitter.e.test, 'removed event from list'); |
| 32 |
t.end(); |
| 33 |
}); |
| 34 |
|
| 35 |
emitter.emit('test'); |
| 36 |
}); |
| 37 |
|
| 38 |
test('keeps context when subscribed only once', function (t) { |
| 39 |
var emitter = new Emitter(); |
| 40 |
var context = { |
| 41 |
contextValue: true |
| 42 |
}; |
| 43 |
|
| 44 |
emitter.once('test', function () { |
| 45 |
t.ok(this.contextValue, 'is in context'); |
| 46 |
t.notOk(emitter.e.test, 'not subscribed anymore'); |
| 47 |
t.end(); |
| 48 |
}, context); |
| 49 |
|
| 50 |
emitter.emit('test'); |
| 51 |
}); |
| 52 |
|
| 53 |
test('emits an event', function (t) { |
| 54 |
var emitter = new Emitter(); |
| 55 |
|
| 56 |
emitter.on('test', function () { |
| 57 |
t.ok(true, 'triggered event'); |
| 58 |
t.end(); |
| 59 |
}); |
| 60 |
|
| 61 |
emitter.emit('test'); |
| 62 |
}); |
| 63 |
|
| 64 |
test('passes all arguments to event listener', function (t) { |
| 65 |
var emitter = new Emitter(); |
| 66 |
|
| 67 |
emitter.on('test', function (arg1, arg2) { |
| 68 |
t.equal(arg1, 'arg1', 'passed the first argument'); |
| 69 |
t.equal(arg2, 'arg2', 'passed the second argument'); |
| 70 |
t.end(); |
| 71 |
}); |
| 72 |
|
| 73 |
emitter.emit('test', 'arg1', 'arg2'); |
| 74 |
}); |
| 75 |
|
| 76 |
test('unsubscribes from all events with name', function (t) { |
| 77 |
var emitter = new Emitter(); |
| 78 |
emitter.on('test', function () { |
| 79 |
t.fail('should not get called'); |
| 80 |
}); |
| 81 |
emitter.off('test'); |
| 82 |
emitter.emit('test') |
| 83 |
|
| 84 |
process.nextTick(function () { |
| 85 |
t.end(); |
| 86 |
}); |
| 87 |
}); |
| 88 |
|
| 89 |
test('unsubscribes single event with name and callback', function (t) { |
| 90 |
var emitter = new Emitter(); |
| 91 |
var fn = function () { |
| 92 |
t.fail('should not get called'); |
| 93 |
} |
| 94 |
|
| 95 |
emitter.on('test', fn); |
| 96 |
emitter.off('test', fn); |
| 97 |
emitter.emit('test') |
| 98 |
|
| 99 |
process.nextTick(function () { |
| 100 |
t.end(); |
| 101 |
}); |
| 102 |
}); |
| 103 |
|
| 104 |
// Test added by https://github.com/lazd |
| 105 |
// From PR: https://github.com/scottcorgan/tiny-emitter/pull/6 |
| 106 |
test('unsubscribes single event with name and callback when subscribed twice', function (t) { |
| 107 |
var emitter = new Emitter(); |
| 108 |
var fn = function () { |
| 109 |
t.fail('should not get called'); |
| 110 |
}; |
| 111 |
|
| 112 |
emitter.on('test', fn); |
| 113 |
emitter.on('test', fn); |
| 114 |
|
| 115 |
emitter.off('test', fn); |
| 116 |
emitter.emit('test'); |
| 117 |
|
| 118 |
process.nextTick(function () { |
| 119 |
t.notOk(emitter.e['test'], 'removes all events'); |
| 120 |
t.end(); |
| 121 |
}); |
| 122 |
}); |
| 123 |
|
| 124 |
test('unsubscribes single event with name and callback when subscribed twice out of order', function (t) { |
| 125 |
var emitter = new Emitter(); |
| 126 |
var calls = 0; |
| 127 |
var fn = function () { |
| 128 |
t.fail('should not get called'); |
| 129 |
}; |
| 130 |
var fn2 = function () { |
| 131 |
calls++; |
| 132 |
}; |
| 133 |
|
| 134 |
emitter.on('test', fn); |
| 135 |
emitter.on('test', fn2); |
| 136 |
emitter.on('test', fn); |
| 137 |
emitter.off('test', fn); |
| 138 |
emitter.emit('test'); |
| 139 |
|
| 140 |
process.nextTick(function () { |
| 141 |
t.equal(calls, 1, 'callback was called'); |
| 142 |
t.end(); |
| 143 |
}); |
| 144 |
}); |
| 145 |
|
| 146 |
test('removes an event inside another event', function (t) { |
| 147 |
var emitter = new Emitter(); |
| 148 |
|
| 149 |
emitter.on('test', function () { |
| 150 |
t.equal(emitter.e.test.length, 1, 'event is still in list'); |
| 151 |
|
| 152 |
emitter.off('test'); |
| 153 |
|
| 154 |
t.notOk(emitter.e.test, 0, 'event is gone from list'); |
| 155 |
t.end(); |
| 156 |
}); |
| 157 |
|
| 158 |
emitter.emit('test'); |
| 159 |
}); |
| 160 |
|
| 161 |
test('event is emitted even if unsubscribed in the event callback', function (t) { |
| 162 |
var emitter = new Emitter(); |
| 163 |
var calls = 0; |
| 164 |
var fn = function () { |
| 165 |
calls += 1; |
| 166 |
emitter.off('test', fn); |
| 167 |
}; |
| 168 |
|
| 169 |
emitter.on('test', fn); |
| 170 |
|
| 171 |
emitter.on('test', function () { |
| 172 |
calls += 1; |
| 173 |
}); |
| 174 |
|
| 175 |
emitter.on('test', function () { |
| 176 |
calls += 1; |
| 177 |
}); |
| 178 |
|
| 179 |
process.nextTick(function () { |
| 180 |
t.equal(calls, 3, 'all callbacks were called'); |
| 181 |
t.end(); |
| 182 |
}); |
| 183 |
|
| 184 |
emitter.emit('test'); |
| 185 |
}); |
| 186 |
|
| 187 |
test('calling off before any events added does nothing', function (t) { |
| 188 |
var emitter = new Emitter(); |
| 189 |
emitter.off('test', function () {}); |
| 190 |
t.end(); |
| 191 |
}); |
| 192 |
|
| 193 |
test('emitting event that has not been subscribed to yet', function (t) { |
| 194 |
var emitter = new Emitter(); |
| 195 |
|
| 196 |
emitter.emit('some-event', 'some message'); |
| 197 |
t.end(); |
| 198 |
}); |
| 199 |
|
| 200 |
test('unsubscribes single event with name and callback which was subscribed once', function (t) { |
| 201 |
var emitter = new Emitter(); |
| 202 |
var fn = function () { |
| 203 |
t.fail('event not unsubscribed'); |
| 204 |
} |
| 205 |
|
| 206 |
emitter.once('test', fn); |
| 207 |
emitter.off('test', fn); |
| 208 |
emitter.emit('test'); |
| 209 |
|
| 210 |
t.end(); |
| 211 |
}); |
| 212 |
|
| 213 |
test('exports an instance', function (t) { |
| 214 |
t.ok(emitter, 'exports an instance') |
| 215 |
t.ok(emitter instanceof Emitter, 'an instance of the Emitter class'); |
| 216 |
t.end(); |
| 217 |
}); |