-
-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathuseField.dynamic-name-869.test.js
More file actions
98 lines (84 loc) · 2.87 KB
/
useField.dynamic-name-869.test.js
File metadata and controls
98 lines (84 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @jest-environment jsdom
*/
import React from 'react'
import { render, cleanup, act } from '@testing-library/react'
import '@testing-library/jest-dom'
import Form from './ReactFinalForm'
import Field from './Field'
describe('useField - Dynamic Name (Issue #869)', () => {
afterEach(cleanup)
it('should keep name and value in sync when field name changes', () => {
const renderSpy = jest.fn()
const TestComponent = ({ fieldName }) => {
return (
<Form
onSubmit={() => {}}
initialValues={{ a: 'value-a', b: 'value-b' }}
>
{() => (
<Field name={fieldName}>
{({ input }) => {
// Log every render to track name/value sync
renderSpy(input.name, input.value)
return <input {...input} data-testid="field" />
}}
</Field>
)}
</Form>
)
}
const { rerender } = render(<TestComponent fieldName="a" />)
// Initial render - field 'a'
expect(renderSpy).toHaveBeenCalledWith('a', 'value-a')
renderSpy.mockClear()
// Change field name from 'a' to 'b'
act(() => {
rerender(<TestComponent fieldName="b" />)
})
// BUG: First render after name change has mismatched name/value
// We get name='b' but value='value-a' (stale)
const calls = renderSpy.mock.calls
// The bug manifests as: first call has name='b' but value='value-a'
// Expected: ALL calls should have name and value in sync
calls.forEach(call => {
const [name, value] = call
if (name === 'a') {
expect(value).toBe('value-a')
} else if (name === 'b') {
expect(value).toBe('value-b') // This will FAIL on first render
}
})
})
it('should have correct value immediately after name change (no stale renders)', () => {
const TestComponent = ({ fieldName }) => {
return (
<Form
onSubmit={() => {}}
initialValues={{ a: 'value-a', b: 'value-b' }}
>
{() => (
<Field name={fieldName}>
{({ input }) => (
<div>
<span data-testid="name">{input.name}</span>
<span data-testid="value">{input.value}</span>
</div>
)}
</Field>
)}
</Form>
)
}
const { rerender, getByTestId } = render(<TestComponent fieldName="a" />)
expect(getByTestId('name')).toHaveTextContent('a')
expect(getByTestId('value')).toHaveTextContent('value-a')
// Change field name
act(() => {
rerender(<TestComponent fieldName="b" />)
})
// IMMEDIATELY after rerender, name and value should be in sync
expect(getByTestId('name')).toHaveTextContent('b')
expect(getByTestId('value')).toHaveTextContent('value-b') // BUG: This will show 'value-a'
})
})