-
Notifications
You must be signed in to change notification settings - Fork 16
/
dom-remove-event.js
103 lines (76 loc) · 2.28 KB
/
dom-remove-event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 removeEvent = require("../remove-event.js")
var Delegator = require("../index.js")
test("removing event", function (assert) {
var elem = h("div")
document.body.appendChild(elem)
Delegator()
var values = []
function fn(ev) {
values.push(ev)
}
addEvent(elem, "click", fn)
removeEvent(elem, "click", fn)
var ev = createEvent("click")
elem.dispatchEvent(ev)
setImmediate(function () {
assert.equal(values.length, 0)
document.body.removeChild(elem)
assert.end()
})
})
test("remove one of multiple events", function (assert) {
var elem = h("div")
document.body.appendChild(elem)
var d = Delegator()
var values = []
function fn1(ev) {
values.push(["fn1", ev])
}
function fn2(ev) {
values.push(["fn2", ev])
}
function fn3(ev) {
values.push(["fn3", ev])
}
d.addEventListener(elem, "click", fn1)
d.addEventListener(elem, "click", fn2)
d.addEventListener(elem, "click", fn3)
d.removeEventListener(elem, "click", fn2)
var ev = createEvent("click")
elem.dispatchEvent(ev)
setImmediate(function () {
assert.equal(values.length, 2)
assert.equal(values[0][0], "fn1")
assert.equal(values[1][0], "fn3")
assert.equal(values[0][1].target, elem)
document.body.removeChild(elem)
assert.end()
})
})
test("removing event doesn't throw", function (assert) {
var elem = h("div")
document.body.appendChild(elem)
var d = Delegator()
assert.doesNotThrow(function () {
d.removeEventListener(elem, "click", function () {})
})
document.body.removeChild(elem)
assert.end()
})
test("removing other listener", function (assert) {
var elem = h("div")
document.body.appendChild(elem)
var d = Delegator()
d.addEventListener(elem, "click", function () {})
assert.doesNotThrow(function () {
d.removeEventListener(elem, "click", function () {})
})
document.body.removeChild(elem)
assert.end()
})