From dcc640b91e4b609547cded1ab463b04ad1cdab63 Mon Sep 17 00:00:00 2001 From: Robert-Frampton Date: Tue, 21 Nov 2017 14:59:47 -0800 Subject: [PATCH 1/3] Default STATE values should be preserved when rendering web components. Fixes #305 --- packages/metal-web-component/src/define_web_component.js | 6 +++++- .../metal-web-component/test/define_web_component.js | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/metal-web-component/src/define_web_component.js b/packages/metal-web-component/src/define_web_component.js index 93bf10c3..854d0a68 100644 --- a/packages/metal-web-component/src/define_web_component.js +++ b/packages/metal-web-component/src/define_web_component.js @@ -80,9 +80,13 @@ export function defineWebComponent(tagName, Ctor) { let opts = {}; for (let i = 0, l = observedAttributes.length; i < l; i++) { - opts[observedAttributes[i]] = this.deserializeValue_( + let deserializedValue = this.deserializeValue_( this.getAttribute(observedAttributes[i]) ); + + if (deserializedValue) { + opts[observedAttributes[i]] = deserializedValue; + } } this.component = new Ctor(opts, element); this.componentHasProps = hasProps; diff --git a/packages/metal-web-component/test/define_web_component.js b/packages/metal-web-component/test/define_web_component.js index b1f9dce8..8ba8783d 100644 --- a/packages/metal-web-component/test/define_web_component.js +++ b/packages/metal-web-component/test/define_web_component.js @@ -137,6 +137,15 @@ describe('Web components', function() { assert.isUndefined(title.key1); assert.equal(title.key3, 'value3'); }); + + it('should have the default state value after rendering', function() { + const tagName = createWebComponent('custom-test-element-09'); + el = document.createElement(tagName); + + document.body.appendChild(el); + + assert.equal(el.component.title, 'default title'); + }); }); describe('Define JSX component', function() { From 05447bc57420cc53badd7bef33fec3ced18e891b Mon Sep 17 00:00:00 2001 From: Robert-Frampton Date: Wed, 22 Nov 2017 08:15:01 -0800 Subject: [PATCH 2/3] Using const instead of let --- packages/metal-web-component/src/define_web_component.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/metal-web-component/src/define_web_component.js b/packages/metal-web-component/src/define_web_component.js index 854d0a68..e0989ae5 100644 --- a/packages/metal-web-component/src/define_web_component.js +++ b/packages/metal-web-component/src/define_web_component.js @@ -78,9 +78,9 @@ export function defineWebComponent(tagName, Ctor) { }); } - let opts = {}; + const opts = {}; for (let i = 0, l = observedAttributes.length; i < l; i++) { - let deserializedValue = this.deserializeValue_( + const deserializedValue = this.deserializeValue_( this.getAttribute(observedAttributes[i]) ); From df918b2d36c4e548b010a3abac06b444fc7713a0 Mon Sep 17 00:00:00 2001 From: Robert-Frampton Date: Wed, 22 Nov 2017 08:20:13 -0800 Subject: [PATCH 3/3] Run format --- packages/metal-component/test/Component.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/metal-component/test/Component.js b/packages/metal-component/test/Component.js index 17c2caf5..7e751b2c 100644 --- a/packages/metal-component/test/Component.js +++ b/packages/metal-component/test/Component.js @@ -213,13 +213,13 @@ describe('Component', function() { assert.strictEqual(1, comp.disposed.callCount); }); - it('should emit "disposed" event when component is disposed', function () { - var listener = sinon.stub(); + it('should emit "disposed" event when component is disposed', function() { + let listener = sinon.stub(); comp = new Component({ events: { - disposed: listener - } + disposed: listener, + }, }); assert.strictEqual(0, listener.callCount);