@@ -5,9 +5,11 @@ import arrayMutators from 'final-form-arrays'
55import { useFieldArray } from '.'
66import { ARRAY_ERROR } from 'final-form'
77
8+ // Helper function for async delays
9+ const sleep = ( ms : number ) => new Promise ( resolve => setTimeout ( resolve , ms ) )
10+
811describe ( 'useFieldArray async validate regression #176' , ( ) => {
912 it ( 'should await async validate and not store Promise as ARRAY_ERROR' , async ( ) => {
10- const sleep = ( ms : number ) => new Promise ( resolve => setTimeout ( resolve , ms ) )
1113
1214 const asyncValidate = jest . fn ( async ( value : any [ ] ) => {
1315 await sleep ( 10 )
@@ -51,11 +53,9 @@ describe('useFieldArray async validate regression #176', () => {
5153 await waitFor ( ( ) => {
5254 // The error should NOT be a Promise
5355 const error = formState . errors ?. items
54- if ( error ) {
55- expect ( error ) . not . toBeInstanceOf ( Promise )
56- }
56+ expect ( error ) . not . toBeInstanceOf ( Promise )
5757
58- // Check ARRAY_ERROR specifically
58+ // Check ARRAY_ERROR specifically if it's an array
5959 if ( Array . isArray ( error ) ) {
6060 const arrayError = ( error as any ) [ ARRAY_ERROR ]
6161 expect ( arrayError ) . not . toBeInstanceOf ( Promise )
@@ -106,8 +106,6 @@ describe('useFieldArray async validate regression #176', () => {
106106 } )
107107
108108 it ( 'should properly handle async validation errors' , async ( ) => {
109- const sleep = ( ms : number ) => new Promise ( resolve => setTimeout ( resolve , ms ) )
110-
111109 const asyncValidateWithError = jest . fn ( async ( value : any [ ] ) => {
112110 await sleep ( 10 )
113111 return value && value . length < 5 ? 'Need at least 5 items' : undefined
@@ -148,11 +146,11 @@ describe('useFieldArray async validate regression #176', () => {
148146 // Error should be resolved, not a Promise
149147 await waitFor ( ( ) => {
150148 const error = formState . errors ?. items
151- if ( Array . isArray ( error ) ) {
152- const arrayError = ( error as any ) [ ARRAY_ERROR ]
153- expect ( arrayError ) . toBe ( 'Need at least 5 items' )
154- expect ( arrayError ) . not . toBeInstanceOf ( Promise )
155- }
149+ expect ( formState . errors ?. items ) . toBeDefined ( )
150+ expect ( Array . isArray ( error ) ) . toBe ( true )
151+ const arrayError = ( error as any ) [ ARRAY_ERROR ]
152+ expect ( arrayError ) . toBe ( 'Need at least 5 items' )
153+ expect ( arrayError ) . not . toBeInstanceOf ( Promise )
156154 } )
157155 } )
158156} )
0 commit comments