@@ -476,6 +476,77 @@ describe('<Form> component', () => {
476
476
} ) ;
477
477
} ) ;
478
478
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
+
479
550
describe ( 'onSubmit() prop' , ( ) => {
480
551
const store = applyMiddleware ( thunk ) ( createStore ) ( combineReducers ( {
481
552
testForm : formReducer ( 'test' ) ,
0 commit comments