@@ -3,6 +3,7 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
3
3
import { useFormContext , useWatch } from 'react-hook-form' ;
4
4
5
5
import { NumberInput } from './NumberInput' ;
6
+ import { TextInput } from './TextInput' ;
6
7
import { AdminContext } from '../AdminContext' ;
7
8
import { SaveButton } from '../button' ;
8
9
import { SimpleForm , Toolbar } from '../form' ;
@@ -290,6 +291,45 @@ describe('<NumberInput />', () => {
290
291
} ) ;
291
292
expect ( onSubmit . mock . calls [ 0 ] [ 0 ] . views ) . toBeNull ( ) ;
292
293
} ) ;
294
+
295
+ it ( 'should reformat if format function gets changed' , async ( ) => {
296
+ const AngleInput = props => {
297
+ const unit = useWatch ( { name : 'unit' } ) ;
298
+ return (
299
+ < NumberInput
300
+ format = { v =>
301
+ unit === 'radian' ? v : ( v / Math . PI ) * 180
302
+ }
303
+ { ...props }
304
+ />
305
+ ) ;
306
+ } ;
307
+
308
+ const onSubmit = jest . fn ( ) ;
309
+
310
+ render (
311
+ < AdminContext >
312
+ < SimpleForm
313
+ defaultValues = { { unit : 'radian' , value : Math . PI / 2 } }
314
+ onSubmit = { onSubmit }
315
+ >
316
+ < AngleInput resource = "posts" source = "value" />
317
+ < TextInput resource = "posts" source = "unit" />
318
+ </ SimpleForm >
319
+ </ AdminContext >
320
+ ) ;
321
+ const valueInput = screen . getByLabelText (
322
+ 'resources.posts.fields.value'
323
+ ) ;
324
+ const unitInput = screen . getByLabelText (
325
+ 'resources.posts.fields.unit'
326
+ ) ;
327
+ fireEvent . change ( unitInput , { target : { value : 'degree' } } ) ;
328
+
329
+ await waitFor ( ( ) => {
330
+ expect ( ( valueInput as HTMLInputElement ) . value ) . toEqual ( '90' ) ;
331
+ } ) ;
332
+ } ) ;
293
333
} ) ;
294
334
295
335
describe ( 'onChange event' , ( ) => {
0 commit comments