|
| 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 | +import {ifEnvSupports} from '../test-util'; |
| 15 | + |
| 16 | +function customElementsSupport() { |
| 17 | + return ('registerElement' in document); |
| 18 | +} |
| 19 | +customElementsSupport.message = 'window.customElements'; |
| 20 | + |
| 21 | +describe('customElements', ifEnvSupports(customElementsSupport, function() { |
| 22 | + const testZone = Zone.current.fork({name: 'test'}); |
| 23 | + const callbacks = [ |
| 24 | + 'connectedCallback', 'disconnectedCallback', 'adoptedCallback', |
| 25 | + 'attributeChangedCallback' |
| 26 | + ]; |
| 27 | + const bridge = { |
| 28 | + connectedCallback: () => {}, |
| 29 | + disconnectedCallback: () => {}, |
| 30 | + adoptedCallback: () => {}, |
| 31 | + attributeChangedCallback: () => {} |
| 32 | + }; |
| 33 | + |
| 34 | + class TestCustomElement extends HTMLElement { |
| 35 | + constructor() { |
| 36 | + super(); |
| 37 | + } |
| 38 | + |
| 39 | + static get observedAttributes() { |
| 40 | + return ['attr1', 'attr2']; |
| 41 | + } |
| 42 | + |
| 43 | + connectedCallback() { |
| 44 | + return bridge.connectedCallback(); |
| 45 | + } |
| 46 | + |
| 47 | + disconnectedCallback() { |
| 48 | + return bridge.disconnectedCallback(); |
| 49 | + } |
| 50 | + |
| 51 | + attributeChangedCallback(attrName, oldVal, newVal) { |
| 52 | + return bridge.attributeChangedCallback(attrName, oldVal, newVal); |
| 53 | + } |
| 54 | + |
| 55 | + adoptedCallback() { |
| 56 | + return bridge.adoptedCallback(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + testZone.run(() => { |
| 61 | + customElements.define('x-test', TestCustomElement); |
| 62 | + }); |
| 63 | + |
| 64 | + let elt; |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + bridge.connectedCallback = () => {}; |
| 68 | + bridge.disconnectedCallback = () => {}; |
| 69 | + bridge.attributeChangedCallback = () => {}; |
| 70 | + bridge.adoptedCallback = () => {}; |
| 71 | + }); |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + if (elt) { |
| 75 | + document.body.removeChild(elt); |
| 76 | + elt = null; |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + it('should work with connectedCallback', function(done) { |
| 81 | + bridge.connectedCallback = function() { |
| 82 | + expect(Zone.current.name).toBe(testZone.name); |
| 83 | + done(); |
| 84 | + }; |
| 85 | + |
| 86 | + elt = document.createElement('x-test'); |
| 87 | + document.body.appendChild(elt); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should work with disconnectedCallback', function(done) { |
| 91 | + bridge.disconnectedCallback = function() { |
| 92 | + expect(Zone.current.name).toBe(testZone.name); |
| 93 | + done(); |
| 94 | + }; |
| 95 | + |
| 96 | + elt = document.createElement('x-test'); |
| 97 | + document.body.appendChild(elt); |
| 98 | + document.body.removeChild(elt); |
| 99 | + elt = null; |
| 100 | + }); |
| 101 | + |
| 102 | + it('should work with attributeChanged', function(done) { |
| 103 | + bridge.attributeChangedCallback = function( |
| 104 | + attrName, oldVal, newVal) { |
| 105 | + expect(Zone.current.name).toBe(testZone.name); |
| 106 | + expect(attrName).toEqual('attr1'); |
| 107 | + expect(newVal).toEqual('value1'); |
| 108 | + done(); |
| 109 | + }; |
| 110 | + |
| 111 | + elt = document.createElement('x-test'); |
| 112 | + document.body.appendChild(elt); |
| 113 | + elt.setAttribute('attr1', 'value1'); |
| 114 | + }); |
| 115 | + })); |
0 commit comments