-
Notifications
You must be signed in to change notification settings - Fork 2k
/
dom-bind.html
283 lines (227 loc) · 7.75 KB
/
dom-bind.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<!doctype html>
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<meta charset="utf-8">
<script src="../../node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>
<script src="wct-browser-config.js"></script>
<script src="../../node_modules/wct-browser-legacy/browser.js"></script>
</head>
<body>
<dom-bind id="earlyDomBind">
<template>
<div id="earlyBoundChild">{{value}}</div>
</template>
</dom-bind>
<script type="module">
import './dom-bind-elements1.js';
import './dom-bind-elements2.js';
/* global earlyDomBind */
earlyDomBind.value = 'hi!';
</script>
<script type="module" src="../../polymer-legacy.js"></script>
<script type="module" src="./dom-bind-elements1.js"></script>
<dom-bind id="declarativeDomBind">
<template>
<x-basic id="declaredXBasic1" value="{{value}}" notifyingvalue="{{nvalue}}" on-custom="handleEvent" on-tap="handleTap" computed="{{compute(dep)}}"></x-basic>
<x-basic id="declaredXBasic2" value="{{value}}" notifyingvalue="{{nvalue}}"></x-basic>
<x-produce-a bind-to-text={{boundText}}></x-produce-a>
<div id="boundTextDiv">{{boundText}}</div>
</template>
</dom-bind>
<div id="container">
</div>
<dom-bind id="timingDomBind" config="config">
<template>
<x-needs-host id="needsHost"></x-needs-host>
</template>
</dom-bind>
<script type="module">
import './dom-bind-elements1.js';
import './dom-bind-elements2.js';
</script>
<div id="nonUpgrade">
<dom-bind>
<template>stamped</template>
</dom-bind>
</div>
<script type="module">
import './dom-bind-elements1.js';
import './dom-bind-elements2.js';
import { DomBind } from '../../lib/elements/dom-bind.js';
import { flush } from '../../lib/utils/flush.js';
suite('Polymer.DomBind class', function() {
test('is available', function() {
assert.isDefined(DomBind);
assert.equal(typeof DomBind, 'function');
});
});
suite('dom-bind touched before upgrade', function() {
test('value binds top-down', function() {
/* global earlyBoundChild*/
assert.equal(earlyBoundChild.textContent, 'hi!');
});
});
suite('declarative dom-bind', function() {
var domBind;
var el1;
var el2;
setup(function() {
/* global declarativeDomBind declaredXBasic1 declaredXBasic2 */
domBind = declarativeDomBind;
el1 = declaredXBasic1;
el2 = declaredXBasic2;
});
test('value binds top-down', function() {
domBind.value = 'foo';
assert.equal(el1.value, 'foo');
assert.equal(el2.value, 'foo');
});
test('notifyingvalue binds from child to child', function() {
el1.notifyingvalue = 'bar';
assert.equal(domBind.nvalue, 'bar');
assert.equal(el2.notifyingvalue, 'bar');
});
test('event listener fires', function() {
domBind.handleEvent = sinon.spy();
el1.fire('custom');
assert.equal(domBind.handleEvent.callCount, 1);
});
test('gesture event listener fires', function() {
domBind.handleTap = sinon.spy();
el1.click();
assert.equal(domBind.handleTap.callCount, 1);
});
test('inline function runs', function() {
domBind.compute = function(val) {
return val * 10;
};
domBind.dep = 5;
assert.equal(el1.computed, 50);
});
test('initial value notifies to dom-bind', function() {
assert.equal(domBind.boundText, 'this text is bound');
/* global boundTextDiv */
assert.equal(boundTextDiv.textContent, 'this text is bound');
});
});
suite('imperative dom-bind', function() {
var domBind;
var el1;
var el2;
setup(function() {
domBind = document.createElement('dom-bind');
var template = document.createElement('template');
domBind.appendChild(template);
var doc = template.content.ownerDocument;
el1 = doc.createElement('x-basic');
el1.setAttribute('id', 'impEl1');
el1.setAttribute('value', '{{value}}');
el1.setAttribute('notifyingvalue', '{{nvalue}}');
el1.setAttribute('on-custom', 'handleEvent');
el1.setAttribute('on-tap', 'handleTap');
el1.setAttribute('computed', '{{compute(dep)}}');
el2 = doc.createElement('x-basic');
el2.setAttribute('id', 'impEl2');
el2.setAttribute('value', '{{value}}');
el2.setAttribute('notifyingvalue', '{{nvalue}}');
template.content.appendChild(el1);
template.content.appendChild(el2);
document.body.appendChild(domBind);
el1 = domBind.$.impEl1;
el2 = domBind.$.impEl2;
});
teardown(function() {
if (domBind.parentElement) {
domBind.parentElement.removeChild(domBind);
}
});
test('value binds top-down', function() {
domBind.value = 'foo';
assert.equal(el1.value, 'foo');
assert.equal(el2.value, 'foo');
});
test('notifyingvalue binds from child to child', function() {
el1.notifyingvalue = 'bar';
assert.equal(domBind.nvalue, 'bar');
assert.equal(el2.notifyingvalue, 'bar');
});
test('event listener fires', function() {
domBind.handleEvent = sinon.spy();
el1.fire('custom');
assert.equal(domBind.handleEvent.callCount, 1);
});
test('gesture event listener fires', function() {
domBind.handleTap = sinon.spy();
el1.click();
assert.equal(domBind.handleTap.callCount, 1);
});
test('inline function runs', function() {
domBind.compute = function(val) {
return val * 10;
};
domBind.dep = 5;
assert.equal(el1.computed, 50);
});
test('move dom-bind', function( ) {
/* global container */
container.appendChild(domBind);
assert.equal(container.firstElementChild, el1);
assert.equal(container.firstElementChild.nextElementSibling, el2);
});
test('remove dom-bind', function() {
assert(document.body.contains(el1));
assert(document.body.contains(el2));
domBind.parentElement.removeChild(domBind);
assert(!document.body.contains(el1));
assert(!document.body.contains(el2));
});
test('dom-bind distributed when inserted in element attached',
function() {
var el = document.createElement('x-attach-dom-bind');
document.body.appendChild(el);
// Flush CE & distribution
flush();
assert.equal(el.$.local.$.slot.assignedNodes()[0].textContent, 'hey',
'dom-bind did not distribute');
document.body.removeChild(el);
});
test('dom-bind distributed when inserted dynamically', function() {
var composeEl = document.createElement('x-compose');
document.body.appendChild(composeEl);
var dynamicDomBind = document.createElement('dom-bind');
var dynamicTemplate = document.createElement('template');
dynamicDomBind.appendChild(dynamicTemplate);
var span = document.createElement('span');
span.innerHTML = '{{hello}}';
dynamicTemplate.content.appendChild(span);
dynamicDomBind.hello = 'hey';
composeEl.$.local.appendChild(dynamicDomBind);
// Flush CE & distribution
flush();
assert.equal(composeEl.$.local.$.slot.assignedNodes()[0].textContent, 'hey',
'dom-bind did not distribute');
document.body.removeChild(composeEl);
});
});
/* global needsHost nonUpgrade */
suite('timing', function() {
test('late-loaded import should block stamping', function() {
assert.equal(needsHost.config, 'config');
});
test('non-upgrade case finds template', function() {
assert.equal(nonUpgrade.textContent.trim(), 'stamped');
});
});
</script>
<script type="module" src="./dom-bind-elements2.js"></script>
</body>
</html>