Skip to content

Commit 0077ce1

Browse files
joanaJoanata
authored andcommitted
Update hooks test
1 parent d0ad045 commit 0077ce1

File tree

3 files changed

+962
-4
lines changed

3 files changed

+962
-4
lines changed

test/src/hooks/use-field-state.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ describe('useFieldState', () => {
3232
expect(result.current.value).toBe('bar');
3333
});
3434

35+
it('should return the nested field value', () => {
36+
const state = {
37+
fields: {
38+
values: {
39+
foo: {
40+
bar: 'baz'
41+
}
42+
}
43+
}
44+
};
45+
46+
const Wrapper = ({ children }) => (
47+
<FormStateContext.Provider value={state}>
48+
{children}
49+
</FormStateContext.Provider>
50+
);
51+
52+
const { result } = renderHook(() => useFieldState('foo.bar'), {
53+
wrapper: Wrapper
54+
});
55+
56+
expect(result.current.value).toBe('baz');
57+
});
58+
3559
it('should return the field error', () => {
3660
const state = {
3761
fields: {
@@ -52,6 +76,30 @@ describe('useFieldState', () => {
5276
expect(result.current.error).toBe('bar');
5377
});
5478

79+
it('should return the nested field error', () => {
80+
const state = {
81+
fields: {
82+
errors: {
83+
foo: {
84+
bar: 'baz'
85+
}
86+
}
87+
}
88+
};
89+
90+
const Wrapper = ({ children }) => (
91+
<FormStateContext.Provider value={state}>
92+
{children}
93+
</FormStateContext.Provider>
94+
);
95+
96+
const { result } = renderHook(() => useFieldState('foo.bar'), {
97+
wrapper: Wrapper
98+
});
99+
100+
expect(result.current.error).toBe('baz');
101+
});
102+
55103
it('should return the field meta state', () => {
56104
const state = {
57105
fields: {
@@ -71,4 +119,28 @@ describe('useFieldState', () => {
71119

72120
expect(result.current.meta).toBe('bar');
73121
});
122+
123+
it('should return the nested field meta state', () => {
124+
const state = {
125+
fields: {
126+
meta: {
127+
foo: {
128+
bar: 'baz'
129+
}
130+
}
131+
}
132+
};
133+
134+
const Wrapper = ({ children }) => (
135+
<FormStateContext.Provider value={state}>
136+
{children}
137+
</FormStateContext.Provider>
138+
);
139+
140+
const { result } = renderHook(() => useFieldState('foo.bar'), {
141+
wrapper: Wrapper
142+
});
143+
144+
expect(result.current.meta).toBe('baz');
145+
});
74146
});

test/src/hooks/use-field.test.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)