-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathregistry.test.html
More file actions
257 lines (232 loc) · 8.97 KB
/
registry.test.html
File metadata and controls
257 lines (232 loc) · 8.97 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
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
<!DOCTYPE html>
<html>
<head>
<title>CustomElementsRegistry</title>
<script>
(window.customElements =
window.customElements || {}).forcePolyfill = true;
</script>
<script src="../../node_modules/@webcomponents/webcomponentsjs/bundles/webcomponents-pf_js.js"></script>
<script src="../../node_modules/@webcomponents/custom-elements/custom-elements.min.js"></script>
</head>
<body>
<script type="module">
import {runTests, assert} from '../../environment.js';
import {safariGCBugWorkaround} from '../safari-gc-bug-workaround.js';
runTests(async () => {
suiteSetup(() => safariGCBugWorkaround());
/**
* @license
* Copyright (c) 2016 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
*/
suite('CustomElementsRegistry', function () {
var work;
var HTMLNS = 'http://www.w3.org/1999/xhtml';
customElements.enableFlush = true;
setup(function () {
work = document.createElement('div');
document.body.appendChild(work);
});
teardown(function () {
document.body.removeChild(work);
});
suite('window', function () {
test('customElements.define exists', function () {
assert.isFunction(customElements.define);
});
});
suite('define', function () {
test('requires a name argument', function () {
assert.throws(
function () {
customElements.define();
},
'',
'customElements.define failed to throw when given no arguments'
);
});
test('name must contain a dash', function () {
assert.throws(
function () {
customElements.define('xfoo', class extends HTMLElement {});
},
'',
'customElements.define failed to throw when given no arguments'
);
});
test('name must not be a reserved name', function () {
[
'annotation-xml',
'color-profile',
'font-face',
'font-face-src',
'font-face-uri',
'font-face-format',
'font-face-name',
'missing-glyph',
].forEach((invalidName) => {
assert.throws(
function () {
customElements.define(
invalidName,
class extends HTMLElement {}
);
},
'',
`Registration should fail for name '${invalidName}'.`
);
});
});
test('name must be unique', function () {
class XDuplicate extends HTMLElement {}
customElements.define('x-duplicate', XDuplicate);
assert.throws(
function () {
customElements.define('x-duplicate', XDuplicate);
},
'',
'customElements.define failed to throw when called multiple times with the same element name'
);
});
test('names are case-sensitive', function () {
class XCase extends HTMLElement {}
assert.throws(function () {
customElements.define('X-CASE', XCase);
});
});
test('requires a constructor argument', function () {
assert.throws(
function () {
customElements.define('x-no-options');
},
'',
'customElements.define failed to throw without a constructor argument'
);
});
test('second argument prototype property is required', function () {
assert.throws(
function () {
customElements.define('x-no-proto', {});
},
'',
'customElements.define failed to throw with a constructor argument with no prototype'
);
});
test('fails if reading callbacks fails', function () {
assert.throws(
function () {
customElements.define(
'x-bad-attributes',
class extends HTMLElement {
attributeChangedCallback() {}
static get observedAttributes() {
throw new Error('no attributes');
}
}
);
},
'',
'customElements.define failed to throw when reading callback throws.'
);
});
test('succeeds with named used for a failed definition with invalid class', function () {
try {
customElements.define('x-failed-define', {});
} catch (e) {} // eslint-disable-line no-empty
let error;
try {
customElements.define(
'x-failed-define',
class extends HTMLElement {}
);
} catch (e) {
error = e;
}
assert.isUndefined(
error,
'Error thrown when defining an element using the same name as a failed definition with an invalid class.'
);
});
test('succeeds with named used for a failed definition with invalid callbacks', function () {
try {
customElements.define(
'x-failed-define-callbacks',
class extends HTMLElement {
attributeChangedCallback() {}
static get observedAttributes() {
throw new Error('no attributes');
}
}
);
} catch (e) {} // eslint-disable-line no-empty
let error;
try {
customElements.define(
'x-failed-define-callbacks',
class extends HTMLElement {}
);
} catch (e) {
error = e;
}
assert.isUndefined(
error,
'Error thrown when defining an element using the same name as a failed definition with invalid callbacks.'
);
});
});
suite('get', function () {
test('returns a defined constructor', function () {
class XGet extends HTMLElement {}
customElements.define('x-get', XGet);
assert.equal(XGet, customElements.get('x-get'));
});
test('returns undefined for an undefined constructor', function () {
assert.isUndefined(customElements.get('x-undefined'));
});
});
suite('getName', function () {
test('returns the local name for a defined custom element constructor', function () {
class XGetName extends HTMLElement {}
customElements.define('x-get-name', XGetName);
var name = customElements.getName(XGetName);
assert.equal(name, 'x-get-name');
});
test('returns null for an undefined custom element constructor', function () {
class XUndefinedElement extends HTMLElement {}
var name = customElements.getName(XUndefinedElement);
assert.isNull(name);
});
});
suite('whenDefined', function () {
test('resolves when a tag is defined', function () {
var promise = customElements
.whenDefined('x-when-defined')
.then(function (r) {
assert.isUndefined(r);
});
class XDefined extends HTMLElement {}
customElements.define('x-when-defined', XDefined);
return promise;
});
test('throws for an invalid element name', function () {
return customElements.whenDefined('div').then(
function () {
assert.fail();
},
function () {
// pass
return;
}
);
});
});
});
});
</script>
</body>
</html>