Skip to content

Commit d58c5d1

Browse files
committed
chore: add unit tests
1 parent 79e719b commit d58c5d1

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,4 +624,79 @@ describe('createFormHook', () => {
624624
const input = getByLabelText('Testing')
625625
expect(input).toHaveValue('FirstName')
626626
})
627+
628+
it('should throw if `useTypedAppFormContext` is used without AppForm', () => {
629+
type Person = {
630+
firstName: string
631+
lastName: string
632+
}
633+
const formOpts = formOptions({
634+
defaultValues: {
635+
firstName: 'FirstName',
636+
lastName: 'LastName',
637+
} as Person,
638+
})
639+
640+
function Child() {
641+
const form = useTypedAppFormContext(formOpts)
642+
643+
return (
644+
<form.AppField
645+
name="firstName"
646+
children={(field) => <field.TextField label="Testing" />}
647+
/>
648+
)
649+
}
650+
651+
function Parent() {
652+
const form = useAppForm({
653+
defaultValues: {
654+
firstName: 'FirstName',
655+
lastName: 'LastName',
656+
} as Person,
657+
})
658+
659+
return <Child />
660+
}
661+
662+
expect(() => render(<Parent />)).toThrow()
663+
})
664+
665+
it('should allow using typed app form with form components', () => {
666+
type Person = {
667+
firstName: string
668+
lastName: string
669+
}
670+
const formOpts = formOptions({
671+
defaultValues: {
672+
firstName: 'FirstName',
673+
lastName: 'LastName',
674+
} as Person,
675+
})
676+
677+
function Child() {
678+
const form = useTypedAppFormContext(formOpts)
679+
680+
return <form.SubscribeButton label="Testing" />
681+
}
682+
683+
function Parent() {
684+
const form = useAppForm({
685+
defaultValues: {
686+
firstName: 'FirstName',
687+
lastName: 'LastName',
688+
} as Person,
689+
})
690+
691+
return (
692+
<form.AppForm>
693+
<Child />
694+
</form.AppForm>
695+
)
696+
}
697+
698+
const { getByText } = render(<Parent />)
699+
const button = getByText('Testing')
700+
expect(button).toBeInTheDocument()
701+
})
627702
})

0 commit comments

Comments
 (0)