Skip to content

Commit

Permalink
fix(asinput): Remove control of state.value from asInput
Browse files Browse the repository at this point in the history
fix(asinput): Remove control of state.value from asInput
  • Loading branch information
MichaelRoytman authored Dec 8, 2017
2 parents a18fd5d + 8391ed4 commit 213b4fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/asInput/asInput.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('asInput()', () => {
const wrapper = mount(<InputTestComponent {...props} />);
expect(wrapper.find('label').text()).toEqual(props.label);
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', () => {
Expand Down Expand Up @@ -81,6 +81,21 @@ describe('asInput()', () => {
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', () => {
it('blur handler', () => {
const spy = jest.fn();
Expand Down Expand Up @@ -149,7 +164,7 @@ describe('asInput()', () => {
const err = wrapper.find('.form-control-feedback');
expect(err.exists()).toEqual(true);
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');
expect(dangerIcon.exists()).toEqual(true);
Expand Down
8 changes: 8 additions & 0 deletions src/asInput/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ const asInput = (WrappedComponent, labelFirst = true) => {
};
}

componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.props.value) {
this.setState({
value: nextProps.value,
});
}
}

getDescriptions() {
// possible future work: multiple feedback msgs?
const errorId = `error-${this.state.id}`;
Expand Down

0 comments on commit 213b4fa

Please sign in to comment.