Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Jul 18, 2018
1 parent ada72d4 commit 2a38d38
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'unit/custom-style-async.html',
'unit/custom-style-scope-cache.html',
'unit/events.html',
'unit/strict-template-policy.html',
'unit/template-whitespace.html',
'unit/resolveurl.html',
'unit/case-map.html',
Expand Down
217 changes: 217 additions & 0 deletions test/unit/strict-template-policy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<!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>
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script>
window.strictTemplatePolicy = true;
// Errors thrown in custom element reactions are not thrown up
// the call stack to the dom methods that provoked them, so need
// to catch them here and prevent mocha from complaining about them
window.addEventListener('error', function(event) {
event.stopImmediatePropagation();
if (!window.globalError) {
window.globalError = event;
}
});
</script>
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="../../polymer.html">
<script>
HTMLImports.whenReady(() => {
// Errors thrown in Polymer's debouncer queue get re-thrown
// via setTimeout, making them particulary difficult to verify;
// Wrap debouncer callbacks and store on the globalError to test later
const debounce = Polymer.Debouncer.debounce;
Polymer.Debouncer.debounce = function(debouncer, asyncModule, callback) {
return debounce(debouncer, asyncModule, function() {
try {
callback();
} catch(error) {
window.globalError = error;
}
});
};
});
</script>
</head>
<body>

<dom-module id="trusted-element">
<template>Trusted</template>
<script>
HTMLImports.whenReady(function() {
class TrustedElement extends Polymer.Element {
static get is() { return 'trusted-element'; }
}
customElements.define(TrustedElement.is, TrustedElement);
});
</script>
</dom-module>

<dom-module id="trusted-element-legacy">
<template>Trusted</template>
<script>
HTMLImports.whenReady(function() {
Polymer({is: 'trusted-element-legacy'});
});
</script>
</dom-module>
<div id="target"></div>

<script>
suite('strictTemplatePolicy', function() {

teardown(function() {
window.globalError = null;
});

function assertThrows(fn, re) {
// Safari does not forward error messages
re = new RegExp('(' + re.toString().slice(1,-1) + ')|(Script error\\.)', re.flags);
assert.throws(function() {
fn();
// Throw any errors caught on window
if (window.globalError) {
throw new Error(window.globalError.message);
}
// Force polyfill reactions and/or async template stamping
Polymer.flush();
// Throw any add'l errors caught on window
if (window.globalError) {
throw new Error(window.globalError.message);
}
}, re);
}

test('dom-bind', function() {
assertThrows(function() {
document.getElementById('target').innerHTML =
'<dom-bind>' +
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-bind>';
}, /dom-bind not allowed/);
assert.notOk(document.getElementById('injected'));
});

test('dom-if', function() {
assertThrows(function() {
document.getElementById('target').innerHTML =
'<dom-if if>' +
' <template>' +
' <div id="injected"></div>'+
' </template>' +
'</dom-if>';
}, /template owner not trusted/);
assert.notOk(document.getElementById('injected'));
});

test('dom-repeat', function() {
assertThrows(function() {
document.getElementById('target').innerHTML =
'<dom-repeat items="[0]">' +
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-repeat>';
}, /template owner not trusted/);
assert.notOk(document.getElementById('injected'));
});

test('dom-module after registration', function() {
assertThrows(function() {
document.getElementById('target').innerHTML =
'<dom-module id="trusted-element">' +
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-module>' +
'<trusted-element></trusted-element>';
}, /trusted-element registered twice/);
const el = document.querySelector('trusted-element');
assert.notOk(el && el.shadowRoot && el.shadowRoot.querySelector('#injected'));
});

test('dom-module before registration', function() {
document.getElementById('target').innerHTML =
'<dom-module id="has-no-template">' +
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-module>';
class HasNoTemplate extends Polymer.Element {
static get is() { return 'has-no-template'; }
static get template() { return null; }
}
customElements.define(HasNoTemplate.is, HasNoTemplate);
let el = document.createElement('has-no-template');
document.getElementById('target').appendChild(el);
assert.notOk(el.shadowRoot);
});

test('dom-module after registration (legacy)', function() {
assertThrows(function() {
document.getElementById('target').innerHTML =
'<dom-module id="trusted-element-legacy">' +
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-module>' +
'<trusted-element-legacy></trusted-element-legacy>';
}, /trusted-element-legacy registered twice/);
const el = document.querySelector('trusted-element-legacy');
assert.notOk(el && el.shadowRoot && el.shadowRoot.querySelector('#injected'));
});

test('dom-module before registration (legacy)', function() {
document.getElementById('target').innerHTML =
'<dom-module id="has-no-template-legacy">' +
' <template>' +
' <div id="injected"></div>'+
' </template>`' +
'</dom-module>';
Polymer({
is: 'has-no-template-legacy',
_template: null
});
let el = document.createElement('has-no-template');
document.getElementById('target').appendChild(el);
assert.notOk(document.querySelector('has-no-template').shadowRoot);
});

test('element without explicit template throws', function() {
assertThrows(function() {
class HasNoTemplateThrows extends Polymer.Element {
static get is() { return 'has-no-template-throws'; }
};
customElements.define(HasNoTemplateThrows.is, HasNoTemplateThrows);
var el = document.createElement('has-no-template-throws');
document.getElementById('target').appendChild(el);
}, /expecting dom-module or null template/);
});

test('element without explicit template throws (legacy)', function() {
assertThrows(function() {
Polymer({
is: 'has-no-template-throws-legacy'
});
var el = document.createElement('has-no-template-throws-legacy');
document.getElementById('target').appendChild(el);
}, /expecting dom-module or null _template/);
});

});
</script>

</body>
</html>

0 comments on commit 2a38d38

Please sign in to comment.