@@ -5,7 +5,7 @@ import Form, { Field, useForm } from '../src';
5
5
import type { FormInstance , ValidateMessages } from '../src/interface' ;
6
6
import { changeValue , getInput , matchError } from './common' ;
7
7
import InfoField , { Input } from './common/InfoField' ;
8
- import timeout from './common/timeout' ;
8
+ import timeout , { waitFakeTime } from './common/timeout' ;
9
9
10
10
describe ( 'Form.Validate' , ( ) => {
11
11
it ( 'required' , async ( ) => {
@@ -1033,4 +1033,60 @@ describe('Form.Validate', () => {
1033
1033
1034
1034
jest . useRealTimers ( ) ;
1035
1035
} ) ;
1036
+
1037
+ it ( 'dirty' , async ( ) => {
1038
+ jest . useFakeTimers ( ) ;
1039
+
1040
+ const formRef = React . createRef < FormInstance > ( ) ;
1041
+
1042
+ const Demo = ( { touchMessage, validateMessage } ) => (
1043
+ < Form ref = { formRef } >
1044
+ < InfoField name = "touch" rules = { [ { required : true , message : touchMessage } ] } >
1045
+ < Input />
1046
+ </ InfoField >
1047
+ < InfoField name = "validate" rules = { [ { required : true , message : validateMessage } ] } >
1048
+ < Input />
1049
+ </ InfoField >
1050
+ < InfoField name = "noop" rules = { [ { required : true , message : 'noop' } ] } >
1051
+ < Input />
1052
+ </ InfoField >
1053
+ </ Form >
1054
+ ) ;
1055
+
1056
+ const { container, rerender } = render (
1057
+ < Demo touchMessage = "touch" validateMessage = "validate" /> ,
1058
+ ) ;
1059
+
1060
+ fireEvent . change ( container . querySelectorAll ( 'input' ) [ 0 ] , {
1061
+ target : {
1062
+ value : 'light' ,
1063
+ } ,
1064
+ } ) ;
1065
+ fireEvent . change ( container . querySelectorAll ( 'input' ) [ 0 ] , {
1066
+ target : {
1067
+ value : '' ,
1068
+ } ,
1069
+ } ) ;
1070
+
1071
+ formRef . current . validateFields ( [ 'validate' ] ) ;
1072
+
1073
+ await waitFakeTime ( ) ;
1074
+ matchError ( container . querySelectorAll < HTMLDivElement > ( '.field' ) [ 0 ] , `touch` ) ;
1075
+ matchError ( container . querySelectorAll < HTMLDivElement > ( '.field' ) [ 1 ] , `validate` ) ;
1076
+ matchError ( container . querySelectorAll < HTMLDivElement > ( '.field' ) [ 2 ] , false ) ;
1077
+
1078
+
1079
+ // Revalidate
1080
+ rerender (
1081
+ < Demo touchMessage = "new_touch" validateMessage = "new_validate" /> ,
1082
+ ) ;
1083
+ formRef . current . validateFields ( { dirty : true } ) ;
1084
+
1085
+ await waitFakeTime ( ) ;
1086
+ matchError ( container . querySelectorAll < HTMLDivElement > ( '.field' ) [ 0 ] , `new_touch` ) ;
1087
+ matchError ( container . querySelectorAll < HTMLDivElement > ( '.field' ) [ 1 ] , `new_validate` ) ;
1088
+ matchError ( container . querySelectorAll < HTMLDivElement > ( '.field' ) [ 2 ] , false ) ;
1089
+
1090
+ jest . useRealTimers ( ) ;
1091
+ } ) ;
1036
1092
} ) ;
0 commit comments