|
| 1 | +<!doctype html> |
| 2 | +<!-- |
| 3 | +@license |
| 4 | +Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 5 | +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt |
| 6 | +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 7 | +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt |
| 8 | +Code distributed by Google as part of the polymer project is also |
| 9 | +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt |
| 10 | +--> |
| 11 | +<html> |
| 12 | +<head> |
| 13 | + <title>test-fixture</title> |
| 14 | + |
| 15 | + <script> |
| 16 | + // Save original HTMLElement constructor since customElements v1 polyfill |
| 17 | + // overrides it https://github.com/webcomponents/webcomponentsjs/issues/623 |
| 18 | + var _HTMLElement = window.HTMLElement; |
| 19 | + </script> |
| 20 | + |
| 21 | + <script src="../../webcomponentsjs-v1/webcomponents-lite.js"></script> |
| 22 | + <script src="../../web-component-tester/browser.js"></script> |
| 23 | + |
| 24 | + <link rel="import" id="test-fixture-import" href="../test-fixture.html"> |
| 25 | + <script src="../test-fixture-mocha.js"></script> |
| 26 | +</head> |
| 27 | +<body> |
| 28 | + <script> |
| 29 | + (function() { |
| 30 | + function XCustom() { |
| 31 | + if (window.Reflect) { |
| 32 | + return Reflect.construct(HTMLElement, [], XCustom) |
| 33 | + } |
| 34 | + return HTMLElement.call(this) || this; |
| 35 | + } |
| 36 | + |
| 37 | + XCustom.constructor = XCustom; |
| 38 | + XCustom.prototype = Object.create(HTMLElement.prototype); |
| 39 | + XCustom.prototype.onDetached = function() {}; |
| 40 | + XCustom.prototype.disconnectedCallback = function() { |
| 41 | + this.onDetached(); |
| 42 | + }; |
| 43 | + |
| 44 | + customElements.define('x-custom', XCustom); |
| 45 | + })(); |
| 46 | + </script> |
| 47 | + <test-fixture id="TrivialFixture"> |
| 48 | + <template> |
| 49 | + <div id="Foo"></div> |
| 50 | + </template> |
| 51 | + </test-fixture> |
| 52 | + <test-fixture id="ComplexDomFixture"> |
| 53 | + <template> |
| 54 | + <div id="Bar"> |
| 55 | + <div id="BarChild"></div> |
| 56 | + </div> |
| 57 | + <div id="BarSibling"></div> |
| 58 | + </template> |
| 59 | + </test-fixture> |
| 60 | + <test-fixture id="MultiTemplateFixture"> |
| 61 | + <template> |
| 62 | + <div id="Baz"></div> |
| 63 | + </template> |
| 64 | + <template> |
| 65 | + <div id="Qux"></div> |
| 66 | + <div id="QuxSibling"></div> |
| 67 | + </template> |
| 68 | + </test-fixture> |
| 69 | + <test-fixture id="CommentedSingleChildFixture"> |
| 70 | + <template> |
| 71 | + <!-- comment --> |
| 72 | + <!-- comment --> |
| 73 | + <div id="Foo"></div> |
| 74 | + <!-- comment --> |
| 75 | + </template> |
| 76 | + </test-fixture> |
| 77 | + <test-fixture id="CommentedMultiChildFixture"> |
| 78 | + <template> |
| 79 | + <!-- comment --> |
| 80 | + <div id="Bar"> |
| 81 | + <div id="BarChild"></div> |
| 82 | + </div> |
| 83 | + <!-- comment --> |
| 84 | + <!-- comment --> |
| 85 | + <div id="BarSibling"></div> |
| 86 | + <!-- comment --> |
| 87 | + <div id="Baz"></div> |
| 88 | + </template> |
| 89 | + </test-fixture> |
| 90 | + <test-fixture id="AttachedFixture"> |
| 91 | + <template> |
| 92 | + <x-custom></x-custom> |
| 93 | + </template> |
| 94 | + </test-fixture> |
| 95 | + <script> |
| 96 | +describe('test-fixture import', function() { |
| 97 | + it('loads from test-fixture import', function() { |
| 98 | + var importNode = document.getElementById('test-fixture-import'); |
| 99 | + expect(importNode.import).to.not.be.null; |
| 100 | + }); |
| 101 | +}); |
| 102 | +describe('<test-fixture>', function () { |
| 103 | + var trivialFixture; |
| 104 | + var complexDomFixture; |
| 105 | + var multiTemplateFixture; |
| 106 | + var commentedSingleChildFixture; |
| 107 | + var commentedMultiChildFixture; |
| 108 | + |
| 109 | + beforeEach(function () { |
| 110 | + trivialFixture = document.getElementById('TrivialFixture'); |
| 111 | + complexDomFixture = document.getElementById('ComplexDomFixture'); |
| 112 | + multiTemplateFixture = document.getElementById('MultiTemplateFixture'); |
| 113 | + commentedSingleChildFixture = document.getElementById('CommentedSingleChildFixture'); |
| 114 | + commentedMultiChildFixture = document.getElementById('CommentedMultiChildFixture'); |
| 115 | + }); |
| 116 | + |
| 117 | + afterEach(function () { |
| 118 | + trivialFixture.restore(); |
| 119 | + complexDomFixture.restore(); |
| 120 | + multiTemplateFixture.restore(); |
| 121 | + commentedSingleChildFixture.restore(); |
| 122 | + commentedMultiChildFixture.restore(); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('an stamped-out fixture', function () { |
| 126 | + var attachedFixture; |
| 127 | + var element; |
| 128 | + |
| 129 | + beforeEach(function () { |
| 130 | + attachedFixture = document.getElementById('AttachedFixture'); |
| 131 | + element = attachedFixture.create(); |
| 132 | + }); |
| 133 | + |
| 134 | + afterEach(function () { |
| 135 | + attachedFixture.restore(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('detaches the fixtured DOM when it is restored', function () { |
| 139 | + var detached = false; |
| 140 | + |
| 141 | + element.onDetached = function () { |
| 142 | + detached = true; |
| 143 | + }; |
| 144 | + |
| 145 | + attachedFixture.restore(); |
| 146 | + expect(detached).to.be.eql(true); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('when create is called', function () { |
| 151 | + var el; |
| 152 | + |
| 153 | + beforeEach(function () { |
| 154 | + el = trivialFixture.create(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('clones all template fragments within itself', function () { |
| 158 | + expect(el).to.be.ok; |
| 159 | + expect(document.getElementById('Foo')).to.be.ok; |
| 160 | + }); |
| 161 | + |
| 162 | + it('detaches all fixture templates from itself', function () { |
| 163 | + expect(trivialFixture.querySelectorAll('template').length).to.be.equal(0); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('and then restore is called', function () { |
| 167 | + beforeEach(function () { |
| 168 | + trivialFixture.restore(); |
| 169 | + }); |
| 170 | + |
| 171 | + it('re-attaches all fixture templates', function () { |
| 172 | + expect(trivialFixture.querySelectorAll('template').length).to.be.equal(1); |
| 173 | + }); |
| 174 | + |
| 175 | + it('removes all cloned elements from itself', function () { |
| 176 | + expect(document.getElementById('Foo')).to.not.be.ok; |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + describe('for a dom with a single root element', function () { |
| 181 | + it('returns a reference to the root element', function () { |
| 182 | + expect(el).to.be.instanceOf(_HTMLElement); |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + describe('for a complex dom', function () { |
| 187 | + var els; |
| 188 | + |
| 189 | + beforeEach(function () { |
| 190 | + els = complexDomFixture.create(); |
| 191 | + }); |
| 192 | + |
| 193 | + it('fixtures all the dom elements in the template', function () { |
| 194 | + expect(document.getElementById('Bar')).to.be.ok; |
| 195 | + expect(document.getElementById('BarSibling')).to.be.ok; |
| 196 | + expect(document.getElementById('BarChild')).to.be.ok; |
| 197 | + }); |
| 198 | + |
| 199 | + it('returns an array of root elements', function () { |
| 200 | + expect(els).to.be.instanceOf(Array); |
| 201 | + expect(els[0]).to.be.instanceOf(_HTMLElement); |
| 202 | + expect(els[1]).to.be.instanceOf(_HTMLElement); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + describe('when there are multiple templates', function () { |
| 207 | + var groups; |
| 208 | + |
| 209 | + beforeEach(function () { |
| 210 | + groups = multiTemplateFixture.create(); |
| 211 | + }); |
| 212 | + |
| 213 | + it('fixtures elements from all of the templates', function () { |
| 214 | + expect(document.getElementById('Baz')).to.be.ok; |
| 215 | + expect(document.getElementById('Qux')).to.be.ok; |
| 216 | + }); |
| 217 | + |
| 218 | + it('returns an array with elements grouped by template', function () { |
| 219 | + expect(groups).to.be.instanceOf(Array); |
| 220 | + expect(groups[0]).to.be.instanceOf(_HTMLElement); |
| 221 | + expect(groups[1]).to.be.instanceOf(Array); |
| 222 | + expect(groups[1][0]).to.be.instanceOf(_HTMLElement); |
| 223 | + expect(groups[1][1]).to.be.instanceOf(_HTMLElement); |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + describe('when there are comments in the template', function() { |
| 228 | + var el; |
| 229 | + var els; |
| 230 | + |
| 231 | + beforeEach(function () { |
| 232 | + el = commentedSingleChildFixture.create(); |
| 233 | + els = commentedMultiChildFixture.create(); |
| 234 | + }); |
| 235 | + |
| 236 | + it('returns a single element if the template has a single child', function() { |
| 237 | + expect(el).to.be.instanceOf(_HTMLElement); |
| 238 | + }); |
| 239 | + |
| 240 | + it('returns multiple elements if the template has multiple children', function() { |
| 241 | + expect(els).to.be.instanceOf(Array); |
| 242 | + expect(els.length).to.equal(3); |
| 243 | + }); |
| 244 | + }); |
| 245 | + }); |
| 246 | + |
| 247 | + describe('when the fixture global is called', function () { |
| 248 | + var el; |
| 249 | + |
| 250 | + beforeEach(function () { |
| 251 | + el = fixture('TrivialFixture'); |
| 252 | + }); |
| 253 | + |
| 254 | + it('generates a DOM fragment from the associated fixture', function () { |
| 255 | + expect(el).to.be.equal(document.getElementById('Foo')); |
| 256 | + }); |
| 257 | + }); |
| 258 | +}); |
| 259 | + </script> |
| 260 | + |
| 261 | +</body> |
| 262 | +</html> |
0 commit comments