Commit 8391ed4f by Michael Roytman

fix(asinput): Override state value when props value changes

Add a componentWillReceiveProps function to handle case where parent component wants to set the value of asInput's value state via props.
parent 4f253ff7
...@@ -41,7 +41,7 @@ describe('asInput()', () => { ...@@ -41,7 +41,7 @@ describe('asInput()', () => {
const wrapper = mount(<InputTestComponent {...props} />); const wrapper = mount(<InputTestComponent {...props} />);
expect(wrapper.find('label').text()).toEqual(props.label); expect(wrapper.find('label').text()).toEqual(props.label);
expect(wrapper.find('#description-asInput1').text()).toEqual(props.description); expect(wrapper.find('#description-asInput1').text()).toEqual(props.description);
expect(wrapper.state('value')).toEqual(props.value); expect(wrapper.prop('value')).toEqual(props.value);
}); });
it('creates generic prop id', () => { it('creates generic prop id', () => {
...@@ -81,6 +81,21 @@ describe('asInput()', () => { ...@@ -81,6 +81,21 @@ describe('asInput()', () => {
expect(wrapper.find('small').prop('id')).toEqual(`description-${testId}`); expect(wrapper.find('small').prop('id')).toEqual(`description-${testId}`);
}); });
it('overrides state value when props value changes', () => {
const initValue = 'foofoo';
const newValue = 'barbar';
const props = {
...baseProps,
value: initValue,
};
const wrapper = mount(<InputTestComponent {...props} />);
expect(wrapper.state('value')).toEqual(initValue);
wrapper.setProps({
value: newValue,
});
expect(wrapper.state('value')).toEqual(newValue);
});
describe('fires', () => { describe('fires', () => {
it('blur handler', () => { it('blur handler', () => {
const spy = jest.fn(); const spy = jest.fn();
...@@ -149,7 +164,7 @@ describe('asInput()', () => { ...@@ -149,7 +164,7 @@ describe('asInput()', () => {
const err = wrapper.find('.form-control-feedback'); const err = wrapper.find('.form-control-feedback');
expect(err.exists()).toEqual(true); expect(err.exists()).toEqual(true);
expect(err.text()).toEqual(validationResult.validationMessage); expect(err.text()).toEqual(validationResult.validationMessage);
// expect(err.hasClass('invalid-feedback')).toEqual(true); expect(err.hasClass('invalid-feedback')).toEqual(true);
const dangerIcon = wrapper.find('.fa-exclamation-circle'); const dangerIcon = wrapper.find('.fa-exclamation-circle');
expect(dangerIcon.exists()).toEqual(true); expect(dangerIcon.exists()).toEqual(true);
......
...@@ -46,6 +46,14 @@ const asInput = (WrappedComponent, labelFirst = true) => { ...@@ -46,6 +46,14 @@ const asInput = (WrappedComponent, labelFirst = true) => {
}; };
} }
componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.props.value) {
this.setState({
value: nextProps.value,
});
}
}
getDescriptions() { getDescriptions() {
// possible future work: multiple feedback msgs? // possible future work: multiple feedback msgs?
const errorId = `error-${this.state.id}`; const errorId = `error-${this.state.id}`;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment