Skip to content

Commit

Permalink
fix: removeListener (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidant authored and gr2m committed Oct 2, 2019
1 parent 27bdc45 commit 02a41a6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion event-handler/remove-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function receiverListener (state, webhookNameOrNames, handler) {

// remove last hook that has been added, that way
// it behaves the same as removeListener
for (let i = state.hooks[webhookNameOrNames].length; i > 0; i--) {
for (let i = state.hooks[webhookNameOrNames].length - 1; i >= 0; i--) {
if (state.hooks[webhookNameOrNames][i] === handler) {
state.hooks[webhookNameOrNames].splice(i, 1)
return
Expand Down
42 changes: 42 additions & 0 deletions test/unit/event-handler-remove-listener-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const test = require('tap').test

const removeListener = require('../../event-handler/remove-listener')

test('remove-listener: single listener', t => {
const push = () => {}

const state = {
hooks: {
push: [push]
}
}

t.doesNotThrow(() => {
removeListener(state, 'push', push)
})
t.deepEqual(state, { hooks: { push: [] } })
t.end()
})

test('remove-listener: multiple listeners', t => {
const push1 = () => {}
const push2 = () => {}
const push3 = () => {}

const ping = () => {}

const state = {
hooks: {
push: [push1, push2, push3],
ping: [ping]
}
}

t.doesNotThrow(() => {
removeListener(state, 'push', push1)
removeListener(state, 'push', push2)
removeListener(state, 'push', push3)
})
t.deepEqual(state, { hooks: { push: [], ping: [ping] } })
t.end()
})

0 comments on commit 02a41a6

Please sign in to comment.