@@ -294,3 +294,118 @@ test('validate warns and skips validation on a disabled field', async () => {
294294 // Clean up the mocks
295295 consoleWarnSpy . mockRestore ( ) ;
296296} ) ;
297+
298+ describe ( 'isValidated state' , ( ) => {
299+ test ( 'field starts with isValidated as false' , async ( ) => {
300+ const { isValidated } = await renderSetup ( ( ) => {
301+ return useFormField ( { label : 'Field' , initialValue : 'bar' } ) . state ;
302+ } ) ;
303+
304+ expect ( isValidated . value ) . toBe ( false ) ;
305+ } ) ;
306+
307+ test ( 'isValidated remains false after programmatic validation without schema' , async ( ) => {
308+ const { isValidated, validate } = await renderSetup ( ( ) => {
309+ return useFormField ( { label : 'Field' , initialValue : 'bar' } ) . state ;
310+ } ) ;
311+
312+ expect ( isValidated . value ) . toBe ( false ) ;
313+ await validate ( ) ;
314+ expect ( isValidated . value ) . toBe ( false ) ; // Should remain false - programmatic validation
315+ } ) ;
316+
317+ test ( 'isValidated remains false after programmatic validation with schema' , async ( ) => {
318+ const { isValidated, validate } = await renderSetup ( ( ) => {
319+ return useFormField ( {
320+ label : 'Field' ,
321+ initialValue : 'bar' ,
322+ schema : defineStandardSchema ( async ( ) => {
323+ return { value : 'bar' } ;
324+ } ) ,
325+ } ) . state ;
326+ } ) ;
327+
328+ expect ( isValidated . value ) . toBe ( false ) ;
329+ await validate ( ) ;
330+ expect ( isValidated . value ) . toBe ( false ) ; // Should remain false - programmatic validation
331+ } ) ;
332+
333+ test ( 'isValidated remains false after programmatic validation with errors' , async ( ) => {
334+ const { isValidated, validate } = await renderSetup ( ( ) => {
335+ return useFormField ( {
336+ label : 'Field' ,
337+ initialValue : '' ,
338+ schema : defineStandardSchema ( async ( ) => {
339+ return { issues : [ { message : 'Required' , path : [ 'field' ] } ] } ;
340+ } ) ,
341+ } ) . state ;
342+ } ) ;
343+
344+ expect ( isValidated . value ) . toBe ( false ) ;
345+ await validate ( true ) ;
346+ expect ( isValidated . value ) . toBe ( false ) ; // Should remain false - programmatic validation
347+ } ) ;
348+
349+ test ( 'isValidated can be set manually' , async ( ) => {
350+ const { isValidated, setIsValidated } = await renderSetup ( ( ) => {
351+ return useFormField ( { label : 'Field' , initialValue : 'bar' } ) . state ;
352+ } ) ;
353+
354+ expect ( isValidated . value ) . toBe ( false ) ;
355+ setIsValidated ( true ) ;
356+ expect ( isValidated . value ) . toBe ( true ) ;
357+ setIsValidated ( false ) ;
358+ expect ( isValidated . value ) . toBe ( false ) ;
359+ } ) ;
360+
361+ test ( 'isValidated becomes true after form submit attempt' , async ( ) => {
362+ const { form, field } = await renderSetup (
363+ ( ) => {
364+ const form = useForm ( {
365+ initialValues : { field : 'valid' } ,
366+ schema : defineStandardSchema < { field : string } > ( async ( ) => {
367+ return { value : { field : 'valid' } } ;
368+ } ) ,
369+ } ) ;
370+ return { form } ;
371+ } ,
372+ ( ) => {
373+ const field = useFormField ( {
374+ path : 'field' ,
375+ schema : defineStandardSchema ( async ( ) => {
376+ return { value : 'valid' } ;
377+ } ) ,
378+ } ) ;
379+ return { field } ;
380+ } ,
381+ ) ;
382+
383+ expect ( field . state . isValidated . value ) . toBe ( false ) ;
384+
385+ // Submit attempt is a user interaction
386+ const handleSubmit = form . handleSubmit ( async ( ) => { } ) ;
387+ await handleSubmit ( ) ;
388+
389+ expect ( field . state . isValidated . value ) . toBe ( true ) ;
390+ } ) ;
391+
392+ test ( 'isValidated is independent for pathless fields' , async ( ) => {
393+ const { field1, field2 } = await renderSetup ( ( ) => {
394+ const field1 = useFormField ( { label : 'Field1' , initialValue : 'bar' } ) ;
395+ const field2 = useFormField ( { label : 'Field2' , initialValue : 'baz' } ) ;
396+ return { field1, field2 } ;
397+ } ) ;
398+
399+ expect ( field1 . state . isValidated . value ) . toBe ( false ) ;
400+ expect ( field2 . state . isValidated . value ) . toBe ( false ) ;
401+
402+ // Manually set isValidated (simulating user interaction)
403+ field1 . state . setIsValidated ( true ) ;
404+ expect ( field1 . state . isValidated . value ) . toBe ( true ) ;
405+ expect ( field2 . state . isValidated . value ) . toBe ( false ) ;
406+
407+ field2 . state . setIsValidated ( true ) ;
408+ expect ( field1 . state . isValidated . value ) . toBe ( true ) ;
409+ expect ( field2 . state . isValidated . value ) . toBe ( true ) ;
410+ } ) ;
411+ } ) ;
0 commit comments