-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfx-shared-instance-test.html
More file actions
157 lines (127 loc) · 5.24 KB
/
fx-shared-instance-test.html
File metadata and controls
157 lines (127 loc) · 5.24 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!--
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-shared-instance.html">
<polymer-element name="fx-shared-instance-test" extends="fx-shared-instance" noscript></polymer>
<script>
"use strict";
describe("fx-shared-instance", function() {
var container;
var events;
beforeEach(function() {
container = document.body.appendChild(document.createElement("div"));
events = [];
});
afterEach(function() {
container.remove();
container = null;
events = null;
});
function defaultCallback(data) {
events.push({
instance: this,
data: data
});
}
function createInstance(type, method, callback) {
var observer = document.createElement("fx-shared-instance-test");
if (type)
observer.instanceType = type;
if (method && callback)
observer[method] = callback;
else if (method)
observer[method] = defaultCallback;
return observer;
}
it("should notify all instances", function() {
var a = container.appendChild(createInstance("", "example"));
var b = container.appendChild(createInstance("", "example"));
a.notifyInstances("example", {value: 100});
assert.equal(events.length, 2);
assert.equal(events[0].instance, a);
assert.equal(events[0].data.value, 100);
assert.equal(events[1].instance, b);
assert.equal(events[1].data.value, 100);
});
it("should not fail when message type is not a function", function() {
var a = container.appendChild(createInstance(""));
a.notifyInstances("not a function");
a.notifyInstances("__proto__");
assert.equal(events.length, 0);
});
it("should only notify attached instances", function() {
var a = container.appendChild(createInstance("", "example"));
var b = container.appendChild(createInstance("", "example"));
a.remove();
a.notifyInstances("example");
assert.equal(events.length, 1);
assert.equal(events[0].instance, b);
events = [];
b.remove();
b.notifyInstances("example");
assert.equal(events.length, 0);
container.appendChild(b);
container.appendChild(a);
events = [];
b.notifyInstances("example");
assert.equal(events.length, 2);
assert.equal(events[0].instance, b);
assert.equal(events[1].instance, a);
});
it("should notify all instances async", function() {
var a = container.appendChild(createInstance("", "example"));
var b = container.appendChild(createInstance("", "example"));
return a.notifyInstancesAsync("example", {value: 100}).then(function(value) {
assert.equal(value, undefined);
assert.equal(events.length, 2);
assert.equal(events[0].instance, a);
assert.equal(events[0].data.value, 100);
assert.equal(events[1].instance, b);
assert.equal(events[1].data.value, 100);
});
});
it("should allow static notification", function() {
FxSharedInstance.notify("fx-shared-instance-test", {value: 1});
var a = container.appendChild(createInstance("", "example"));
FxSharedInstance.notify("fx-shared-instance-test", "example", {value: 2});
FxSharedInstance.notify("not-an-element", "example", {value: 2});
assert.equal(events.length, 1);
assert.equal(events[0].instance, a);
assert.equal(events[0].data.value, 2);
return FxSharedInstance.notifyAsync("fx-shared-instance-test", "example", {value:3}).then(function() {
assert.equal(events.length, 2);
assert.equal(events[1].instance, a);
assert.equal(events[1].data.value, 3);
});
});
it("should allow getting all instances", function() {
var a = container.appendChild(createInstance());
var b = container.appendChild(createInstance());
assert.equal(a.getInstances().length, 2);
assert.deepEqual(a.getInstances(), [a, b]);
var instances = FxSharedInstance.getInstances("fx-shared-instance-test");
assert.equal(instances.length, 2);
assert.deepEqual(instances, [a, b]);
a.remove();
assert.equal(a.getInstances().length, 1);
assert.deepEqual(a.getInstances(), [b]);
var instances = FxSharedInstance.getInstances("fx-shared-instance-test");
assert.equal(instances.length, 1);
assert.deepEqual(instances, [b]);
b.remove();
assert.equal(b.getInstances().length, 0);
var instances = FxSharedInstance.getInstances("fx-shared-instance-test");
assert.equal(instances.length, 0);
});
it("should allow setting an explicit instance type", function() {
var a = container.appendChild(createInstance("ex-test"));
assert.equal(a.getInstances().length, 1);
assert.deepEqual(a.getInstances(), [a]);
var instances = FxSharedInstance.getInstances("ex-test");
assert.equal(instances.length, 1);
assert.deepEqual(instances, [a]);
});
});
</script>