Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
fix(*): use value as default if present
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSharpieOne committed Oct 3, 2016
1 parent 5d2bf42 commit e45ef08
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
14 changes: 14 additions & 0 deletions __test__/AvBaseInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ describe('BaseInput', function() {
expect(this.setStateSpy).to.have.been.calledWithMatch({value: defaultValue});
});

it('should set the value to the value prop if provided', () => {
const defaultValue = 'some value';
this.component.props.value = defaultValue;
this.component.componentWillMount();
expect(this.component.value).to.equal(defaultValue);
});

it('should set the state value to the value prop if provided', () => {
const defaultValue = 'some value';
this.component.props.value = defaultValue;
this.component.componentWillMount();
expect(this.setStateSpy).to.have.been.calledWithMatch({value: defaultValue});
});

it('should trigger validation', () => {
const spy = sinon.spy(this.component, 'validate');
this.component.componentWillMount();
Expand Down
26 changes: 24 additions & 2 deletions __test__/AvRadioGroup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,16 @@ describe('AvRadioGroup', () => {
expect(options.context.FormCtrl.unregister).to.have.been.called;
});

it('should give default value from props', () => {
it('should give default value from value prop', () => {
const wrapper = shallow(<AvRadioGroup name="yo" value="momo" />, options);
const component = wrapper.instance();
expect(component.value).to.eql('momo');
});

it('should give default value from defaultValue prop when there is no value prop', () => {
const wrapper = shallow(<AvRadioGroup name="yo" defaultValue="momo" />, options);
const component = wrapper.instance();
expect(component.getDefaultValue()).to.eql({key: 'defaultValue', value: 'momo'});
expect(component.value).to.eql('momo');
});

it('should update the value when the value prop changes', () => {
Expand All @@ -77,6 +83,22 @@ describe('AvRadioGroup', () => {
expect(component.getValue()).to.equal('yoyo');
});

it('should update the validations when the props change', () => {
const wrapper = shallow(<AvRadioGroup name="yo" defaultValue="momo" />, options);
const component = wrapper.instance();
const spy = sinon.spy(component, 'updateValidations');
wrapper.setProps({required: true});
expect(spy).to.have.been.called;
});

it('should not update the validations when the props did not change', () => {
const wrapper = shallow(<AvRadioGroup name="yo" defaultValue="momo" />, options);
const component = wrapper.instance();
const spy = sinon.spy(component, 'updateValidations');
wrapper.setProps({defaultValue: 'momo'});
expect(spy).to.not.have.been.called;
});

it('should give default value from context', () => {
const wrapper = shallow(<AvRadioGroup name="yo" />, options);
const component = wrapper.instance();
Expand Down
2 changes: 1 addition & 1 deletion src/AvBaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class AvBaseInput extends Component {
}

componentWillMount() {
this.value = this.getDefaultValue().value;
this.value = this.props.value || this.getDefaultValue().value;
this.setState({value: this.value});
this.updateValidations();
}
Expand Down
2 changes: 1 addition & 1 deletion src/AvRadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class AvRadioGroup extends InputContainer {

componentWillMount() {
super.componentWillMount();
this.value = this.getDefaultValue().value;
this.value = this.props.value || this.getDefaultValue().value;
this.setState({value: this.value});
this.updateValidations();
}
Expand Down

0 comments on commit e45ef08

Please sign in to comment.