Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not favor undefined over default values when updating data manager #357

Merged
merged 2 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/metal-component/src/ComponentDataManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {object} from 'metal';
import {isDef, object} from 'metal';
import State from 'metal-state';

const BLACKLIST = {
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions packages/metal-component/test/ComponentDataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
49 changes: 49 additions & 0 deletions packages/metal-jsx/test/JSXComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,55 @@ describe('JSXComponent', function() {
assert.ok(dom.hasClass(component.element, 'parent'));
assert.ok(dom.hasClass(component.element, 'grandchild'));
});

it('should use default prop value when "undefined" is passed as a value on update', function(
done
) {
class ChildComponent extends JSXComponent {
render() {
return <div />;
}
}
ChildComponent.PROPS = {
childFoo: {
value: 'foo',
},
childBar: {
value: 'bar',
},
};

class ParentComponent extends JSXComponent {
render() {
return (
<ChildComponent
childBar={this.state.bar}
childFoo={this.state.foo}
ref="childComponent"
/>
);
}
}
ParentComponent.STATE = {
bar: {
value: 'bar',
},
foo: {},
};

component = new ParentComponent();

component.state.bar = 'newBar';

component.once('stateChanged', function() {
assert.strictEqual(
component.refs.childComponent.props.childBar,
'newBar'
);
assert.strictEqual(component.refs.childComponent.props.childFoo, 'foo');
done();
});
});
});

describe('shouldUpdate', function() {
Expand Down