Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions test/dom-delegation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var test = require("tape")
var setImmediate = require("timers").setImmediate
var document = require("global/document")

var h = require("./lib/h.js")
var createEvent = require("./lib/create-event.js")

var addEvent = require("../add-event.js")
var Delegator = require("../index.js")

test("delegated listeners are called", function (assert) {
var parent = h("div", [ h("div") ])
var child = parent.childNodes[0]
document.body.appendChild(parent)

Delegator()
var values = []

function fn(ev) {
values.push(ev)
}

addEvent(parent, "click", fn)
addEvent(child, "click", fn)

var ev = createEvent("click")
child.dispatchEvent(ev)

setImmediate(function () {
assert.equal(values.length, 2)
assert.equal(values[0].currentTarget, child)
assert.equal(values[1].currentTarget, parent)

document.body.removeChild(parent)
assert.end()
})
})

test("all handler does not prevent delegation", function (assert) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe - you could make the first test pass without making this test pass if you only change half of the condition at https://github.com/Raynos/dom-delegator/blob/v9.0.0/dom-delegator.js#L120

var parent = h("div", [ h("div") ])
var child = parent.childNodes[0]
document.body.appendChild(parent)

Delegator()
var values = []

function fn(ev) {
values.push(ev)
}

addEvent(parent, "click", fn)
addEvent(child, "event", fn)

var ev = createEvent("click")
child.dispatchEvent(ev)

setImmediate(function () {
assert.equal(values.length, 2)
assert.equal(values[0].currentTarget, child)
assert.equal(values[1].currentTarget, parent)

document.body.removeChild(parent)
assert.end()
})
})
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ require("./dom-data-events.js")
require("./dom-duplicate-event.js")
require("./dom-unlisten-to.js")
require("./dom-global-listeners.js")
require("./dom-delegation.js")