Skip to content

Commit c80ebbc

Browse files
committed
test: add coverage for Subscribe default selector
1 parent bf2642f commit c80ebbc

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

packages/react-form/tests/createFormHook.test.tsx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { render } from '@testing-library/react'
2+
import { render, waitFor } from '@testing-library/react'
33
import { formOptions } from '@tanstack/form-core'
44
import userEvent from '@testing-library/user-event'
55
import { createFormHook, createFormHookContexts, useStore } from '../src'
@@ -699,4 +699,61 @@ describe('createFormHook', () => {
699699
const button = getByText('Testing')
700700
expect(button).toBeInTheDocument()
701701
})
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">{state.values.lastName}</span>
735+
)}
736+
/>
737+
</div>
738+
)
739+
},
740+
})
741+
742+
const Parent = () => {
743+
const form = useAppForm({
744+
...formOpts,
745+
})
746+
return <ChildFormAsField form={form} fields="person" />
747+
}
748+
749+
const { getByTestId } = render(<Parent />)
750+
const input = getByTestId('lastName')
751+
const stateLastName = getByTestId('state-lastName')
752+
753+
expect(stateLastName).toHaveTextContent('LastName')
754+
755+
await user.clear(input)
756+
await user.type(input, 'Updated')
757+
await waitFor(() => expect(stateLastName).toHaveTextContent('Updated'))
758+
})
702759
})

packages/react-form/tests/useForm.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,4 +1075,45 @@ describe('useForm', () => {
10751075
await user.click(getByText('Rerender'))
10761076
await findByText('Another')
10771077
})
1078+
1079+
it('should render Subscribe without selector (default identity)', async () => {
1080+
function Comp() {
1081+
const form = useForm({
1082+
defaultValues: {
1083+
name: 'test',
1084+
},
1085+
})
1086+
1087+
return (
1088+
<>
1089+
<form.Field
1090+
name="name"
1091+
children={(field) => (
1092+
<input
1093+
data-testid="input"
1094+
value={field.state.value}
1095+
onChange={(e) => field.handleChange(e.target.value)}
1096+
/>
1097+
)}
1098+
/>
1099+
1100+
<form.Subscribe
1101+
children={(state) => (
1102+
<span data-testid="state-value">{state.values.name}</span>
1103+
)}
1104+
/>
1105+
</>
1106+
)
1107+
}
1108+
1109+
const { getByTestId } = render(<Comp />)
1110+
const input = getByTestId('input')
1111+
const stateValue = getByTestId('state-value')
1112+
1113+
expect(stateValue).toHaveTextContent('test')
1114+
1115+
await user.clear(input)
1116+
await user.type(input, 'updated')
1117+
await waitFor(() => expect(stateValue).toHaveTextContent('updated'))
1118+
})
10781119
})

0 commit comments

Comments
 (0)