@@ -57,41 +57,58 @@ interface RemixFormConfig {
57
57
[ key : string ] : unknown ;
58
58
}
59
59
60
- vi . mock ( 'remix-hook-form' , ( ) => ( {
61
- RemixFormProvider : ( { children } : { children : ReactNode } ) => children ,
62
- useRemixForm : ( config : RemixFormConfig ) => {
63
- return {
64
- ...config ,
65
- getValues : ( _name : string ) => testInputValue ,
66
- reset : vi . fn ( ( ) => {
67
- testInputValue = '' ;
68
- // Force re-render by dispatching a custom event
69
- const inputs = document . querySelectorAll ( 'input[name="text"]' ) ;
70
- inputs . forEach ( input => {
71
- ( input as HTMLInputElement ) . value = '' ;
72
- } ) ;
73
- } ) ,
74
- setValue : vi . fn ( ( _name : string , value : string ) => {
75
- testInputValue = value ;
76
- } ) ,
77
- register : vi . fn ( ( name : string ) => ( {
78
- name,
79
- onChange : ( e : ChangeEvent < HTMLInputElement > ) => {
80
- testInputValue = e . target . value ;
81
- } ,
82
- value : testInputValue
83
- } ) ) ,
84
- handleSubmit : vi . fn ( ( onValid : ( data : { text : string } ) => void ) => ( e : FormEvent ) => {
85
- e . preventDefault ( ) ;
86
- if ( testInputValue ?. trim ( ) ) {
87
- onValid ( { text : testInputValue . trim ( ) } ) ;
88
- }
89
- } ) ,
90
- formState : { errors : { } } ,
91
- watch : vi . fn ( ( _name : string ) => testInputValue )
92
- } ;
93
- }
94
- } ) ) ;
60
+ vi . mock ( 'remix-hook-form' , ( ) => {
61
+ let latestConfig : RemixFormConfig | undefined ;
62
+ return {
63
+ RemixFormProvider : ( { children } : { children : ReactNode } ) => children ,
64
+ useRemixForm : ( config : RemixFormConfig ) => {
65
+ latestConfig = config ;
66
+ return {
67
+ ...config ,
68
+ getValues : ( _name : string ) => testInputValue ,
69
+ reset : vi . fn ( ( ) => {
70
+ testInputValue = '' ;
71
+ // Force re-render by dispatching a custom event
72
+ const inputs = document . querySelectorAll ( 'input[name="text"]' ) ;
73
+ inputs . forEach ( input => {
74
+ ( input as HTMLInputElement ) . value = '' ;
75
+ } ) ;
76
+ } ) ,
77
+ setValue : vi . fn ( ( _name : string , value : string ) => {
78
+ testInputValue = value ;
79
+ } ) ,
80
+ register : vi . fn ( ( name : string ) => ( {
81
+ name,
82
+ onChange : ( e : ChangeEvent < HTMLInputElement > ) => {
83
+ testInputValue = e . target . value ;
84
+ } ,
85
+ value : testInputValue
86
+ } ) ) ,
87
+ handleSubmit : vi . fn ( ( arg ?: unknown ) => {
88
+ // Support both usages:
89
+ // 1) onSubmit={methods.handleSubmit} → arg is the FormEvent
90
+ // 2) onSubmit={methods.handleSubmit(onValid)} → arg is the onValid callback
91
+ const isEvent = arg && typeof ( arg as FormEvent ) . preventDefault === 'function' ;
92
+ if ( isEvent ) {
93
+ const e = arg as FormEvent ;
94
+ e . preventDefault ( ) ;
95
+ const onValid = latestConfig ?. submitHandlers ?. onValid ;
96
+ if ( onValid && testInputValue ?. trim ( ) ) onValid ( { text : testInputValue . trim ( ) } ) ;
97
+ return undefined ;
98
+ }
99
+ const maybeOnValid = arg as ( ( data : { text : string } ) => void ) | undefined ;
100
+ return ( e : FormEvent ) => {
101
+ e . preventDefault ( ) ;
102
+ const onValid = maybeOnValid || latestConfig ?. submitHandlers ?. onValid ;
103
+ if ( onValid && testInputValue ?. trim ( ) ) onValid ( { text : testInputValue . trim ( ) } ) ;
104
+ } ;
105
+ } ) ,
106
+ formState : { errors : { } } ,
107
+ watch : vi . fn ( ( _name : string ) => testInputValue )
108
+ } ;
109
+ }
110
+ } ;
111
+ } ) ;
95
112
96
113
function renderWithRouter ( ui : ReactElement ) {
97
114
const router = createMemoryRouter ( [ { path : '/' , element : ui } ] , { initialEntries : [ '/' ] } ) ;
0 commit comments