@@ -5,8 +5,9 @@ import TestUtils from 'react-addons-test-utils';
5
5
import { applyMiddleware , combineReducers , createStore } from 'redux' ;
6
6
import { Provider } from 'react-redux' ;
7
7
import thunk from 'redux-thunk' ;
8
+ import createTestStore from 'redux-test-store' ;
8
9
9
- import { Form , modelReducer , formReducer , Field , actions } from '../src' ;
10
+ import { Form , modelReducer , formReducer , Field , actions , actionTypes } from '../src' ;
10
11
11
12
describe ( '<Form> component' , ( ) => {
12
13
describe ( 'wraps component if specified' , ( ) => {
@@ -930,15 +931,15 @@ describe('<Form> component', () => {
930
931
} ) ;
931
932
932
933
describe ( 'deep state path' , ( ) => {
933
- const fromsReducer = combineReducers ( {
934
+ const formsReducer = combineReducers ( {
934
935
testForm : formReducer ( 'forms.test' ) ,
935
936
test : modelReducer ( 'forms.test' , {
936
937
foo : '' ,
937
938
bar : '' ,
938
939
} ) ,
939
940
} ) ;
940
941
const store = applyMiddleware ( thunk ) ( createStore ) ( combineReducers ( {
941
- forms : fromsReducer ,
942
+ forms : formsReducer ,
942
943
} ) ) ;
943
944
944
945
const form = TestUtils . renderIntoDocument (
@@ -958,4 +959,49 @@ describe('<Form> component', () => {
958
959
assert . containSubset ( props . formValue , { valid : true , model : 'forms.test' } ) ;
959
960
} ) ;
960
961
} ) ;
962
+
963
+ describe ( 'invalidating async validity on form change' , ( ) => {
964
+ const store = createTestStore ( applyMiddleware ( thunk ) ( createStore ) ( combineReducers ( {
965
+ test : modelReducer ( 'test' , { val : 'invalid' } ) ,
966
+ testForm : formReducer ( 'test' , { val : 'invalid' } ) ,
967
+ } ) ) ) ;
968
+
969
+ function handleSubmit ( ) {
970
+ const promise = new Promise ( ( resolve , reject ) => reject ( 'Form is invalid' ) ) ;
971
+
972
+ store . dispatch ( actions . submit ( 'test' , promise ) ) ;
973
+ }
974
+
975
+ const form = TestUtils . renderIntoDocument (
976
+ < Provider store = { store } >
977
+ < Form model = "test"
978
+ onSubmit = { handleSubmit }
979
+ >
980
+ < Field model = "test.foo" >
981
+ < input type = "text" />
982
+ </ Field >
983
+ </ Form >
984
+ </ Provider >
985
+ ) ;
986
+
987
+ const formElement = TestUtils . findRenderedDOMComponentWithTag ( form , 'form' ) ;
988
+ const inputElement = TestUtils . findRenderedDOMComponentWithTag ( form , 'input' ) ;
989
+
990
+ it ( 'should set errors from rejected submit handler on valid submit' , ( done ) => {
991
+ store . when ( actionTypes . SET_ERRORS , ( state ) => {
992
+ assert . isFalse ( state . testForm . valid ) ;
993
+ assert . equal ( state . testForm . errors , 'Form is invalid' ) ;
994
+ done ( ) ;
995
+ } ) ;
996
+
997
+ TestUtils . Simulate . submit ( formElement ) ;
998
+ } ) ;
999
+
1000
+ it ( 'should set validity on form changes after submit failed' , ( ) => {
1001
+ inputElement . value = 'valid' ;
1002
+ TestUtils . Simulate . change ( inputElement ) ;
1003
+
1004
+ assert . isTrue ( store . getState ( ) . testForm . valid ) ;
1005
+ } ) ;
1006
+ } ) ;
961
1007
} ) ;
0 commit comments