@@ -90,47 +90,164 @@ const reconfigureControl = (
9090 ...controlOverrides ,
9191} ) ;
9292
93+ type FormValues = {
94+ flat : string ;
95+ nested : {
96+ object : { test : string } ;
97+ array : { test : string } [ ] ;
98+ } ;
99+ } ;
100+
93101describe ( 'useTypedField' , ( ) => {
94- const control = reconfigureControl ( ) ;
95- const { result } = renderHook ( ( ) =>
96- useTypedController < {
97- test : string ;
98- test1 : { test2 : string } [ ] ;
99- } > ( {
100- control,
101- } ) ,
102- ) ;
103- const TypedController = result . current ;
102+ it ( 'should render correctly when as is input and name is string' , ( ) => {
103+ const control = reconfigureControl ( ) ;
104+ const { result } = renderHook ( ( ) =>
105+ useTypedController < FormValues > ( {
106+ control,
107+ } ) ,
108+ ) ;
109+ const TypedController = result . current ;
110+
111+ const { asFragment } = render (
112+ < TypedController as = "input" name = "flat" defaultValue = "test" /> ,
113+ ) ;
114+
115+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
116+ } ) ;
117+
118+ it ( 'should render correctly when name is string and render is textarea' , ( ) => {
119+ const control = reconfigureControl ( ) ;
120+ const { result } = renderHook ( ( ) =>
121+ useTypedController < FormValues > ( {
122+ control,
123+ } ) ,
124+ ) ;
125+ const TypedController = result . current ;
126+
127+ const { asFragment } = render (
128+ < TypedController
129+ name = "flat"
130+ defaultValue = "test"
131+ render = { ( props ) => < textarea { ...props } /> }
132+ /> ,
133+ ) ;
134+
135+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
136+ } ) ;
137+
138+ it ( 'should render correctly when as is textarea and name is array' , ( ) => {
139+ const control = reconfigureControl ( ) ;
140+ const { result } = renderHook ( ( ) =>
141+ useTypedController < FormValues > ( {
142+ control,
143+ } ) ,
144+ ) ;
145+ const TypedController = result . current ;
146+
147+ const { asFragment } = render (
148+ < TypedController
149+ as = "textarea"
150+ name = { [ 'nested' , 'object' , 'test' ] }
151+ defaultValue = "test"
152+ /> ,
153+ ) ;
154+
155+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
156+ } ) ;
157+
158+ it ( 'should render correctly when name is array and render is input' , ( ) => {
159+ const control = reconfigureControl ( ) ;
160+ const { result } = renderHook ( ( ) =>
161+ useTypedController < FormValues > ( {
162+ control,
163+ } ) ,
164+ ) ;
165+ const TypedController = result . current ;
166+
167+ const { asFragment } = render (
168+ < TypedController
169+ name = { [ 'nested' , 'array' , 0 , 'test' ] }
170+ defaultValue = "test"
171+ render = { ( props ) => < input { ...props } /> }
172+ /> ,
173+ ) ;
174+
175+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
176+ } ) ;
104177
105178 it ( 'should render correctly when name is string' , ( ) => {
179+ const control = reconfigureControl ( ) ;
180+ const { result } = renderHook ( ( ) =>
181+ useTypedController < FormValues > ( {
182+ control,
183+ } ) ,
184+ ) ;
185+ const TypedController = result . current ;
186+
106187 render (
107188 < TypedController
108- name = "test "
189+ name = "flat "
109190 defaultValue = ""
110191 render = { ( props ) => < input { ...props } /> }
111192 /> ,
112193 ) ;
194+
113195 expect ( control . register ) . toHaveBeenCalledWith (
114196 {
115197 focus : undefined ,
116- name : 'test ' ,
198+ name : 'flat ' ,
117199 } ,
118200 undefined ,
119201 ) ;
120202 } ) ;
121203
122- it ( 'should render correctly when name is array' , ( ) => {
204+ it ( 'should format name correctly when name is array (dot syntax)' , ( ) => {
205+ const control = reconfigureControl ( ) ;
206+ const { result } = renderHook ( ( ) =>
207+ useTypedController < FormValues > ( {
208+ control,
209+ } ) ,
210+ ) ;
211+ const TypedController = result . current ;
212+
123213 render (
124214 < TypedController
125- name = { [ 'test1 ' , 0 , 'test2 ' ] }
215+ name = { [ 'nested ' , 'object' , 'test ' ] }
126216 defaultValue = ""
127217 render = { ( props ) => < input { ...props } /> }
128218 /> ,
129219 ) ;
220+
221+ expect ( control . register ) . toHaveBeenCalledWith (
222+ {
223+ focus : undefined ,
224+ name : 'nested.object.test' ,
225+ } ,
226+ undefined ,
227+ ) ;
228+ } ) ;
229+
230+ it ( 'should format name correctly when name is array (dot-bracket syntax)' , ( ) => {
231+ const control = reconfigureControl ( ) ;
232+ const { result } = renderHook ( ( ) =>
233+ useTypedController < FormValues > ( {
234+ control,
235+ } ) ,
236+ ) ;
237+ const TypedController = result . current ;
238+
239+ render (
240+ < TypedController
241+ name = { [ 'nested' , 'array' , 0 , 'test' ] }
242+ defaultValue = ""
243+ render = { ( props ) => < input { ...props } /> }
244+ /> ,
245+ ) ;
246+
130247 expect ( control . register ) . toHaveBeenCalledWith (
131248 {
132249 focus : undefined ,
133- name : 'test1 [0].test2 ' ,
250+ name : 'nested.array [0].test ' ,
134251 } ,
135252 undefined ,
136253 ) ;
0 commit comments