@@ -5,7 +5,7 @@ import Form, { Field, useForm } from '../src';
55import type { FormInstance , ValidateMessages } from '../src/interface' ;
66import { changeValue , getInput , matchError } from './common' ;
77import InfoField , { Input } from './common/InfoField' ;
8- import timeout from './common/timeout' ;
8+ import timeout , { waitFakeTime } from './common/timeout' ;
99
1010describe ( 'Form.Validate' , ( ) => {
1111 it ( 'required' , async ( ) => {
@@ -1033,4 +1033,60 @@ describe('Form.Validate', () => {
10331033
10341034 jest . useRealTimers ( ) ;
10351035 } ) ;
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+ } ) ;
10361092} ) ;
0 commit comments