File tree Expand file tree Collapse file tree 1 file changed +75
-0
lines changed
packages/react-form/tests Expand file tree Collapse file tree 1 file changed +75
-0
lines changed Original file line number Diff line number Diff 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} )
You can’t perform that action at this time.
0 commit comments