@@ -6,6 +6,7 @@ import useFieldApi from '../../files/use-field-api';
66import componentTypes from '../../files/component-types' ;
77import Form from '../../files/form' ;
88import RendererContext from '../../files/renderer-context' ;
9+ import validatorTypes from '../../files/validator-types' ;
910
1011describe ( 'useFieldApi' , ( ) => {
1112 const Catcher = ( { children } ) => children ;
@@ -179,4 +180,39 @@ describe('useFieldApi', () => {
179180 expect ( wrapper . find ( 'input' ) . prop ( 'value' ) ) . toEqual ( '' ) ;
180181 expect ( registerInputFileSpy ) . toHaveBeenCalledWith ( 'file-input' ) ;
181182 } ) ;
183+
184+ it ( 'should not crash when passing validate directly' , ( ) => {
185+ const TestDummy = ( { validate } ) => {
186+ const { input } = useFieldApi ( {
187+ name : 'foo' ,
188+ validate : validate ? [ { type : validatorTypes . REQUIRED } ] : [ { type : validatorTypes . URL } ]
189+ } ) ;
190+ return < input { ...input } id = "foo" /> ;
191+ } ;
192+
193+ const FormWrapper = ( { validate = true } ) => (
194+ < Form onSubmit = { jest . fn ( ) } >
195+ { ( { handleSubmit } ) => (
196+ < form onSubmit = { handleSubmit } >
197+ < RendererContext . Provider
198+ value = { {
199+ validatorMapper : { required : ( ) => ( value ) => ( ! value ? 'required' : undefined ) , url : ( ) => jest . fn ( ) } ,
200+ formOptions : { }
201+ } }
202+ >
203+ < TestDummy validate = { validate } />
204+ </ RendererContext . Provider >
205+ </ form >
206+ ) }
207+ </ Form >
208+ ) ;
209+
210+ const wrapper = mount ( < FormWrapper /> ) ;
211+ expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 1 ) ;
212+ wrapper . find ( '#foo' ) . simulate ( 'change' , { target : { value : 'bar' } } ) ;
213+ wrapper . update ( ) ;
214+ wrapper . setProps ( { validate : false } ) ;
215+ wrapper . update ( ) ;
216+ expect ( wrapper . find ( 'input' ) ) . toHaveLength ( 1 ) ;
217+ } ) ;
182218} ) ;
0 commit comments