@@ -2,7 +2,7 @@ import userEvents from '@testing-library/user-event';
22import { act , waitFor } from '@testing-library/react' ;
33
44import { renderWithForm } from '../../../test/index' ;
5- import { SubmitButton } from '../../form' ;
5+ import { ResetButton , SubmitButton } from '../../form' ;
66import { TextInput } from '../../fields' ;
77
88import { Form } from './index' ;
@@ -90,4 +90,164 @@ describe('<Form />', () => {
9090 expect ( getByText ( 'Internal error' ) ) . toBeInTheDocument ( ) ;
9191 } ) ;
9292 } ) ;
93+
94+ it ( 'should handle reset button behavior correctly' , async ( ) => {
95+ const onSubmit = jest . fn ( ( data ) => { } ) ;
96+
97+ const { getByRole, getByText } = renderWithForm (
98+ < >
99+ < Form . Item
100+ name = "test"
101+ label = "Test"
102+ rules = { [
103+ {
104+ async validator ( rule , value ) {
105+ return value ? Promise . resolve ( ) : Promise . reject ( 'Required' ) ;
106+ } ,
107+ } ,
108+ ] }
109+ >
110+ < TextInput />
111+ </ Form . Item >
112+
113+ < SubmitButton > Submit</ SubmitButton >
114+ < ResetButton > Reset</ ResetButton >
115+ < Form . SubmitError />
116+ </ > ,
117+ { formProps : { onSubmit } } ,
118+ ) ;
119+
120+ const input = getByRole ( 'textbox' ) ;
121+ const submitButton = getByRole ( 'button' , { name : 'Submit' } ) ;
122+ const resetButton = getByRole ( 'button' , { name : 'Reset' } ) ;
123+
124+ // Check initial button states
125+ expect ( submitButton ) . toBeEnabled ( ) ;
126+ expect ( resetButton ) . toBeDisabled ( ) ;
127+
128+ // Click submit button and verify state
129+ await userEvents . click ( submitButton ) ;
130+
131+ await waitFor ( ( ) => {
132+ expect ( getByText ( 'Required' ) ) . toBeInTheDocument ( ) ;
133+ } ) ;
134+
135+ expect ( submitButton ) . toBeDisabled ( ) ;
136+ expect ( resetButton ) . toBeDisabled ( ) ;
137+
138+ // Type into input and verify reset button becomes active
139+ await act ( async ( ) => {
140+ await userEvents . type ( input , 'test' ) ;
141+ } ) ;
142+
143+ expect ( resetButton ) . toBeEnabled ( ) ;
144+ expect ( submitButton ) . toBeEnabled ( ) ;
145+
146+ // Click reset button and verify states
147+ await userEvents . click ( resetButton ) ;
148+
149+ await waitFor ( ( ) => {
150+ expect ( resetButton ) . toBeDisabled ( ) ;
151+ } ) ;
152+
153+ expect ( input ) . toHaveValue ( '' ) ;
154+ expect ( submitButton ) . toBeEnabled ( ) ;
155+ } ) ;
156+
157+ it ( 'should respect the isDisabled:true property for ResetButton and SubmitButton' , async ( ) => {
158+ const onSubmit = jest . fn ( ( ) => Promise . resolve ( ) ) ;
159+ const onSubmitFailed = jest . fn ( ( ) => { } ) ;
160+
161+ const { getByRole } = renderWithForm (
162+ < >
163+ < Form . Item
164+ name = "test"
165+ label = "Test"
166+ rules = { [
167+ {
168+ validator ( rule , value ) {
169+ return value ? Promise . resolve ( ) : Promise . reject ( 'Required' ) ;
170+ } ,
171+ } ,
172+ ] }
173+ >
174+ < TextInput />
175+ </ Form . Item >
176+
177+ { /* Explicitly set isDisabled */ }
178+ < SubmitButton isDisabled = { true } > Submit</ SubmitButton >
179+ < ResetButton isDisabled = { true } > Reset</ ResetButton >
180+ </ > ,
181+ { formProps : { onSubmit, onSubmitFailed } } ,
182+ ) ;
183+
184+ const input = getByRole ( 'textbox' ) ;
185+ const submitButton = getByRole ( 'button' , { name : 'Submit' } ) ;
186+ const resetButton = getByRole ( 'button' , { name : 'Reset' } ) ;
187+
188+ // Check that both buttons are disabled because isDisabled is true
189+ expect ( submitButton ) . toBeDisabled ( ) ;
190+ expect ( resetButton ) . toBeDisabled ( ) ;
191+
192+ // Try typing in input
193+ await act ( async ( ) => {
194+ await userEvents . type ( input , 'test' ) ;
195+ } ) ;
196+
197+ // Buttons should still be disabled because of isDisabled
198+ expect ( submitButton ) . toBeDisabled ( ) ;
199+ expect ( resetButton ) . toBeDisabled ( ) ;
200+ } ) ;
201+
202+ it ( 'Respect isDisabled:false property for ResetButton and SubmitButton' , async ( ) => {
203+ const onSubmit = jest . fn ( ( ) => Promise . resolve ( ) ) ;
204+ const onSubmitFailed = jest . fn ( ( ) => { } ) ;
205+
206+ // Render with isDisabled = false for buttons
207+ const { getByRole : getByRoleNew } = renderWithForm (
208+ < >
209+ < Form . Item
210+ name = "test"
211+ label = "Test"
212+ rules = { [
213+ {
214+ validator ( rule , value ) {
215+ return value ? Promise . resolve ( ) : Promise . reject ( 'Required' ) ;
216+ } ,
217+ } ,
218+ ] }
219+ >
220+ < TextInput />
221+ </ Form . Item >
222+
223+ < SubmitButton isDisabled = { false } > Submit</ SubmitButton >
224+ < ResetButton isDisabled = { false } > Reset</ ResetButton >
225+ </ > ,
226+ { formProps : { onSubmit, onSubmitFailed } } ,
227+ ) ;
228+
229+ const submitButton = getByRoleNew ( 'button' , { name : 'Submit' } ) ;
230+ const resetButton = getByRoleNew ( 'button' , { name : 'Reset' } ) ;
231+
232+ // Verify default behavior without isDisabled=true
233+ expect ( submitButton ) . toBeEnabled ( ) ;
234+ expect ( resetButton ) . toBeDisabled ( ) ;
235+
236+ // Type in input and verify reset button becomes enabled
237+ const inputNew = getByRoleNew ( 'textbox' ) ;
238+ await act ( async ( ) => {
239+ await userEvents . type ( inputNew , 'test' ) ;
240+ } ) ;
241+
242+ expect ( resetButton ) . toBeEnabled ( ) ;
243+
244+ // Click reset button and verify behavior
245+ await userEvents . click ( resetButton ) ;
246+
247+ await waitFor ( ( ) => {
248+ expect ( inputNew ) . toHaveValue ( '' ) ;
249+ expect ( submitButton ) . toBeEnabled ( ) ;
250+ expect ( resetButton ) . toBeDisabled ( ) ;
251+ } ) ;
252+ } ) ;
93253} ) ;
0 commit comments