|
| 1 | +/* |
| 2 | + * check that document.registerElement(name, { prototype: proto }); |
| 3 | + * is properly patched |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +describe('document.registerElement', function () { |
| 9 | + if (!'registerElement' in document) { |
| 10 | + console.log('WARNING: skipping document.registerElement test (missing this API)'); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + var flag, hasParent; |
| 15 | + |
| 16 | + // register a custom element for each callback |
| 17 | + var callbacks = [ |
| 18 | + 'created', |
| 19 | + 'attached', |
| 20 | + 'detached', |
| 21 | + 'attributeChanged' |
| 22 | + ]; |
| 23 | + |
| 24 | + function flagAndCheckZone() { |
| 25 | + flag = true; |
| 26 | + hasParent = !!window.zone.parent; |
| 27 | + } |
| 28 | + |
| 29 | + var customElements = callbacks.map(function (callbackName) { |
| 30 | + var fullCallbackName = callbackName + 'Callback'; |
| 31 | + var proto = Object.create(HTMLElement.prototype); |
| 32 | + proto[fullCallbackName] = flagAndCheckZone; |
| 33 | + return document.registerElement('x-' + callbackName.toLowerCase(), { |
| 34 | + prototype: proto |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + |
| 39 | + beforeEach(function () { |
| 40 | + flag = false; |
| 41 | + hasParent = false; |
| 42 | + }); |
| 43 | + |
| 44 | + |
| 45 | + it('should work with createdCallback', function () { |
| 46 | + runs(function () { |
| 47 | + var elt = document.createElement('x-created'); |
| 48 | + }); |
| 49 | + |
| 50 | + waitsFor(function() { |
| 51 | + return flag; |
| 52 | + }, 'createdCallback to fire', 100); |
| 53 | + |
| 54 | + runs(function() { |
| 55 | + expect(hasParent).toBe(true); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + |
| 60 | + it('should work with attachedCallback', function () { |
| 61 | + runs(function () { |
| 62 | + var elt = document.createElement('x-attached'); |
| 63 | + document.body.appendChild(elt); |
| 64 | + document.body.removeChild(elt); |
| 65 | + }); |
| 66 | + |
| 67 | + waitsFor(function() { |
| 68 | + return flag; |
| 69 | + }, 'attachedCallback to fire', 100); |
| 70 | + |
| 71 | + runs(function() { |
| 72 | + expect(hasParent).toBe(true); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + |
| 77 | + it('should work with detachedCallback', function () { |
| 78 | + runs(function () { |
| 79 | + var elt = document.createElement('x-detached'); |
| 80 | + document.body.appendChild(elt); |
| 81 | + document.body.removeChild(elt); |
| 82 | + }); |
| 83 | + |
| 84 | + waitsFor(function() { |
| 85 | + return flag; |
| 86 | + }, 'detachedCallback to fire', 100); |
| 87 | + |
| 88 | + runs(function() { |
| 89 | + expect(hasParent).toBe(true); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + |
| 94 | + it('should work with attributeChanged', function () { |
| 95 | + runs(function () { |
| 96 | + var elt = document.createElement('x-attributechanged'); |
| 97 | + elt.id = 'bar'; |
| 98 | + }); |
| 99 | + |
| 100 | + waitsFor(function() { |
| 101 | + return flag; |
| 102 | + }, 'attributeChanged to fire', 100); |
| 103 | + |
| 104 | + runs(function() { |
| 105 | + expect(hasParent).toBe(true); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | +}); |
0 commit comments