Skip to content

Commit

Permalink
Merge pull request #37 from PolymerElements/custom-elements-v1
Browse files Browse the repository at this point in the history
<test-fixture> now works with Custom Elements v1
  • Loading branch information
e111077 authored Jan 9, 2017
2 parents 35210e3 + 242e4ed commit 365c7e6
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 12 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"devDependencies": {
"web-component-tester": "^4.0.0",
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0",
"webcomponentsjs-v1": "webcomponents/webcomponentsjs#v1"
},
"main": "test-fixture.html",
"ignore": []
Expand Down
30 changes: 24 additions & 6 deletions test-fixture.html
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,18 @@

forcePolyfillAttachedStateSynchrony: function () {
// Force synchrony in attachedCallback and detachedCallback where
// implemented, in the event that we are dealing with the async Web
// Components Polyfill.
if (window.CustomElements && window.CustomElements.takeRecords) {
// implemented, in the event that we are dealing with one of these async
// polyfills:
// 1. Web Components CustomElements polyfill (v1 or v0).
if (window.customElements && window.customElements.flush) {
window.customElements.flush();
} else if (window.CustomElements && window.CustomElements.takeRecords) {
window.CustomElements.takeRecords();
}
// 2. ShadyDOM polyfill.
if (window.ShadyDOM) {
window.ShadyDOM.flush();
}
},

collectElementChildren: function (parent) {
Expand Down Expand Up @@ -313,9 +320,20 @@
});

try {
document.registerElement('test-fixture', {
prototype: TestFixturePrototype
});
if (window.customElements) {
function TestFixture() {
return ((window.Reflect && Reflect.construct) ?
Reflect.construct(HTMLElement, [], TestFixture)
: HTMLElement.call(this)) || this;
}
TestFixture.prototype = TestFixturePrototype;
TestFixture.prototype.constructor = TestFixture;
customElements.define('test-fixture', TestFixture);
} else {
document.registerElement('test-fixture', {
prototype: TestFixturePrototype
});
}
} catch (e) {
if (window.WCT) {
console.warn('if you are using WCT, you do not need to manually import test-fixture.html');
Expand Down
9 changes: 5 additions & 4 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>

<link rel="import" href="../../polymer/polymer.html">

</head>
<body>
<script>
WCT.loadSuites([
'test-fixture.html',
'handle-multiple-registrations.html'
'test-fixture.html', // shady
'test-fixture.html?dom=shadow', // shadow
'test-fixture-v1.html?wc-shadydom=true&wc-ce=true', // shady
'test-fixture-v1.html', // shadow
'handle-multiple-registrations.html',
]);
</script>
</body>
Expand Down
262 changes: 262 additions & 0 deletions test/test-fixture-v1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
<!doctype html>
<!--
@license
Copyright (c) 2015 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>
<title>test-fixture</title>

<script>
// Save original HTMLElement constructor since customElements v1 polyfill
// overrides it https://github.com/webcomponents/webcomponentsjs/issues/623
var _HTMLElement = window.HTMLElement;
</script>

<script src="../../webcomponentsjs-v1/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>

<link rel="import" id="test-fixture-import" href="../test-fixture.html">
<script src="../test-fixture-mocha.js"></script>
</head>
<body>
<script>
(function() {
function XCustom() {
if (window.Reflect) {
return Reflect.construct(HTMLElement, [], XCustom)
}
return HTMLElement.call(this) || this;
}

XCustom.constructor = XCustom;
XCustom.prototype = Object.create(HTMLElement.prototype);
XCustom.prototype.onDetached = function() {};
XCustom.prototype.disconnectedCallback = function() {
this.onDetached();
};

customElements.define('x-custom', XCustom);
})();
</script>
<test-fixture id="TrivialFixture">
<template>
<div id="Foo"></div>
</template>
</test-fixture>
<test-fixture id="ComplexDomFixture">
<template>
<div id="Bar">
<div id="BarChild"></div>
</div>
<div id="BarSibling"></div>
</template>
</test-fixture>
<test-fixture id="MultiTemplateFixture">
<template>
<div id="Baz"></div>
</template>
<template>
<div id="Qux"></div>
<div id="QuxSibling"></div>
</template>
</test-fixture>
<test-fixture id="CommentedSingleChildFixture">
<template>
<!-- comment -->
<!-- comment -->
<div id="Foo"></div>
<!-- comment -->
</template>
</test-fixture>
<test-fixture id="CommentedMultiChildFixture">
<template>
<!-- comment -->
<div id="Bar">
<div id="BarChild"></div>
</div>
<!-- comment -->
<!-- comment -->
<div id="BarSibling"></div>
<!-- comment -->
<div id="Baz"></div>
</template>
</test-fixture>
<test-fixture id="AttachedFixture">
<template>
<x-custom></x-custom>
</template>
</test-fixture>
<script>
describe('test-fixture import', function() {
it('loads from test-fixture import', function() {
var importNode = document.getElementById('test-fixture-import');
expect(importNode.import).to.not.be.null;
});
});
describe('<test-fixture>', function () {
var trivialFixture;
var complexDomFixture;
var multiTemplateFixture;
var commentedSingleChildFixture;
var commentedMultiChildFixture;

beforeEach(function () {
trivialFixture = document.getElementById('TrivialFixture');
complexDomFixture = document.getElementById('ComplexDomFixture');
multiTemplateFixture = document.getElementById('MultiTemplateFixture');
commentedSingleChildFixture = document.getElementById('CommentedSingleChildFixture');
commentedMultiChildFixture = document.getElementById('CommentedMultiChildFixture');
});

afterEach(function () {
trivialFixture.restore();
complexDomFixture.restore();
multiTemplateFixture.restore();
commentedSingleChildFixture.restore();
commentedMultiChildFixture.restore();
});

describe('an stamped-out fixture', function () {
var attachedFixture;
var element;

beforeEach(function () {
attachedFixture = document.getElementById('AttachedFixture');
element = attachedFixture.create();
});

afterEach(function () {
attachedFixture.restore();
});

it('detaches the fixtured DOM when it is restored', function () {
var detached = false;

element.onDetached = function () {
detached = true;
};

attachedFixture.restore();
expect(detached).to.be.eql(true);
});
});

describe('when create is called', function () {
var el;

beforeEach(function () {
el = trivialFixture.create();
});

it('clones all template fragments within itself', function () {
expect(el).to.be.ok;
expect(document.getElementById('Foo')).to.be.ok;
});

it('detaches all fixture templates from itself', function () {
expect(trivialFixture.querySelectorAll('template').length).to.be.equal(0);
});

describe('and then restore is called', function () {
beforeEach(function () {
trivialFixture.restore();
});

it('re-attaches all fixture templates', function () {
expect(trivialFixture.querySelectorAll('template').length).to.be.equal(1);
});

it('removes all cloned elements from itself', function () {
expect(document.getElementById('Foo')).to.not.be.ok;
});
});

describe('for a dom with a single root element', function () {
it('returns a reference to the root element', function () {
expect(el).to.be.instanceOf(_HTMLElement);
});
});

describe('for a complex dom', function () {
var els;

beforeEach(function () {
els = complexDomFixture.create();
});

it('fixtures all the dom elements in the template', function () {
expect(document.getElementById('Bar')).to.be.ok;
expect(document.getElementById('BarSibling')).to.be.ok;
expect(document.getElementById('BarChild')).to.be.ok;
});

it('returns an array of root elements', function () {
expect(els).to.be.instanceOf(Array);
expect(els[0]).to.be.instanceOf(_HTMLElement);
expect(els[1]).to.be.instanceOf(_HTMLElement);
});
});

describe('when there are multiple templates', function () {
var groups;

beforeEach(function () {
groups = multiTemplateFixture.create();
});

it('fixtures elements from all of the templates', function () {
expect(document.getElementById('Baz')).to.be.ok;
expect(document.getElementById('Qux')).to.be.ok;
});

it('returns an array with elements grouped by template', function () {
expect(groups).to.be.instanceOf(Array);
expect(groups[0]).to.be.instanceOf(_HTMLElement);
expect(groups[1]).to.be.instanceOf(Array);
expect(groups[1][0]).to.be.instanceOf(_HTMLElement);
expect(groups[1][1]).to.be.instanceOf(_HTMLElement);
});
});

describe('when there are comments in the template', function() {
var el;
var els;

beforeEach(function () {
el = commentedSingleChildFixture.create();
els = commentedMultiChildFixture.create();
});

it('returns a single element if the template has a single child', function() {
expect(el).to.be.instanceOf(_HTMLElement);
});

it('returns multiple elements if the template has multiple children', function() {
expect(els).to.be.instanceOf(Array);
expect(els.length).to.equal(3);
});
});
});

describe('when the fixture global is called', function () {
var el;

beforeEach(function () {
el = fixture('TrivialFixture');
});

it('generates a DOM fragment from the associated fixture', function () {
expect(el).to.be.equal(document.getElementById('Foo'));
});
});
});
</script>

</body>
</html>
2 changes: 1 addition & 1 deletion test/test-fixture.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<script src="../test-fixture-mocha.js"></script>

<link rel="import" id="test-fixture-import" href="../test-fixture.html">
<script src="../test-fixture-mocha.js"></script>
</head>
<body>
<script>
Expand Down

0 comments on commit 365c7e6

Please sign in to comment.