1- import { useForm } from 'react-hook-form' ;
1+ import { useForm , useWatch } from 'react-hook-form' ;
22import { type Lens , useLens } from '@hookform/lenses' ;
33import { renderHook } from '@testing-library/react' ;
44import { expectTypeOf } from 'vitest' ;
@@ -11,7 +11,7 @@ test('lens can be created', () => {
1111 } ) ;
1212
1313 expectTypeOf ( result . current . lens ) . toEqualTypeOf < Lens < { a : string } > > ( ) ;
14- expect ( result . current . lens . interop ( ) ) . toEqual ( { name : '' , control : result . current . form . control } ) ;
14+ expect ( result . current . lens . interop ( ) ) . toEqual ( { name : undefined , control : result . current . form . control } ) ;
1515} ) ;
1616
1717test ( 'lenses are different when created with different forms' , ( ) => {
@@ -27,3 +27,25 @@ test('lenses are different when created with different forms', () => {
2727 expect ( result . current . form . control ) . toBe ( result . current . lens1 . interop ( ) . control ) ;
2828 expect ( result . current . form . control ) . toBe ( result . current . lens2 . interop ( ) . control ) ;
2929} ) ;
30+
31+ test ( 'root lens should be watchable when name is an empty string in LensCore' , ( ) => {
32+ const { result } = renderHook ( ( ) => {
33+ const form = useForm ( {
34+ defaultValues : {
35+ a : 3 ,
36+ b : 'hello' ,
37+ } ,
38+ } ) ;
39+
40+ const lens = useLens ( { control : form . control } ) ;
41+ const interop = lens . interop ( ) ;
42+ const value = useWatch ( interop ) ;
43+
44+ return { form, interop, value } ;
45+ } ) ;
46+
47+ expect ( result . current . interop ) . toEqual ( { name : undefined , control : result . current . form . control } ) ;
48+ expect ( result . current . value ) . toEqual ( { a : 3 , b : 'hello' } ) ;
49+ expect ( result . current . value . a ) . toEqual ( 3 ) ;
50+ expect ( result . current . value . b ) . toEqual ( 'hello' ) ;
51+ } ) ;
0 commit comments