-
Notifications
You must be signed in to change notification settings - Fork 1
/
fx-observer-test.html
115 lines (89 loc) · 3.37 KB
/
fx-observer-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
104
105
106
107
108
109
110
111
112
113
114
115
<!--
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-observer.html">
<script>
"use strict";
describe("fx-observer", function() {
var container;
beforeEach(function() {
container = document.body.appendChild(document.createElement("div"));
});
afterEach(function() {
container.remove();
container = null;
});
function createObserver(type, listener) {
var observer = new FxObserver();
observer.for = type;
if (listener)
observer.addEventListener("notify", listener);
return observer;
}
it("should broadcast events to instances", function() {
var events = [];
function listener(event) {
events.push(event);
assert.ok(event.detail);
assert.ok(event.detail.data, "this is data");
}
var observer1 = container.appendChild(createObserver("example", listener));
var observer2 = container.appendChild(createObserver("example", listener));
observer2.notify({data: "this is data"});
assert.equal(events.length, 2);
assert.equal(events[0].target, observer1);
assert.equal(events[1].target, observer2);
});
it("should only broadcast to and from attached instances", function() {
var events = [];
function listener(event) {
events.push(event);
assert.ok(event.detail);
assert.ok(event.detail.data, "this is data");
}
var observer1 = createObserver("example", listener);
var observer2 = container.appendChild(createObserver("example", listener));
observer1.notify({data: "this should not send"});
observer2.notify({data: "this is data"});
assert.equal(events.length, 1);
assert.equal(events[0].target, observer2);
events = [];
container.appendChild(observer1);
observer1.notify({data: "this is data"});
assert.equal(events.length, 2);
assert.equal(events[0].target, observer2);
assert.equal(events[1].target, observer1);
});
it("should support async notify", function() {
var events = [];
function listener(event) {
events.push(event);
}
var observer = container.appendChild(createObserver("example", listener));
return observer.notifyAsync({value: 100}).then(function(value) {
assert.notOk(value);
assert.equal(events.length, 1);
assert.ok(events[0].detail);
assert.equal(events[0].detail.value, 100);
});
});
it("should allow static notification", function() {
var events = [];
function listener(event) {
events.push(event);
}
FxObserver.notify("example", {value: 1});
var observer = container.appendChild(createObserver("example", listener));
FxObserver.notify("example", {value: 2});
FxObserver.notify("example2", {value: 2});
assert.equal(events.length, 1);
assert.equal(events[0].detail.value, 2);
return FxObserver.notifyAsync("example", {value:3}).then(function() {
assert.equal(events.length, 2);
assert.equal(events[1].detail.value, 3);
});
});
});
</script>