|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | | -import { render } from '@testing-library/react' |
| 2 | +import { render, waitFor } from '@testing-library/react' |
3 | 3 | import { formOptions } from '@tanstack/form-core' |
4 | 4 | import userEvent from '@testing-library/user-event' |
5 | 5 | import { createFormHook, createFormHookContexts, useStore } from '../src' |
@@ -699,4 +699,63 @@ describe('createFormHook', () => { |
699 | 699 | const button = getByText('Testing') |
700 | 700 | expect(button).toBeInTheDocument() |
701 | 701 | }) |
| 702 | + |
| 703 | + it('should render FieldGroup Subscribe without selector (default identity)', async () => { |
| 704 | + const formOpts = formOptions({ |
| 705 | + defaultValues: { |
| 706 | + person: { |
| 707 | + firstName: 'FirstName', |
| 708 | + lastName: 'LastName', |
| 709 | + }, |
| 710 | + }, |
| 711 | + }) |
| 712 | + |
| 713 | + const ChildFormAsField = withFieldGroup({ |
| 714 | + defaultValues: formOpts.defaultValues.person, |
| 715 | + render: ({ group }) => { |
| 716 | + return ( |
| 717 | + <div> |
| 718 | + <group.Field |
| 719 | + name="lastName" |
| 720 | + children={(field) => ( |
| 721 | + <label> |
| 722 | + Last Name: |
| 723 | + <input |
| 724 | + data-testid="lastName" |
| 725 | + value={field.state.value} |
| 726 | + onBlur={field.handleBlur} |
| 727 | + onChange={(e) => field.handleChange(e.target.value)} |
| 728 | + /> |
| 729 | + </label> |
| 730 | + )} |
| 731 | + /> |
| 732 | + <group.Subscribe |
| 733 | + children={(state) => ( |
| 734 | + <span data-testid="state-lastName"> |
| 735 | + {state.values.lastName} |
| 736 | + </span> |
| 737 | + )} |
| 738 | + /> |
| 739 | + </div> |
| 740 | + ) |
| 741 | + }, |
| 742 | + }) |
| 743 | + |
| 744 | + const Parent = () => { |
| 745 | + const form = useAppForm({ |
| 746 | + ...formOpts, |
| 747 | + }) |
| 748 | + return <ChildFormAsField form={form} fields="person" /> |
| 749 | + } |
| 750 | + |
| 751 | + const { getByTestId } = render(<Parent />) |
| 752 | + const input = getByTestId('lastName') |
| 753 | + const stateLastName = getByTestId('state-lastName') |
| 754 | + |
| 755 | + expect(stateLastName).toHaveTextContent('LastName') |
| 756 | + |
| 757 | + await user.clear(input) |
| 758 | + await user.type(input, 'Updated') |
| 759 | + await waitFor(() => expect(stateLastName).toHaveTextContent('Updated')) |
| 760 | + }) |
702 | 761 | }) |
0 commit comments