Skip to content

Commit

Permalink
Build: add tests: removing while emitting
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoborus committed May 31, 2016
1 parent 376a4a8 commit a8df496
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions tests/tests.js
Expand Up @@ -8,17 +8,14 @@ test('on and emit', t => {
const obj = {}
let control = 0
const fn = () => ++control
let unsubscribe = emitter.on(obj, fn)
emitter.on(obj, fn)
emitter.emit(obj)
t.is(control, 1, 'trigger')
emitter.emit(obj)
t.is(control, 2, 'trigger')
emitter.on(obj, fn)
emitter.emit(obj)
t.is(control, 3, 'trigger')
unsubscribe()
emitter.emit(obj)
t.is(control, 3, 'unsubscribe')
t.end()
})

Expand Down Expand Up @@ -103,7 +100,7 @@ test('emit with arguments', t => {
t.end()
})

test('remove listener while emitting', t => {
test('once in a event with muliple listeners', t => {
const emitter = ae()
const out = []
emitter.on('test', () => {
Expand All @@ -124,3 +121,25 @@ test('remove listener while emitting', t => {
t.is(out[4], 'finish')
t.end()
})

test('remove listener in a event with muliple listeners', t => {
const emitter = ae()
const out = []
const f1 = () => out.push(1)
const f2 = () => {
out.push(2)
emitter.off('test', f3)
}
const f3 = () => out.push(3)
emitter.on('test', f1)
emitter.on('test', f2)
emitter.on('test', f3)
emitter.emit('test')
t.is(out[0], 1)
t.is(out[1], 2)
console.log(out)
emitter.emit('test')
t.is(out[2], 1)
t.is(out[3], 2)
t.end()
})

0 comments on commit a8df496

Please sign in to comment.