piral-forms 1.11.0-beta.fef8836 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/useForm.test.ts +403 -451
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-forms",
|
|
3
|
-
"version": "1.11.0
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Plugin for providing advanced form support in Piral.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral",
|
|
@@ -52,8 +52,8 @@
|
|
|
52
52
|
"@types/history": "^4.7.8",
|
|
53
53
|
"@types/react": "^18.0.0",
|
|
54
54
|
"@types/react-router-dom": "^5.1.6",
|
|
55
|
-
"piral-core": "1.11.0
|
|
55
|
+
"piral-core": "^1.11.0",
|
|
56
56
|
"react": "^18.0.0"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "7bb5f7938f1a3211aa3e81ac5b7101b7605140d6"
|
|
59
59
|
}
|
package/src/useForm.test.ts
CHANGED
|
@@ -30,494 +30,446 @@ describe('Form Hook Module', () => {
|
|
|
30
30
|
setStateFake.mockReset();
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
-
it(
|
|
34
|
-
|
|
35
|
-
async () => {
|
|
36
|
-
const { useForm } = await import('./useForm');
|
|
33
|
+
it('Returns the current data and not changed initially', testOptions, async () => {
|
|
34
|
+
const { useForm } = await import('./useForm');
|
|
37
35
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
},
|
|
36
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
37
|
+
select({
|
|
38
|
+
forms: {
|
|
39
|
+
foo: {
|
|
40
|
+
changed: false,
|
|
41
|
+
currentData: {},
|
|
42
|
+
initialData: {},
|
|
43
|
+
submitting: false,
|
|
44
|
+
error: undefined,
|
|
48
45
|
},
|
|
49
|
-
}),
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
const options = {
|
|
53
|
-
wait: false,
|
|
54
|
-
silent: false,
|
|
55
|
-
message: '',
|
|
56
|
-
onSubmit() {
|
|
57
|
-
return Promise.resolve();
|
|
58
46
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
};
|
|
62
|
-
const { changed, submitting, formData } = useForm({}, undefined as any, options, 'foo');
|
|
63
|
-
expect(changed).toBeFalsy();
|
|
64
|
-
expect(submitting).toBeFalsy();
|
|
65
|
-
expect(formData).toEqual({});
|
|
66
|
-
expect(setStateFake.mock.calls[0][0].length).toBe(3);
|
|
67
|
-
},
|
|
68
|
-
testOptions,
|
|
69
|
-
);
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
70
49
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
50
|
+
const options = {
|
|
51
|
+
wait: false,
|
|
52
|
+
silent: false,
|
|
53
|
+
message: '',
|
|
54
|
+
onSubmit() {
|
|
55
|
+
return Promise.resolve();
|
|
56
|
+
},
|
|
57
|
+
onChange: undefined,
|
|
58
|
+
emptyData: {},
|
|
59
|
+
};
|
|
60
|
+
const { changed, submitting, formData } = useForm({}, undefined as any, options, 'foo');
|
|
61
|
+
expect(changed).toBeFalsy();
|
|
62
|
+
expect(submitting).toBeFalsy();
|
|
63
|
+
expect(formData).toEqual({});
|
|
64
|
+
expect(setStateFake.mock.calls[0][0].length).toBe(3);
|
|
65
|
+
});
|
|
75
66
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
forms: {},
|
|
79
|
-
}),
|
|
80
|
-
);
|
|
81
|
-
const options = {
|
|
82
|
-
wait: false,
|
|
83
|
-
silent: false,
|
|
84
|
-
message: '',
|
|
85
|
-
onSubmit() {
|
|
86
|
-
return Promise.resolve();
|
|
87
|
-
},
|
|
88
|
-
onChange: undefined,
|
|
89
|
-
emptyData: {},
|
|
90
|
-
};
|
|
91
|
-
const { changed, submitting, formData } = useForm({}, undefined as any, options);
|
|
92
|
-
expect(changed).toBeFalsy();
|
|
93
|
-
expect(submitting).toBeFalsy();
|
|
94
|
-
expect(formData).toEqual({});
|
|
95
|
-
expect(setStateFake.mock.calls[0][0].length).toBe(36);
|
|
96
|
-
},
|
|
97
|
-
testOptions,
|
|
98
|
-
);
|
|
67
|
+
it('Generates a new id if the old one is not provided', testOptions, async () => {
|
|
68
|
+
const { useForm } = await import('./useForm');
|
|
99
69
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
70
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
71
|
+
select({
|
|
72
|
+
forms: {},
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
const options = {
|
|
76
|
+
wait: false,
|
|
77
|
+
silent: false,
|
|
78
|
+
message: '',
|
|
79
|
+
onSubmit() {
|
|
80
|
+
return Promise.resolve();
|
|
81
|
+
},
|
|
82
|
+
onChange: undefined,
|
|
83
|
+
emptyData: {},
|
|
84
|
+
};
|
|
85
|
+
const { changed, submitting, formData } = useForm({}, undefined as any, options);
|
|
86
|
+
expect(changed).toBeFalsy();
|
|
87
|
+
expect(submitting).toBeFalsy();
|
|
88
|
+
expect(formData).toEqual({});
|
|
89
|
+
expect(setStateFake.mock.calls[0][0].length).toBe(36);
|
|
90
|
+
});
|
|
105
91
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}),
|
|
110
|
-
);
|
|
111
|
-
const options = {
|
|
112
|
-
wait: false,
|
|
113
|
-
silent: false,
|
|
114
|
-
message: '',
|
|
115
|
-
onSubmit,
|
|
116
|
-
onChange: undefined,
|
|
117
|
-
emptyData: {},
|
|
118
|
-
};
|
|
119
|
-
const { changed, submitting, formData, submit } = useForm({}, undefined as any, options);
|
|
120
|
-
submit();
|
|
121
|
-
expect(changed).toBeFalsy();
|
|
122
|
-
expect(submitting).toBeFalsy();
|
|
123
|
-
expect(onSubmit).not.toHaveBeenCalled();
|
|
124
|
-
expect(formData).toEqual({});
|
|
125
|
-
expect(setStateFake.mock.calls[0][0].length).toBe(36);
|
|
126
|
-
},
|
|
127
|
-
testOptions,
|
|
128
|
-
);
|
|
92
|
+
it('Submit with no changed data does nothing', testOptions, async () => {
|
|
93
|
+
const { useForm } = await import('./useForm');
|
|
94
|
+
const onSubmit = vitest.fn(() => Promise.resolve());
|
|
129
95
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
96
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
97
|
+
select({
|
|
98
|
+
forms: {},
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
const options = {
|
|
102
|
+
wait: false,
|
|
103
|
+
silent: false,
|
|
104
|
+
message: '',
|
|
105
|
+
onSubmit,
|
|
106
|
+
onChange: undefined,
|
|
107
|
+
emptyData: {},
|
|
108
|
+
};
|
|
109
|
+
const { changed, submitting, formData, submit } = useForm({}, undefined as any, options);
|
|
110
|
+
submit();
|
|
111
|
+
expect(changed).toBeFalsy();
|
|
112
|
+
expect(submitting).toBeFalsy();
|
|
113
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
114
|
+
expect(formData).toEqual({});
|
|
115
|
+
expect(setStateFake.mock.calls[0][0].length).toBe(36);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('Submit with no changed data does something with allowSubmitUnchanged is on', testOptions, async () => {
|
|
119
|
+
const { useForm } = await import('./useForm');
|
|
120
|
+
const onSubmit = vitest.fn(() => Promise.resolve());
|
|
135
121
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
testOptions,
|
|
159
|
-
);
|
|
122
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
123
|
+
select({
|
|
124
|
+
forms: {},
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
const options = {
|
|
128
|
+
allowSubmitUnchanged: true,
|
|
129
|
+
wait: false,
|
|
130
|
+
silent: false,
|
|
131
|
+
message: '',
|
|
132
|
+
onSubmit,
|
|
133
|
+
onChange: undefined,
|
|
134
|
+
emptyData: {},
|
|
135
|
+
};
|
|
136
|
+
const { changed, submitting, formData, submit } = useForm({}, undefined as any, options);
|
|
137
|
+
submit();
|
|
138
|
+
expect(changed).toBeFalsy();
|
|
139
|
+
expect(submitting).toBeFalsy();
|
|
140
|
+
expect(onSubmit).toHaveBeenCalled();
|
|
141
|
+
expect(formData).toEqual({});
|
|
142
|
+
expect(setStateFake.mock.calls[0][0].length).toBe(36);
|
|
143
|
+
});
|
|
160
144
|
|
|
161
|
-
it(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const { useForm } = await import('./useForm');
|
|
165
|
-
const onSubmit = vitest.fn(() => Promise.resolve());
|
|
145
|
+
it('Submit with changed data submits successfully', testOptions, async () => {
|
|
146
|
+
const { useForm } = await import('./useForm');
|
|
147
|
+
const onSubmit = vitest.fn(() => Promise.resolve());
|
|
166
148
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
},
|
|
178
|
-
submitting: false,
|
|
179
|
-
error: undefined,
|
|
149
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
150
|
+
select({
|
|
151
|
+
forms: {
|
|
152
|
+
foo: {
|
|
153
|
+
changed: true,
|
|
154
|
+
currentData: {
|
|
155
|
+
a: 'foo',
|
|
156
|
+
},
|
|
157
|
+
initialData: {
|
|
158
|
+
a: '',
|
|
180
159
|
},
|
|
160
|
+
submitting: false,
|
|
161
|
+
error: undefined,
|
|
181
162
|
},
|
|
182
|
-
}
|
|
183
|
-
)
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
);
|
|
163
|
+
},
|
|
164
|
+
}),
|
|
165
|
+
);
|
|
166
|
+
const options = {
|
|
167
|
+
wait: false,
|
|
168
|
+
silent: false,
|
|
169
|
+
message: '',
|
|
170
|
+
onSubmit,
|
|
171
|
+
onChange: undefined,
|
|
172
|
+
emptyData: {},
|
|
173
|
+
};
|
|
174
|
+
const { changed, formData, submit } = useForm({}, undefined as any, options, 'foo');
|
|
175
|
+
const preventDefault = vitest.fn();
|
|
176
|
+
(submit as any)({ preventDefault });
|
|
177
|
+
expect(changed).toBeTruthy();
|
|
178
|
+
expect(preventDefault).toBeCalled();
|
|
179
|
+
expect(onSubmit).toHaveBeenCalledWith({
|
|
180
|
+
a: 'foo',
|
|
181
|
+
});
|
|
182
|
+
expect(formData).toEqual({
|
|
183
|
+
a: 'foo',
|
|
184
|
+
});
|
|
185
|
+
});
|
|
206
186
|
|
|
207
|
-
it(
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
const { useForm } = await import('./useForm');
|
|
211
|
-
const onSubmit = vitest.fn(() => Promise.reject('My error'));
|
|
187
|
+
it('Submit with changed data running into an error', testOptions, async () => {
|
|
188
|
+
const { useForm } = await import('./useForm');
|
|
189
|
+
const onSubmit = vitest.fn(() => Promise.reject('My error'));
|
|
212
190
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
},
|
|
224
|
-
submitting: false,
|
|
225
|
-
error: undefined,
|
|
191
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
192
|
+
select({
|
|
193
|
+
forms: {
|
|
194
|
+
foo: {
|
|
195
|
+
changed: true,
|
|
196
|
+
currentData: {
|
|
197
|
+
a: 'foo',
|
|
198
|
+
},
|
|
199
|
+
initialData: {
|
|
200
|
+
a: '',
|
|
226
201
|
},
|
|
202
|
+
submitting: false,
|
|
203
|
+
error: undefined,
|
|
227
204
|
},
|
|
228
|
-
}
|
|
229
|
-
)
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
);
|
|
205
|
+
},
|
|
206
|
+
}),
|
|
207
|
+
);
|
|
208
|
+
const options = {
|
|
209
|
+
wait: false,
|
|
210
|
+
silent: false,
|
|
211
|
+
message: '',
|
|
212
|
+
onSubmit,
|
|
213
|
+
onChange: undefined,
|
|
214
|
+
emptyData: {},
|
|
215
|
+
};
|
|
216
|
+
const { changed, formData, submit } = useForm({}, undefined as any, options, 'foo');
|
|
217
|
+
submit();
|
|
218
|
+
expect(changed).toBeTruthy();
|
|
219
|
+
expect(onSubmit).toHaveBeenCalledWith({
|
|
220
|
+
a: 'foo',
|
|
221
|
+
});
|
|
222
|
+
expect(formData).toEqual({
|
|
223
|
+
a: 'foo',
|
|
224
|
+
});
|
|
225
|
+
});
|
|
250
226
|
|
|
251
|
-
it(
|
|
252
|
-
|
|
253
|
-
async () => {
|
|
254
|
-
const { useForm } = await import('./useForm');
|
|
227
|
+
it('Sets new data on changeForm', testOptions, async () => {
|
|
228
|
+
const { useForm } = await import('./useForm');
|
|
255
229
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
},
|
|
294
|
-
changed: true,
|
|
295
|
-
error: undefined,
|
|
230
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
231
|
+
select({
|
|
232
|
+
forms: {},
|
|
233
|
+
}),
|
|
234
|
+
);
|
|
235
|
+
const options = {
|
|
236
|
+
wait: false,
|
|
237
|
+
silent: false,
|
|
238
|
+
message: '',
|
|
239
|
+
onSubmit() {
|
|
240
|
+
return Promise.resolve();
|
|
241
|
+
},
|
|
242
|
+
onChange: undefined,
|
|
243
|
+
emptyData: {},
|
|
244
|
+
};
|
|
245
|
+
const { changeForm } = useForm({}, undefined as any, options, 'id');
|
|
246
|
+
changeForm({
|
|
247
|
+
target: {
|
|
248
|
+
name: 'foo',
|
|
249
|
+
value: 'bar',
|
|
250
|
+
},
|
|
251
|
+
} as any);
|
|
252
|
+
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
253
|
+
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
254
|
+
2,
|
|
255
|
+
'id',
|
|
256
|
+
{
|
|
257
|
+
active: true,
|
|
258
|
+
currentData: {},
|
|
259
|
+
initialData: {},
|
|
260
|
+
changed: false,
|
|
261
|
+
submitting: false,
|
|
262
|
+
error: undefined,
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
currentData: {
|
|
266
|
+
foo: 'bar',
|
|
296
267
|
},
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
268
|
+
changed: true,
|
|
269
|
+
error: undefined,
|
|
270
|
+
},
|
|
271
|
+
);
|
|
272
|
+
});
|
|
301
273
|
|
|
302
|
-
it(
|
|
303
|
-
|
|
304
|
-
async () => {
|
|
305
|
-
const { useForm } = await import('./useForm');
|
|
274
|
+
it('Sets new data on setFormData', testOptions, async () => {
|
|
275
|
+
const { useForm } = await import('./useForm');
|
|
306
276
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
277
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
278
|
+
select({
|
|
279
|
+
forms: {},
|
|
280
|
+
}),
|
|
281
|
+
);
|
|
282
|
+
const options = {
|
|
283
|
+
wait: false,
|
|
284
|
+
silent: false,
|
|
285
|
+
message: '',
|
|
286
|
+
onSubmit() {
|
|
287
|
+
return Promise.resolve();
|
|
288
|
+
},
|
|
289
|
+
onChange: undefined,
|
|
290
|
+
emptyData: {},
|
|
291
|
+
};
|
|
292
|
+
const { setFormData } = useForm({}, undefined as any, options, 'id');
|
|
293
|
+
setFormData({
|
|
294
|
+
foo: 'a',
|
|
295
|
+
bar: 'b',
|
|
296
|
+
} as any);
|
|
297
|
+
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
298
|
+
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
299
|
+
2,
|
|
300
|
+
'id',
|
|
301
|
+
{
|
|
302
|
+
active: true,
|
|
303
|
+
currentData: {},
|
|
304
|
+
initialData: {},
|
|
305
|
+
changed: false,
|
|
306
|
+
submitting: false,
|
|
307
|
+
error: undefined,
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
currentData: {
|
|
311
|
+
foo: 'a',
|
|
312
|
+
bar: 'b',
|
|
338
313
|
},
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
changed: true,
|
|
345
|
-
error: undefined,
|
|
346
|
-
},
|
|
347
|
-
);
|
|
348
|
-
},
|
|
349
|
-
testOptions,
|
|
350
|
-
);
|
|
314
|
+
changed: true,
|
|
315
|
+
error: undefined,
|
|
316
|
+
},
|
|
317
|
+
);
|
|
318
|
+
});
|
|
351
319
|
|
|
352
|
-
it(
|
|
353
|
-
|
|
354
|
-
async () => {
|
|
355
|
-
const { useForm } = await import('./useForm');
|
|
320
|
+
it('Resets changes to initial data', testOptions, async () => {
|
|
321
|
+
const { useForm } = await import('./useForm');
|
|
356
322
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
},
|
|
366
|
-
initialData: {
|
|
367
|
-
a: '',
|
|
368
|
-
},
|
|
369
|
-
submitting: false,
|
|
370
|
-
error: undefined,
|
|
323
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
324
|
+
select({
|
|
325
|
+
forms: {
|
|
326
|
+
id: {
|
|
327
|
+
active: true,
|
|
328
|
+
changed: true,
|
|
329
|
+
currentData: {
|
|
330
|
+
a: 'foo',
|
|
371
331
|
},
|
|
332
|
+
initialData: {
|
|
333
|
+
a: '',
|
|
334
|
+
},
|
|
335
|
+
submitting: false,
|
|
336
|
+
error: undefined,
|
|
372
337
|
},
|
|
373
|
-
}),
|
|
374
|
-
);
|
|
375
|
-
const options = {
|
|
376
|
-
wait: false,
|
|
377
|
-
silent: false,
|
|
378
|
-
message: '',
|
|
379
|
-
onSubmit() {
|
|
380
|
-
return Promise.resolve();
|
|
381
338
|
},
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
339
|
+
}),
|
|
340
|
+
);
|
|
341
|
+
const options = {
|
|
342
|
+
wait: false,
|
|
343
|
+
silent: false,
|
|
344
|
+
message: '',
|
|
345
|
+
onSubmit() {
|
|
346
|
+
return Promise.resolve();
|
|
347
|
+
},
|
|
348
|
+
onChange: undefined,
|
|
349
|
+
emptyData: {},
|
|
350
|
+
};
|
|
351
|
+
const { reset } = useForm({}, undefined as any, options, 'id');
|
|
352
|
+
reset();
|
|
353
|
+
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
354
|
+
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
355
|
+
2,
|
|
356
|
+
'id',
|
|
357
|
+
{
|
|
358
|
+
active: true,
|
|
359
|
+
currentData: {
|
|
360
|
+
a: 'foo',
|
|
402
361
|
},
|
|
403
|
-
{
|
|
404
|
-
|
|
405
|
-
a: '',
|
|
406
|
-
},
|
|
407
|
-
changed: false,
|
|
408
|
-
error: undefined,
|
|
362
|
+
initialData: {
|
|
363
|
+
a: '',
|
|
409
364
|
},
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
365
|
+
changed: true,
|
|
366
|
+
submitting: false,
|
|
367
|
+
error: undefined,
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
currentData: {
|
|
371
|
+
a: '',
|
|
372
|
+
},
|
|
373
|
+
changed: false,
|
|
374
|
+
error: undefined,
|
|
375
|
+
},
|
|
376
|
+
);
|
|
377
|
+
});
|
|
414
378
|
|
|
415
|
-
it(
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
const { useForm } = await import('./useForm');
|
|
419
|
-
const onChange = vitest.fn((data) => Promise.resolve(data));
|
|
379
|
+
it('onChange should be triggered with full data set', testOptions, async () => {
|
|
380
|
+
const { useForm } = await import('./useForm');
|
|
381
|
+
const onChange = vitest.fn((data) => Promise.resolve(data));
|
|
420
382
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
},
|
|
433
|
-
submitting: false,
|
|
434
|
-
error: undefined,
|
|
383
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
384
|
+
select({
|
|
385
|
+
forms: {
|
|
386
|
+
id: {
|
|
387
|
+
active: true,
|
|
388
|
+
changed: true,
|
|
389
|
+
currentData: {
|
|
390
|
+
a: 'foo',
|
|
391
|
+
},
|
|
392
|
+
initialData: {
|
|
393
|
+
a: '',
|
|
435
394
|
},
|
|
395
|
+
submitting: false,
|
|
396
|
+
error: undefined,
|
|
436
397
|
},
|
|
437
|
-
}),
|
|
438
|
-
);
|
|
439
|
-
const options = {
|
|
440
|
-
wait: false,
|
|
441
|
-
silent: false,
|
|
442
|
-
message: '',
|
|
443
|
-
onChange,
|
|
444
|
-
onSubmit() {
|
|
445
|
-
return Promise.resolve();
|
|
446
398
|
},
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
399
|
+
}),
|
|
400
|
+
);
|
|
401
|
+
const options = {
|
|
402
|
+
wait: false,
|
|
403
|
+
silent: false,
|
|
404
|
+
message: '',
|
|
405
|
+
onChange,
|
|
406
|
+
onSubmit() {
|
|
407
|
+
return Promise.resolve();
|
|
408
|
+
},
|
|
409
|
+
emptyData: {},
|
|
410
|
+
};
|
|
411
|
+
const { setFormData } = useForm({}, undefined as any, options, 'id');
|
|
412
|
+
setFormData({ a: 'b' });
|
|
413
|
+
expect(onChange).toHaveBeenCalledWith({ a: 'b' });
|
|
414
|
+
});
|
|
455
415
|
|
|
456
|
-
it(
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
const { useForm } = await import('./useForm');
|
|
460
|
-
const onChange = vitest.fn((data) => Promise.reject('my error'));
|
|
416
|
+
it('onChange which fails should be handled gracefully', testOptions, async () => {
|
|
417
|
+
const { useForm } = await import('./useForm');
|
|
418
|
+
const onChange = vitest.fn((data) => Promise.reject('my error'));
|
|
461
419
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
testOptions,
|
|
482
|
-
);
|
|
420
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
421
|
+
select({
|
|
422
|
+
forms: {},
|
|
423
|
+
}),
|
|
424
|
+
);
|
|
425
|
+
const options = {
|
|
426
|
+
wait: false,
|
|
427
|
+
silent: false,
|
|
428
|
+
message: '',
|
|
429
|
+
onChange,
|
|
430
|
+
onSubmit() {
|
|
431
|
+
return Promise.resolve();
|
|
432
|
+
},
|
|
433
|
+
emptyData: {},
|
|
434
|
+
};
|
|
435
|
+
const { setFormData } = useForm({}, undefined as any, options);
|
|
436
|
+
setFormData({ a: 'b' });
|
|
437
|
+
expect(onChange).toHaveBeenCalledWith({ a: 'b' });
|
|
438
|
+
});
|
|
483
439
|
|
|
484
|
-
it(
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
const { useForm } = await import('./useForm');
|
|
488
|
-
(React as any).useEffect = vitest.fn((cb) => cb()());
|
|
440
|
+
it('cleanup sets active to false', testOptions, async () => {
|
|
441
|
+
const { useForm } = await import('./useForm');
|
|
442
|
+
(React as any).useEffect = vitest.fn((cb) => cb()());
|
|
489
443
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
testOptions,
|
|
522
|
-
);
|
|
444
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
445
|
+
select({
|
|
446
|
+
forms: {},
|
|
447
|
+
}),
|
|
448
|
+
);
|
|
449
|
+
const options = {
|
|
450
|
+
wait: false,
|
|
451
|
+
silent: false,
|
|
452
|
+
message: '',
|
|
453
|
+
onChange: undefined,
|
|
454
|
+
onSubmit() {
|
|
455
|
+
return Promise.resolve();
|
|
456
|
+
},
|
|
457
|
+
emptyData: {},
|
|
458
|
+
};
|
|
459
|
+
useForm({}, undefined as any, options, 'id');
|
|
460
|
+
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
461
|
+
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
462
|
+
2,
|
|
463
|
+
'id',
|
|
464
|
+
{
|
|
465
|
+
active: true,
|
|
466
|
+
changed: false,
|
|
467
|
+
currentData: {},
|
|
468
|
+
error: undefined,
|
|
469
|
+
initialData: {},
|
|
470
|
+
submitting: false,
|
|
471
|
+
},
|
|
472
|
+
{ active: false },
|
|
473
|
+
);
|
|
474
|
+
});
|
|
523
475
|
});
|