Skip to content

Commit 889c752

Browse files
🧪 add regression coverage for dynamic nested names with useController and watch APIs (#13474)
* Initial plan * test: cover dynamic nested controller watch behavior Agent-Logs-Url: https://github.com/react-hook-form/react-hook-form/sessions/67405d17-6ee3-4636-9155-37a3959c2448 --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Bill <[email protected]>
1 parent 581321c commit 889c752

1 file changed

Lines changed: 155 additions & 0 deletions

File tree

‎src/__tests__/useController.test.tsx‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { Control, FieldPath, FieldValues, UseFormReturn } from '../types';
1212
import { useController } from '../useController';
1313
import { useForm } from '../useForm';
1414
import { FormProvider, useFormContext } from '../useFormContext';
15+
import { useWatch } from '../useWatch';
1516
import isBoolean from '../utils/isBoolean';
1617
import noop from '../utils/noop';
1718

@@ -1475,6 +1476,160 @@ describe('useController', () => {
14751476
expect(result.current.field.value).toBe('value1');
14761477
});
14771478

1479+
it('should keep nested array values when useController and watch APIs switch dynamic names', async () => {
1480+
type FormValues = {
1481+
content: {
1482+
children: {
1483+
data: {
1484+
setting: {
1485+
visibility: string;
1486+
};
1487+
};
1488+
type: string;
1489+
}[];
1490+
};
1491+
};
1492+
1493+
const getVisibilityName = (index: 0 | 1) =>
1494+
`content.children.${index}.data.setting.visibility` as FieldPath<FormValues>;
1495+
1496+
let latestValues: FormValues | undefined;
1497+
1498+
const VisibilityField = ({ name }: { name: FieldPath<FormValues> }) => {
1499+
const { control } = useFormContext<FormValues>();
1500+
const { field } = useController({
1501+
control,
1502+
name,
1503+
});
1504+
1505+
return (
1506+
<>
1507+
<p>controller:{String(field.value)}</p>
1508+
<button type="button" onClick={() => field.onChange('desktop')}>
1509+
update-visibility
1510+
</button>
1511+
</>
1512+
);
1513+
};
1514+
1515+
const VisibilityWatch = ({ name }: { name: FieldPath<FormValues> }) => {
1516+
const { control, watch, getValues } = useFormContext<FormValues>();
1517+
const useWatchValue = useWatch({
1518+
control,
1519+
name,
1520+
});
1521+
const watchValue = watch(name);
1522+
1523+
latestValues = getValues();
1524+
1525+
return (
1526+
<>
1527+
<p>useWatch:{String(useWatchValue)}</p>
1528+
<p>watch:{String(watchValue)}</p>
1529+
</>
1530+
);
1531+
};
1532+
1533+
const App = () => {
1534+
const methods = useForm<FormValues>({
1535+
defaultValues: {
1536+
content: {
1537+
children: [
1538+
{
1539+
data: {
1540+
setting: {
1541+
visibility: 'mobile',
1542+
},
1543+
},
1544+
type: 'product',
1545+
},
1546+
{
1547+
data: {
1548+
setting: {
1549+
visibility: 'both',
1550+
},
1551+
},
1552+
type: 'spacer',
1553+
},
1554+
],
1555+
},
1556+
},
1557+
});
1558+
const [focusedIndex, setFocusedIndex] = useState<0 | 1>(0);
1559+
const name = getVisibilityName(focusedIndex);
1560+
1561+
return (
1562+
<FormProvider {...methods}>
1563+
<button type="button" onClick={() => setFocusedIndex(0)}>
1564+
focus-0
1565+
</button>
1566+
<button type="button" onClick={() => setFocusedIndex(1)}>
1567+
focus-1
1568+
</button>
1569+
<VisibilityField name={name} />
1570+
<VisibilityWatch name={name} />
1571+
</FormProvider>
1572+
);
1573+
};
1574+
1575+
render(<App />);
1576+
1577+
expect(screen.getByText('controller:mobile')).toBeVisible();
1578+
expect(screen.getByText('useWatch:mobile')).toBeVisible();
1579+
expect(screen.getByText('watch:mobile')).toBeVisible();
1580+
expect(latestValues?.content.children[0].data.setting.visibility).toBe(
1581+
'mobile',
1582+
);
1583+
expect(latestValues?.content.children[1].data.setting.visibility).toBe(
1584+
'both',
1585+
);
1586+
1587+
fireEvent.click(screen.getByRole('button', { name: 'focus-1' }));
1588+
1589+
await waitFor(() => {
1590+
expect(screen.getByText('controller:both')).toBeVisible();
1591+
expect(screen.getByText('useWatch:both')).toBeVisible();
1592+
expect(screen.getByText('watch:both')).toBeVisible();
1593+
});
1594+
1595+
expect(latestValues?.content.children[0].data.setting.visibility).toBe(
1596+
'mobile',
1597+
);
1598+
expect(latestValues?.content.children[1].data.setting.visibility).toBe(
1599+
'both',
1600+
);
1601+
1602+
fireEvent.click(screen.getByRole('button', { name: 'update-visibility' }));
1603+
1604+
await waitFor(() => {
1605+
expect(screen.getByText('controller:desktop')).toBeVisible();
1606+
expect(screen.getByText('useWatch:desktop')).toBeVisible();
1607+
expect(screen.getByText('watch:desktop')).toBeVisible();
1608+
});
1609+
1610+
expect(latestValues?.content.children[0].data.setting.visibility).toBe(
1611+
'mobile',
1612+
);
1613+
expect(latestValues?.content.children[1].data.setting.visibility).toBe(
1614+
'desktop',
1615+
);
1616+
1617+
fireEvent.click(screen.getByRole('button', { name: 'focus-0' }));
1618+
1619+
await waitFor(() => {
1620+
expect(screen.getByText('controller:mobile')).toBeVisible();
1621+
expect(screen.getByText('useWatch:mobile')).toBeVisible();
1622+
expect(screen.getByText('watch:mobile')).toBeVisible();
1623+
});
1624+
1625+
expect(latestValues?.content.children[0].data.setting.visibility).toBe(
1626+
'mobile',
1627+
);
1628+
expect(latestValues?.content.children[1].data.setting.visibility).toBe(
1629+
'desktop',
1630+
);
1631+
});
1632+
14781633
it('should react to changing control', () => {
14791634
type FormValues = {
14801635
name: string;

0 commit comments

Comments
 (0)