Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
fix: #8. Defer to browsers for incorrect callback type
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Nov 24, 2017
1 parent 6e64b6e commit 0af5895
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ var enhance = module.exports = function enhance(proto) {

var listeners = new WeakMap()
proto.addEventListener = function(name, originalCallback, optionsOrCapture) {
if (optionsOrCapture === undefined || optionsOrCapture === true || optionsOrCapture === false) {
if (
optionsOrCapture === undefined ||
optionsOrCapture === true ||
optionsOrCapture === false ||
(!originalCallback || typeof originalCallback !== 'function' && typeof originalCallback !== 'object')
) {
return originalAddEventListener.call(this, name, originalCallback, optionsOrCapture)
}

Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ function testSuite(eventTarget) {
eventTarget.removeEventListener('test', increment, true)
})

describe('unexpected behaviours', function() {

it('deffers to browser behavior for invalid arguments', function() {
try {
// either browsers will silently ignore this call, or they'll throw an error specifically about `addEventListener`
// if neither of those happen, an error happened in our code.
eventTarget.addEventListener('test1', 'this is invalid, but someone is doing this in the wild!', {})
} catch (e) {
// ok... I lied... Safari throws "Type error" not anything about addEventListener
assert.ok(/(addEventListener|Type error)/.test(e.message), 'error was thrown internally to our code')
}
})

})

describe('expected behaviours', function() {
it('can bind and unbind the same event functions to different event names', function() {
eventTarget.addEventListener('test1', increment)
Expand Down

0 comments on commit 0af5895

Please sign in to comment.