|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * check that document.registerElement(name, { prototype: proto }); |
| 11 | + * is properly patched |
| 12 | + */ |
| 13 | + |
| 14 | +function customElementsSupport() { |
| 15 | + return 'registerElement' in document; |
| 16 | +} |
| 17 | +customElementsSupport.message = 'window.customElements'; |
| 18 | + |
| 19 | +describe('customElements', function() { |
| 20 | + const testZone = Zone.current.fork({ name: 'test' }); |
| 21 | + const bridge = { |
| 22 | + connectedCallback: () => {}, |
| 23 | + disconnectedCallback: () => {}, |
| 24 | + adoptedCallback: () => {}, |
| 25 | + attributeChangedCallback: () => {} |
| 26 | + }; |
| 27 | + |
| 28 | + class TestCustomElement extends HTMLElement { |
| 29 | + constructor() { |
| 30 | + super(); |
| 31 | + } |
| 32 | + |
| 33 | + static get observedAttributes() { |
| 34 | + return ['attr1', 'attr2']; |
| 35 | + } |
| 36 | + |
| 37 | + connectedCallback() { |
| 38 | + return bridge.connectedCallback(); |
| 39 | + } |
| 40 | + |
| 41 | + disconnectedCallback() { |
| 42 | + return bridge.disconnectedCallback(); |
| 43 | + } |
| 44 | + |
| 45 | + attributeChangedCallback(attrName, oldVal, newVal) { |
| 46 | + return bridge.attributeChangedCallback(attrName, oldVal, newVal); |
| 47 | + } |
| 48 | + |
| 49 | + adoptedCallback() { |
| 50 | + return bridge.adoptedCallback(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + testZone.run(() => { |
| 55 | + customElements.define('x-test', TestCustomElement); |
| 56 | + }); |
| 57 | + |
| 58 | + let elt; |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + bridge.connectedCallback = () => {}; |
| 62 | + bridge.disconnectedCallback = () => {}; |
| 63 | + bridge.attributeChangedCallback = () => {}; |
| 64 | + bridge.adoptedCallback = () => {}; |
| 65 | + }); |
| 66 | + |
| 67 | + afterEach(() => { |
| 68 | + if (elt) { |
| 69 | + document.body.removeChild(elt); |
| 70 | + elt = null; |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + it('should work with connectedCallback', function(done) { |
| 75 | + bridge.connectedCallback = function() { |
| 76 | + expect(Zone.current.name).toBe(testZone.name); |
| 77 | + done(); |
| 78 | + }; |
| 79 | + |
| 80 | + elt = document.createElement('x-test'); |
| 81 | + document.body.appendChild(elt); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should work with disconnectedCallback', function(done) { |
| 85 | + bridge.disconnectedCallback = function() { |
| 86 | + expect(Zone.current.name).toBe(testZone.name); |
| 87 | + done(); |
| 88 | + }; |
| 89 | + |
| 90 | + elt = document.createElement('x-test'); |
| 91 | + document.body.appendChild(elt); |
| 92 | + document.body.removeChild(elt); |
| 93 | + elt = null; |
| 94 | + }); |
| 95 | + |
| 96 | + it('should work with attributeChanged', function(done) { |
| 97 | + bridge.attributeChangedCallback = function(attrName, oldVal, newVal) { |
| 98 | + expect(Zone.current.name).toBe(testZone.name); |
| 99 | + expect(attrName).toEqual('attr1'); |
| 100 | + expect(newVal).toEqual('value1'); |
| 101 | + done(); |
| 102 | + }; |
| 103 | + |
| 104 | + elt = document.createElement('x-test'); |
| 105 | + document.body.appendChild(elt); |
| 106 | + elt.setAttribute('attr1', 'value1'); |
| 107 | + }); |
| 108 | +}); |
0 commit comments