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

hasInitialValue_ should return false if value is undefined, but validators should run regardless. Fixes #259 #276

Merged
merged 2 commits into from
Oct 19, 2017
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
13 changes: 13 additions & 0 deletions packages/metal-component/test/ComponentDataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ describe('ComponentDataManager', function() {
assert.strictEqual('initialFoo', component.foo);
});

it('should use default state value when "undefined" is passed as initial value', function() {
initialConfig = {
foo: undefined
};
ComponentDataManager.setUp(component, {
foo: {
value: 'defaultFoo'
}
});

assert.strictEqual(component.foo, 'defaultFoo');
});

it('should throw error if attempting to add state property named "element"', function() {
assert.throws(() => {
ComponentDataManager.setUp(component, {
Expand Down
16 changes: 16 additions & 0 deletions packages/metal-jsx/test/JSXDataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ describe('JSXDataManager', function() {
assert.strictEqual('foo', component.props.foo);
});

it('should use default prop value when "undefined" is passed as initial value', function() {
class TestComponent extends Component {
}
TestComponent.DATA_MANAGER = JSXDataManager;
TestComponent.PROPS = {
foo: {
value: 'defaultFoo'
}
};

component = new TestComponent({
foo: undefined
});
assert.strictEqual('defaultFoo', component.props.foo);
});

it('should include default component data in "props"', function() {
class TestComponent extends Component {
}
Expand Down
9 changes: 6 additions & 3 deletions packages/metal-state/src/State.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { async, getStaticProperty, isDefAndNotNull, isFunction, isObject, isString, object } from 'metal';
import { async, getStaticProperty, isDef, isDefAndNotNull, isFunction, isObject, isString, object } from 'metal';
import { EventEmitter } from 'metal-events';

/**
Expand Down Expand Up @@ -412,7 +412,8 @@ class State extends EventEmitter {
* @protected
*/
hasInitialValue_(name) {
return this.initialValues_.hasOwnProperty(name);
return this.initialValues_.hasOwnProperty(name) &&
isDef(this.initialValues_[name]);
}

/**
Expand Down Expand Up @@ -637,7 +638,9 @@ class State extends EventEmitter {
* @protected
*/
validateInitialValue_(name) {
if (this.hasInitialValue_(name) && !this.callValidator_(name, this.initialValues_[name])) {
if (this.initialValues_.hasOwnProperty(name) &&
!this.callValidator_(name, this.initialValues_[name])) {

delete this.initialValues_[name];
}
}
Expand Down
13 changes: 13 additions & 0 deletions packages/metal-state/test/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,19 @@ describe('State', function() {
assert.strictEqual(1, state.key1);
});

it('should not overwrite default value with "undefined" initial value', function() {
var state = new State({
key1: undefined
});
state.configState({
key1: {
value: 'value1'
}
});

assert.equal(state.key1, 'value1');
});

it('should allow accessing other state properties in validator', function() {
var state = new State({
key1: 1
Expand Down