Skip to content

Commit 85b6eb5

Browse files
author
Erik Rasmussen
committed
Address CodeRabbit review comments
- Hoist sleep helper to describe scope (DRY) - Make waitFor assertions unconditional for proper retry behavior - Remove duplicate sleep declaration in third test All tests still passing (38/38 ✓)
1 parent 632af6e commit 85b6eb5

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/useFieldArray.async-validate-176.test.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import arrayMutators from 'final-form-arrays'
55
import { useFieldArray } from '.'
66
import { ARRAY_ERROR } from 'final-form'
77

8+
// Helper function for async delays
9+
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
10+
811
describe('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

Comments
 (0)