Skip to content

Commit

Permalink
fix(Field): resetDefault when parseValue false
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkahn committed May 24, 2019
1 parent 0acd82d commit e2c2b00
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 59 deletions.
7 changes: 5 additions & 2 deletions src/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Field {
this.fieldsMeta = {};
this.cachedBind = {};
this.instance = {};
this.initValues = options.values || {};
this.initValue = options.values || {};

this.options = Object.assign(
{
Expand Down Expand Up @@ -100,8 +100,11 @@ class Field {
defaultValue = initValue;
} else if (originalProps[defaultValueName]) {
defaultValue = originalProps[defaultValueName];
} else if (this.options.parseName) {
defaultValue = getIn(this.initValue, name);
} else {
defaultValue = getIn(this.initValues, name);
defaultValue =
(this.initValue && this.initValue[name]) || undefined;
}

Object.assign(field, {
Expand Down
154 changes: 97 additions & 57 deletions test/field/options-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import Field from '../../src/field/index';

Enzyme.configure({ adapter: new Adapter() });

/*global describe it */
/*global describe it afterEach */
describe('options', () => {
let wrapper;
afterEach(() => {
if (wrapper) {
wrapper.unmount();
wrapper = null;
}
})
it('should support autoUnmount', function(done) {
class Demo extends React.Component {
state = {
Expand Down Expand Up @@ -38,7 +45,7 @@ describe('options', () => {
);
}
}
const wrapper = mount(<Demo />);
wrapper = mount(<Demo />);
wrapper.setState({ show: false });
wrapper.update();
wrapper.find('button').simulate('click');
Expand Down Expand Up @@ -73,7 +80,7 @@ describe('options', () => {
);
}
}
const wrapper = mount(<Demo />);
wrapper = mount(<Demo />);
wrapper.setState({ show: false });
wrapper.find('button').simulate('click');

Expand All @@ -99,7 +106,6 @@ describe('options', () => {
) : null}
<button
onClick={() => {
// console.log(this.field);
assert(
this.field.getValue('input2') === 'test2'
);
Expand All @@ -111,7 +117,7 @@ describe('options', () => {
);
}
}
const wrapper = mount(<Demo />);
wrapper = mount(<Demo />);
wrapper.setState({ show: false });
wrapper.find('button').simulate('click');

Expand Down Expand Up @@ -151,67 +157,101 @@ describe('options', () => {
);
}
}
const wrapper = mount(<Demo />);
wrapper = mount(<Demo />);
wrapper.find('button').simulate('click');

done();
});

it('should support default `values` in constructor', function(done) {
class Demo extends React.Component {
constructor(props) {
super(props);
this.field = new Field(this, {
values: {
input: 'ttt',
describe('values', () => {
it('should set default field input values when given `values` in constructor', function() {
const inputValue = 'my value';
const field = new Field(this, {
values: {
input: inputValue
},
});
field.init('input');
assert.equal(field.getValue('input'), inputValue);
});

it('should set default field input values when given `values` and `parseName` = true in constructor', function() {
const inputValue = 'my value';
const field = new Field(this, {
parseName: true,
values: {
input: {
child: inputValue,
},
});
}
},
});
field.init('input.child');
assert.equal(field.getValue('input.child'), inputValue);
});

render() {
const init = this.field.init;
return (
<div>
<Input {...init('input')} />
<button
onClick={() => {
assert(this.field.getValue('input') === 'ttt');
}}
>
click
</button>
</div>
);
}
}
const wrapper = mount(<Demo />);
wrapper.find('button').simulate('click');
it('should allow access to field values before init when given `values` in constructor', function() {
const inputValue = 'my value';
const field = new Field(this, {
values: {
input: inputValue,
},
});
assert.equal(field.getValue('input'), inputValue);
});

done();
});
it('should reset `input` to undefined when given `values` in constructor and call `reset`', function() {
const fieldDefault = 'field default value';
const field = new Field(this, {
values: {
input: fieldDefault,
},
});
field.init('input');
field.reset();
assert.equal(field.getValue('input'), undefined);
});

it('should support default `values` in constructor when `parseName` = true', function() {
const inputValue = 'my value';
const field = new Field(this, {
parseName: true,
values: {
input: {
child: inputValue
}
}
it('should reset `input` to constructor `values` after calling `resetToDefault`', function() {
const fieldDefault = 'field default value';
const field = new Field(this, {
values: {
input: fieldDefault,
},
});
field.init('input');
field.resetToDefault('input');
assert.equal(field.getValue('input'), fieldDefault);
});
field.init('input.child');
assert.equal(field.getValue('input.child'), inputValue);
});

it('should support default `values` in constructor and access before init', function() {
const inputValue = 'my value';
const field = new Field(this, {
values: {
input: inputValue
}
it('should reset `input` to undefined when given `values` and `parseName` = true in constructor and call `reset`', function() {
const fieldDefault = 'field default value';
const field = new Field(this, {
parseName: true,
values: {
input: {
child: fieldDefault
},
},
});
field.init('input.child');
field.reset();
assert.equal(field.getValue('input.child'), undefined);
});

it('should reset `input` to undefined when given `values` and `parseName` = true in constructor and call `resetToDefault`', function() {
const fieldDefault = 'field default value';
const field = new Field(this, {
parseName: true,
values: {
input: {
child: fieldDefault
},
},
});
field.init('input.child');
field.resetToDefault('input.child');
assert.equal(field.getValue('input.child'), fieldDefault);
});
assert.equal(field.getValue('input'), inputValue);
});

describe('should support parseName', () => {
Expand Down Expand Up @@ -264,7 +304,7 @@ describe('options', () => {
const field = new Field(this, { autoValidate: true });
const inited = field.init('input', { rules: [{ minLength: 10 }] });

const wrapper = mount(<Input {...inited} />);
wrapper = mount(<Input {...inited} />);
wrapper.find('input').simulate('change', {
target: {
value: 'test',
Expand All @@ -279,7 +319,7 @@ describe('options', () => {
const field = new Field(this, { autoValidate: false });
const inited = field.init('input', { rules: [{ minLength: 10 }] });

const wrapper = mount(<Input {...inited} />);
wrapper = mount(<Input {...inited} />);
wrapper.find('input').simulate('change', {
target: {
value: 'test',
Expand All @@ -300,7 +340,7 @@ describe('options', () => {
rules: [{ minLength: 10 }],
});

const wrapper = mount(<Input {...inited} />);
wrapper = mount(<Input {...inited} />);
wrapper.find('input').simulate('change', {
target: {
value: 'test',
Expand Down

0 comments on commit e2c2b00

Please sign in to comment.