From 37b274c45ec157b2ef9e0924f644e078f1cb1be7 Mon Sep 17 00:00:00 2001 From: Robert-Frampton Date: Thu, 8 Feb 2018 14:42:39 -0800 Subject: [PATCH] Do not favor undefined over default values when updating data manager. Fixes #345 --- .../src/ComponentDataManager.js | 4 ++-- .../test/ComponentDataManager.js | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/metal-component/src/ComponentDataManager.js b/packages/metal-component/src/ComponentDataManager.js index 0bcb02b5..b5f09740 100644 --- a/packages/metal-component/src/ComponentDataManager.js +++ b/packages/metal-component/src/ComponentDataManager.js @@ -1,6 +1,6 @@ 'use strict'; -import {object} from 'metal'; +import {isDef, object} from 'metal'; import State from 'metal-state'; const BLACKLIST = { @@ -120,7 +120,7 @@ class ComponentDataManager { for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (!state.getStateKeyConfig(key).internal) { - if (data.hasOwnProperty(key)) { + if (data.hasOwnProperty(key) && isDef(data[key])) { state.set(key, data[key]); } else { state.setDefaultValue(key); diff --git a/packages/metal-component/test/ComponentDataManager.js b/packages/metal-component/test/ComponentDataManager.js index 76cd03c9..7437d294 100644 --- a/packages/metal-component/test/ComponentDataManager.js +++ b/packages/metal-component/test/ComponentDataManager.js @@ -101,6 +101,27 @@ describe('ComponentDataManager', function() { assert.strictEqual('bar', component.bar); }); + it('should not favor undefined values over default values when replacing all non internal data', function() { + component.constructor.STATE = { + bar: { + value: 'initialBar', + }, + foo: { + value: 'initialFoo', + }, + }; + + initialConfig = {}; + ComponentDataManager.setUp(component, {}); + + ComponentDataManager.replaceNonInternal(component, { + bar: undefined, + foo: 'newFoo', + }); + assert.strictEqual('newFoo', component.foo); + assert.strictEqual('initialBar', component.bar); + }); + it('should return state instance', function() { ComponentDataManager.setUp(component, {}); assert.ok(