Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 5e15cee

Browse files
committed
Fixing Form change validation issue + unit tests
1 parent 12f2a36 commit 5e15cee

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/components/form-component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Form extends Component {
5858
const value = _get(nextProps, fieldModel);
5959

6060
if (!initial && (value === _get(this.props, fieldModel))) {
61-
return getField(formValue, fieldModel).validity;
61+
return getField(formValue, field).validity;
6262
}
6363

6464
validityChanged = true;
@@ -73,7 +73,7 @@ class Form extends Component {
7373
const value = _get(nextProps, fieldModel);
7474

7575
if (!initial && (value === _get(this.props, fieldModel))) {
76-
return getField(formValue, fieldModel).errors;
76+
return getField(formValue, field).errors;
7777
}
7878

7979
validityChanged = true;

test/form-component-spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,77 @@ describe('<Form> component', () => {
476476
});
477477
});
478478

479+
describe('maintaining field validation state', () => {
480+
const initialState = { foo: '', bar: '' };
481+
482+
const required = (val) => val && val.length;
483+
484+
const store = applyMiddleware(thunk)(createStore)(combineReducers({
485+
testForm: formReducer('test', initialState),
486+
test: modelReducer('test', initialState),
487+
}));
488+
489+
const form = TestUtils.renderIntoDocument(
490+
<Provider store={store}>
491+
<Form model="test"
492+
validators={{
493+
foo: required,
494+
bar: required,
495+
}}
496+
validateOn="change"
497+
>
498+
<Field model="test.foo">
499+
<input type="text" />
500+
</Field>
501+
502+
<Field model="test.bar">
503+
<input type="text" />
504+
</Field>
505+
</Form>
506+
</Provider>
507+
);
508+
509+
const [fooControl, barControl] = TestUtils.scryRenderedDOMComponentsWithTag(form, 'input');
510+
511+
it('should initially be invalid', () => {
512+
assert.isFalse(store.getState().testForm.valid);
513+
});
514+
515+
it('should still be invalid if fields are still invalid', () => {
516+
fooControl.value = 'valid';
517+
TestUtils.Simulate.change(fooControl);
518+
519+
assert.isTrue(
520+
store.getState().testForm.fields.foo.valid,
521+
'foo should be valid');
522+
assert.isFalse(
523+
store.getState().testForm.fields.bar.valid,
524+
'bar should be invalid');
525+
526+
assert.isFalse(
527+
store.getState().testForm.valid,
528+
'form should be invalid');
529+
});
530+
531+
it('should be valid once all fields are valid', () => {
532+
fooControl.value = 'valid';
533+
TestUtils.Simulate.change(fooControl);
534+
barControl.value = 'valid';
535+
TestUtils.Simulate.change(barControl);
536+
537+
assert.isTrue(
538+
store.getState().testForm.fields.foo.valid,
539+
'foo should be valid');
540+
assert.isTrue(
541+
store.getState().testForm.fields.bar.valid,
542+
'bar should be valid');
543+
544+
assert.isTrue(
545+
store.getState().testForm.valid,
546+
'form should be valid');
547+
});
548+
});
549+
479550
describe('onSubmit() prop', () => {
480551
const store = applyMiddleware(thunk)(createStore)(combineReducers({
481552
testForm: formReducer('test'),

0 commit comments

Comments
 (0)