From 658c885c7c017a0910ecbd35eb9259cce740a4fc Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Tue, 16 Jul 2019 18:35:32 -0700 Subject: [PATCH] Add tests for connected/disconnected while disabled --- lib/legacy/class.js | 10 ++- lib/mixins/disable-upgrade-mixin.js | 16 +++-- test/unit/disable-upgrade.html | 103 ++++++++++++++++++++++++++-- 3 files changed, 116 insertions(+), 13 deletions(-) diff --git a/lib/legacy/class.js b/lib/legacy/class.js index 66288c4765..3c537c9b80 100644 --- a/lib/legacy/class.js +++ b/lib/legacy/class.js @@ -10,6 +10,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN import { LegacyElementMixin } from './legacy-element-mixin.js'; import { legacyOptimizations } from '../utils/settings.js'; +import { wrap } from '../utils/wrap.js'; const lifecycleProps = { attached: true, @@ -250,6 +251,8 @@ function GenerateClassFromInfo(info, Base, behaviors) { } // Prevent element from enabling properties when it's upgrade disabled. + // Normally overriding connectedCallback would be enough, but dom-* elements + // enable elements early. /** @override */ _enableProperties() { if (!this.__isUpgradeDisabled) { @@ -269,10 +272,10 @@ function GenerateClassFromInfo(info, Base, behaviors) { if (name == DISABLED_ATTR) { // When disable-upgrade is removed, intialize properties and // provoke connectedCallback if the element is already connected. - if (!this.__dataEnabled && value == null) { + if (this.__isUpgradeDisabled && value == null) { super._initializeProperties(); this.__isUpgradeDisabled = false; - if (this.isConnected) { + if (wrap(this).isConnected) { super.connectedCallback(); } } @@ -283,6 +286,7 @@ function GenerateClassFromInfo(info, Base, behaviors) { } // Prevent element from connecting when it's upgrade disabled. + // This prevents user code in `attached` from being called. /** @override */ connectedCallback() { if (!this.__isUpgradeDisabled) { @@ -291,6 +295,8 @@ function GenerateClassFromInfo(info, Base, behaviors) { } // Prevent element from disconnecting when it's upgrade disabled. + // This avoids allowing user code `detached` from being called without a + // paired call to `attached`. /** @override */ disconnectedCallback() { if (!this.__isUpgradeDisabled) { diff --git a/lib/mixins/disable-upgrade-mixin.js b/lib/mixins/disable-upgrade-mixin.js index d77d10507b..8f5fcdfc3c 100644 --- a/lib/mixins/disable-upgrade-mixin.js +++ b/lib/mixins/disable-upgrade-mixin.js @@ -10,8 +10,8 @@ * rights grant found at http://polymer.github.io/PATENTS.txt */ import { ElementMixin } from './element-mixin.js'; - import { dedupingMixin } from '../utils/mixin.js'; +import { wrap } from '../utils/wrap.js'; const DISABLED_ATTR = 'disable-upgrade'; @@ -83,14 +83,15 @@ export const DisableUpgradeMixin = dedupingMixin((base) => { // Prevent element from initializing properties when it's upgrade disabled. /** @override */ _initializeProperties() { - if (!this.hasAttribute(DISABLED_ATTR)) { - super._initializeProperties(); - } else { + if (this.hasAttribute(DISABLED_ATTR)) { this.__isUpgradeDisabled = true; + } else { + super._initializeProperties(); } } // Prevent element from enabling properties when it's upgrade disabled. + // Normally overriding connectedCallback would be enough, but dom-* elements /** @override */ _enableProperties() { if (!this.__isUpgradeDisabled) { @@ -110,10 +111,10 @@ export const DisableUpgradeMixin = dedupingMixin((base) => { if (name == DISABLED_ATTR) { // When disable-upgrade is removed, intialize properties and // provoke connectedCallback if the element is already connected. - if (!this.__dataEnabled && value == null) { + if (this.__isUpgradeDisabled && value == null) { super._initializeProperties(); this.__isUpgradeDisabled = false; - if (this.isConnected) { + if (wrap(this).isConnected) { super.connectedCallback(); } } @@ -124,6 +125,7 @@ export const DisableUpgradeMixin = dedupingMixin((base) => { } // Prevent element from connecting when it's upgrade disabled. + // This prevents user code in `attached` from being called. /** @override */ connectedCallback() { if (!this.__isUpgradeDisabled) { @@ -132,6 +134,8 @@ export const DisableUpgradeMixin = dedupingMixin((base) => { } // Prevent element from disconnecting when it's upgrade disabled. + // This avoids allowing user code `detached` from being called without a + // paired call to `attached`. /** @override */ disconnectedCallback() { if (!this.__isUpgradeDisabled) { diff --git a/test/unit/disable-upgrade.html b/test/unit/disable-upgrade.html index 47bca562ad..6a941344ae 100644 --- a/test/unit/disable-upgrade.html +++ b/test/unit/disable-upgrade.html @@ -53,6 +53,16 @@

[[prop]]

super.ready(); this.enabled = true; } + + connectedCallback() { + this.wasConnected = true; + super.connectedCallback(); + } + + disconnectedCallback() { + this.wasDisconnected = true; + super.disconnectedCallback(); + } }); customElements.define(Disabled.is, Disabled); @@ -74,8 +84,6 @@

[[prop]]