-
Notifications
You must be signed in to change notification settings - Fork 1
/
fx-listener-test.html
103 lines (95 loc) · 3.3 KB
/
fx-listener-test.html
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
<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="fx-listener.html">
<script>
"use strict";
describe("fx-listener", function() {
var container;
beforeEach(function() {
container = document.body.appendChild(document.createElement("div"));
});
afterEach(function() {
if (container)
container.remove();
container = null;
});
it("should listen for events on the window", function() {
var events = [];
var listener = new FxListener();
listener.setAttribute("on-test-event", "ignored");
listener.addEventListener("test-event", function(event) {
events.push(event);
});
window.dispatchEvent(new CustomEvent("test-event"));
container.appendChild(listener);
var event = new CustomEvent("test-event");
window.dispatchEvent(event);
listener.remove();
window.dispatchEvent(new CustomEvent("test-event"));
assert.equal(events.length, 1);
assert.equal(events[0].detail, event);
});
it("should listen for events on child objects", function() {
var events = [];
var listener = new FxListener();
listener.target = "document.body";
listener.setAttribute("on-click", "ignored");
listener.addEventListener("click", function(event) {
events.push(event);
});
document.body.click();
container.appendChild(listener);
document.body.click();
listener.remove();
document.body.click();
assert.equal(events.length, 1);
});
it("should fire listeners in registration order", function() {
var events = [];
function handleEvent(event) {
events.push({
listener: this,
event: event,
});
}
var a = new FxListener();
var b = new FxListener();
a.addEventListener("example", handleEvent);
b.addEventListener("example", handleEvent);
a.setAttribute("on-example", "");
b.setAttribute("on-example", "");
container.appendChild(b);
container.appendChild(a);
window.dispatchEvent(new CustomEvent("example"));
assert.equal(events.length, 2);
assert.equal(events[0].listener, b);
assert.equal(events[1].listener, a);
});
it("should not mix listeners", function() {
var events = [];
function handleEvent(event) {
events.push({
listener: this,
event: event,
});
}
var a = new FxListener();
var b = new FxListener();
a.addEventListener("example1", handleEvent);
b.addEventListener("example2", handleEvent);
a.setAttribute("on-example1", "");
b.setAttribute("on-example2", "");
container.appendChild(a);
container.appendChild(b);
window.dispatchEvent(new CustomEvent("example2"));
assert.equal(events.length, 1);
assert.equal(events[0].listener, b);
window.dispatchEvent(new CustomEvent("example1"));
assert.equal(events.length, 2);
assert.equal(events[1].listener, a);
});
});
</script>