@@ -45,6 +45,30 @@ describe('useField', () => {
4545 expect ( result . current . value ) . toBe ( 'bar' ) ;
4646 } ) ;
4747
48+ it ( 'should return the nested field value' , ( ) => {
49+ const state = {
50+ fields : {
51+ values : {
52+ foo : {
53+ bar : 'baz'
54+ }
55+ }
56+ }
57+ } ;
58+
59+ const Wrapper = ( { children } ) => (
60+ < FieldActionsContext . Provider value = { actions } >
61+ < FormStateContext . Provider value = { state } >
62+ { children }
63+ </ FormStateContext . Provider >
64+ </ FieldActionsContext . Provider >
65+ ) ;
66+
67+ const { result } = renderHook ( ( ) => useField ( 'foo.bar' ) , { wrapper : Wrapper } ) ;
68+
69+ expect ( result . current . value ) . toBe ( 'baz' ) ;
70+ } ) ;
71+
4872 it ( 'should return the field error' , ( ) => {
4973 const state = {
5074 fields : {
@@ -65,6 +89,32 @@ describe('useField', () => {
6589 expect ( result . current . error ) . toBe ( 'bar' ) ;
6690 } ) ;
6791
92+ it ( 'should return the nested field error' , ( ) => {
93+ const state = {
94+ fields : {
95+ errors : {
96+ foo : {
97+ bar : 'baz'
98+ }
99+ }
100+ }
101+ } ;
102+
103+ const Wrapper = ( { children } ) => (
104+ < FieldActionsContext . Provider value = { actions } >
105+ < FormStateContext . Provider value = { state } >
106+ { children }
107+ </ FormStateContext . Provider >
108+ </ FieldActionsContext . Provider >
109+ ) ;
110+
111+ const { result } = renderHook ( ( ) => useField ( 'foo.bar' ) , {
112+ wrapper : Wrapper
113+ } ) ;
114+
115+ expect ( result . current . error ) . toBe ( 'baz' ) ;
116+ } ) ;
117+
68118 it ( 'should return the field meta state' , ( ) => {
69119 const state = {
70120 fields : {
@@ -85,6 +135,30 @@ describe('useField', () => {
85135 expect ( result . current . meta ) . toBe ( 'bar' ) ;
86136 } ) ;
87137
138+ it ( 'should return the nested field meta state' , ( ) => {
139+ const state = {
140+ fields : {
141+ meta : {
142+ foo : {
143+ bar : 'baz'
144+ }
145+ }
146+ }
147+ } ;
148+
149+ const Wrapper = ( { children } ) => (
150+ < FieldActionsContext . Provider value = { actions } >
151+ < FormStateContext . Provider value = { state } >
152+ { children }
153+ </ FormStateContext . Provider >
154+ </ FieldActionsContext . Provider >
155+ ) ;
156+
157+ const { result } = renderHook ( ( ) => useField ( 'foo.bar' ) , { wrapper : Wrapper } ) ;
158+
159+ expect ( result . current . meta ) . toBe ( 'baz' ) ;
160+ } ) ;
161+
88162 it ( 'should return an `onBlur` action that calls the `blurField` form action with the field name' , ( ) => {
89163 const Wrapper = ( { children } ) => (
90164 < FieldActionsContext . Provider value = { actions } >
@@ -102,6 +176,25 @@ describe('useField', () => {
102176 expect ( actions . blurField ) . toHaveBeenCalledWith ( 'foo' ) ;
103177 } ) ;
104178
179+ it ( 'should return an `onBlur` action that calls the `blurField` form action with the nested field name' , ( ) => {
180+ const Wrapper = ( { children } ) => (
181+ < FieldActionsContext . Provider value = { actions } >
182+ < FormStateContext . Provider value = { { } } >
183+ { children }
184+ </ FormStateContext . Provider >
185+ </ FieldActionsContext . Provider >
186+ ) ;
187+
188+ const { result } = renderHook ( ( ) => useField ( 'foo.bar' ) , {
189+ wrapper : Wrapper
190+ } ) ;
191+
192+ result . current . onBlur ( ) ;
193+
194+ expect ( actions . blurField ) . toHaveBeenCalledTimes ( 1 ) ;
195+ expect ( actions . blurField ) . toHaveBeenCalledWith ( 'foo.bar' ) ;
196+ } ) ;
197+
105198 it ( 'should return an `onFocus` action that calls the `focusField` form action with the field name' , ( ) => {
106199 const Wrapper = ( { children } ) => (
107200 < FieldActionsContext . Provider value = { actions } >
@@ -119,6 +212,25 @@ describe('useField', () => {
119212 expect ( actions . focusField ) . toHaveBeenCalledWith ( 'foo' ) ;
120213 } ) ;
121214
215+ it ( 'should return an `onFocus` action that calls the `focusField` form action with the nested field name' , ( ) => {
216+ const Wrapper = ( { children } ) => (
217+ < FieldActionsContext . Provider value = { actions } >
218+ < FormStateContext . Provider value = { { } } >
219+ { children }
220+ </ FormStateContext . Provider >
221+ </ FieldActionsContext . Provider >
222+ ) ;
223+
224+ const { result } = renderHook ( ( ) => useField ( 'foo.bar' ) , {
225+ wrapper : Wrapper
226+ } ) ;
227+
228+ result . current . onFocus ( ) ;
229+
230+ expect ( actions . focusField ) . toHaveBeenCalledTimes ( 1 ) ;
231+ expect ( actions . focusField ) . toHaveBeenCalledWith ( 'foo.bar' ) ;
232+ } ) ;
233+
122234 it ( 'should return an `onChange` action that calls the `setFieldValue` form action with the field name and the provided value' , ( ) => {
123235 const Wrapper = ( { children } ) => (
124236 < FieldActionsContext . Provider value = { actions } >
@@ -135,4 +247,23 @@ describe('useField', () => {
135247 expect ( actions . setFieldValue ) . toHaveBeenCalledTimes ( 1 ) ;
136248 expect ( actions . setFieldValue ) . toHaveBeenCalledWith ( 'foo' , 'bar' ) ;
137249 } ) ;
250+
251+ it ( 'should return an `onChange` action that calls the `setFieldValue` form action with the nested field name and the provided value' , ( ) => {
252+ const Wrapper = ( { children } ) => (
253+ < FieldActionsContext . Provider value = { actions } >
254+ < FormStateContext . Provider value = { { } } >
255+ { children }
256+ </ FormStateContext . Provider >
257+ </ FieldActionsContext . Provider >
258+ ) ;
259+
260+ const { result } = renderHook ( ( ) => useField ( 'foo.bar' ) , {
261+ wrapper : Wrapper
262+ } ) ;
263+
264+ result . current . onChange ( 'baz' ) ;
265+
266+ expect ( actions . setFieldValue ) . toHaveBeenCalledTimes ( 1 ) ;
267+ expect ( actions . setFieldValue ) . toHaveBeenCalledWith ( 'foo.bar' , 'baz' ) ;
268+ } ) ;
138269} ) ;
0 commit comments