@@ -457,4 +457,55 @@ describe("useField", () => {
457457 expect ( spy . mock . calls [ 3 ] [ 1 ] ) . toBe ( spy . mock . calls [ 2 ] [ 1 ] ) ; // onFocus
458458 expect ( spy . mock . calls [ 3 ] [ 2 ] ) . toBe ( spy . mock . calls [ 2 ] [ 2 ] ) ; // onBlur
459459 } ) ;
460+ 461+ it ( "should handle null values correctly with allowNull" , ( ) => {
462+ const spy = jest . fn ( ) ;
463+ const MyField = ( { name } ) => {
464+ const { input } = useField ( name , {
465+ subscription : { value : true } ,
466+ allowNull : true ,
467+ } ) ;
468+ spy ( input . value ) ;
469+ // Convert null to empty string for the input element to avoid React warnings
470+ return (
471+ < input { ...input } value = { input . value === null ? "" : input . value } />
472+ ) ;
473+ } ;
474+ const { rerender } = render (
475+ < Form onSubmit = { onSubmitMock } initialValues = { { myField : null } } >
476+ { ( ) => (
477+ < form >
478+ < MyField name = "myField" />
479+ </ form >
480+ ) }
481+ </ Form > ,
482+ ) ;
483+ 484+ // Change to non-null value
485+ rerender (
486+ < Form onSubmit = { onSubmitMock } initialValues = { { myField : "test" } } >
487+ { ( ) => (
488+ < form >
489+ < MyField name = "myField" />
490+ </ form >
491+ ) }
492+ </ Form > ,
493+ ) ;
494+ 495+ // Change back to null
496+ rerender (
497+ < Form onSubmit = { onSubmitMock } initialValues = { { myField : null } } >
498+ { ( ) => (
499+ < form >
500+ < MyField name = "myField" />
501+ </ form >
502+ ) }
503+ </ Form > ,
504+ ) ;
505+ 506+ const calls = spy . mock . calls . map ( ( call ) => call [ 0 ] ) ;
507+ expect ( calls ) . toContain ( null ) ; // At least one call with null
508+ expect ( calls ) . toContain ( "test" ) ; // At least one call with 'test'
509+ expect ( calls [ calls . length - 1 ] ) . toBe ( null ) ; // Last call is null
510+ } ) ;
460511} ) ;
0 commit comments