Skip to content

Commit

Permalink
LegacyElement
Browse files Browse the repository at this point in the history
* fix issue where registered was called on element instance instead of prototype
* fix issue where applyListeners was called in constructor rather than initializeProperties; this made `disable-upgrade` mixin not properly work with litsners.
  • Loading branch information
Steven Orvell committed Oct 31, 2018
1 parent 4f22303 commit 20c8bf1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
20 changes: 10 additions & 10 deletions lib/legacy/legacy-element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@
this.__boundListeners;
/** @type {Object<string, Function>} */
this._debouncers;
// Ensure listeners are applied immediately so that they are
// added before declarative event listeners. This allows an element to
// decorate itself via an event prior to any declarative listeners
// seeing the event. Note, this ensures compatibility with 1.x ordering.
this._applyListeners();
}

/**
Expand All @@ -99,6 +94,11 @@
return this.prototype.importMeta;
}

static _finalizeClass() {
this.prototype._registered();
super._finalizeClass();
}

/**
* Legacy callback called during the `constructor`, for overriding
* by the user.
Expand Down Expand Up @@ -181,14 +181,14 @@
* @suppress {invalidCasts}
*/
_initializeProperties() {
let proto = Object.getPrototypeOf(this);
if (!proto.hasOwnProperty('__hasRegisterFinished')) {
proto.__hasRegisterFinished = true;
this._registered();
}
super._initializeProperties();
this.root = /** @type {HTMLElement} */(this);
this.created();
// Ensure listeners are applied immediately so that they are
// added before declarative event listeners. This allows an element to
// decorate itself via an event prior to any declarative listeners
// seeing the event. Note, this ensures compatibility with 1.x ordering.
this._applyListeners();
}

/**
Expand Down
27 changes: 20 additions & 7 deletions test/unit/disable-upgrade.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ <h2 id="element">[[prop]]</h2>
type: String
}
},
listeners: {
foo: 'fooHandler'
},
created() {
this.hasCreated = true;
this.prop = 'enabled!';
},
ready() {
this.enabled = true;
}
},
fooHandler: function() {}
});

Polymer({
Expand Down Expand Up @@ -238,30 +242,39 @@ <h2 id="element">[[prop]]</h2>
assert.ok(el.$.enabledEl.enabled);
assert.ok(el.$.enabledEl.$.element);
assert.equal(el.$.enabledEl.$.element.textContent, 'enabled!');
el.$.enabledEl.fooHandler = sinon.spy();
el.$.enabledEl.fire('foo');
assert.equal(el.$.enabledEl.fooHandler.callCount, 1);
assert.notOk(el.$.disabledEl.hasCreated);
assert.notOk(el.$.disabledEl.enabled);
assert.notOk(el.$.disabledEl.$);
el.$.disabledEl.fooHandler = sinon.spy();
el.$.disabledEl.fire('foo');
assert.equal(el.$.disabledEl.fooHandler.callCount, 0);
assert.notOk(el.$.disabledBoundEl.hasCreated);
assert.notOk(el.$.disabledBoundEl.enabled);
assert.notOk(el.$.disabledBoundEl.$);
el.$.disabledBoundEl.fooHandler = sinon.spy();
el.$.disabledBoundEl.fire('foo');
assert.equal(el.$.disabledBoundEl.fooHandler.callCount, 0);
});

test('elements upgrade when `disable-upgrade` removed', function() {
assert.notOk(el.$.disabledEl.hasCreated);
assert.notOk(el.$.disabledEl.enabled);
assert.notOk(el.$.disabledEl.$);
assert.notOk(el.$.disabledBoundEl.hasCreated);
assert.notOk(el.$.disabledBoundEl.enabled);
assert.notOk(el.$.disabledBoundEl.$);
el.enable();
assert.ok(el.$.disabledEl.hasCreated);
assert.ok(el.$.disabledEl.enabled);
assert.ok(el.$.disabledEl.$.element);
assert.equal(el.$.disabledEl.$.element.textContent, 'enabled!');
el.$.disabledEl.fooHandler = sinon.spy();
el.$.disabledEl.fire('foo');
assert.equal(el.$.disabledEl.fooHandler.callCount, 1);
assert.ok(el.$.disabledBoundEl.hasCreated);
assert.ok(el.$.disabledBoundEl.enabled);
assert.ok(el.$.disabledBoundEl.$.element);
assert.equal(el.$.disabledBoundEl.$.element.textContent, 'enabled!');
el.$.disabledBoundEl.fooHandler = sinon.spy();
el.$.disabledBoundEl.fire('foo');
assert.equal(el.$.disabledBoundEl.fooHandler.callCount, 1);
});


Expand Down

0 comments on commit 20c8bf1

Please sign in to comment.