-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from PolymerElements/custom-elements-v1
<test-fixture> now works with Custom Elements v1
- Loading branch information
Showing
5 changed files
with
294 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters