@polymer/polymer
Version:
The Polymer library makes it easy to create your own web components. Give your element some markup and properties, and then use it on a site. Polymer provides features like dynamic templates and data binding to reduce the amount of boilerplate you need to
136 lines (115 loc) • 5.03 kB
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="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="dynamic-imports/dynamic-element.html">
</head>
<body>
<dynamic-element></dynamic-element>
<script>
suite('dynamic imports', function() {
test('use importHref to load and create an element', function(done) {
var d = document.querySelector('dynamic-element');
d.whenDynamicContentReady(function() {
assert.isTrue(d.isAttached, true, 'dynamic element not readied');
assert.ok(d.$.content, 'dynamic element does not have content');
assert.isTrue(d.$.outer.isAttached, true, 'dynamic sub-element not readied');
assert.ok(d.$.outer.$.content, 'dynamic sub-element does not have content');
assert.isTrue(d.$.outer.$.inner.isAttached, true, 'dynamic sub-element not readied');
assert.ok(d.$.outer.$.inner.$.content, 'dynamic sub-element does not have content');
done();
});
});
suite('async/sync loading', function() {
var url = 'dynamic-imports/dynamic-element.html';
test('importHref does not cache failed results', function(done) {
Polymer.Base.importHref('does_not_exist.html', function() {
throw new Error('Load handler should not be called.');
}, function() {
var link = document.head
.querySelector('[href="does_not_exist.html"]');
assert.isNotOk(link, 'The link should not exist');
done();
});
});
test('importHref caches successful results', function(done) {
var targetUrl = 'dynamic-imports/async-import.html';
Polymer.Base.importHref(targetUrl, function(event) {
var targetOne = event.target;
Polymer.Base.importHref(targetUrl, function(event) {
var targetTwo = event.target;
assert.isOk(targetOne, 'Event target should be available');
assert.strictEqual(targetOne, targetTwo,
'Link element references should be identical');
done();
}, done);
}, done);
});
test('importHref sync loads by default', function(done) {
Polymer.Base.importHref(url, function(e) {
assert.isFalse(e.target.hasAttribute('async'),
'sync load is default');
done();
});
});
test('importHref sync called again, triggers load', function(done) {
Polymer.Base.importHref(url, function() {
Polymer.Base.importHref(url, function() {
done();
});
});
});
test('importHref async loading', function(done) {
Polymer.Base.importHref('dynamic-imports/async-import.html', function(e) {
assert.isTrue(window.asyncImportLoaded, 'flag not set from import');
assert.isTrue(e.target.hasAttribute('async'), 'async load');
assert.ok(e.target.import);
done();
}, null, true);
});
test('importHref does not leak event listeners on load', function(done) {
var errorSpy = sinon.spy();
var loadSpy = sinon.spy(function(e) {
var target = e.target;
target.dispatchEvent(new Event('error'));
assert.isFalse(errorSpy.called, 'doesn\'t trigger the error event listener');
setTimeout(function() {
loadSpy.reset();
target.dispatchEvent(new Event('load'));
assert.isFalse(loadSpy.called, 'doesn\'t trigger the load event listener');
done();
});
});
Polymer.Base.importHref('dynamic-imports/async-import.html', loadSpy, errorSpy, true);
});
test('importHref does not leak event listeners on error', function(done) {
var loadSpy = sinon.spy();
var errorSpy = sinon.spy(function(e) {
var target = e.target;
target.dispatchEvent(new Event('load'));
assert.isFalse(loadSpy.called, 'doesn\'t trigger the load event listener');
setTimeout(function() {
errorSpy.reset();
target.dispatchEvent(new Event('error'));
assert.isFalse(errorSpy.called, 'doesn\'t trigger the error event listener');
done();
});
});
Polymer.Base.importHref('dynamic-imports/async-import-invalid.html', loadSpy, errorSpy, true);
});
});
});
</script>
</body>
</html>