@formvk/core 0.1.0 → 1.0.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +63 -0
  3. package/package.json +8 -4
  4. package/src/__tests__/array.spec.ts +0 -168
  5. package/src/__tests__/effects.spec.ts +0 -595
  6. package/src/__tests__/externals.spec.ts +0 -101
  7. package/src/__tests__/field.spec.ts +0 -572
  8. package/src/__tests__/form.spec.ts +0 -720
  9. package/src/__tests__/graph.spec.ts +0 -56
  10. package/src/__tests__/heart.spec.ts +0 -60
  11. package/src/__tests__/internals.spec.ts +0 -138
  12. package/src/__tests__/lifecycle.spec.ts +0 -51
  13. package/src/__tests__/object.spec.ts +0 -107
  14. package/src/__tests__/query.spec.ts +0 -100
  15. package/src/__tests__/shared.ts +0 -10
  16. package/src/__tests__/typed.spec.ts +0 -52
  17. package/src/__tests__/void.spec.ts +0 -163
  18. package/src/effects/index.ts +0 -2
  19. package/src/effects/onFieldEffects.ts +0 -97
  20. package/src/effects/onFormEffects.ts +0 -48
  21. package/src/global.d.ts +0 -8
  22. package/src/index.ts +0 -4
  23. package/src/models/ArrayField.ts +0 -133
  24. package/src/models/BaseField.ts +0 -359
  25. package/src/models/Field.ts +0 -450
  26. package/src/models/Form.ts +0 -586
  27. package/src/models/Graph.ts +0 -56
  28. package/src/models/Heart.ts +0 -75
  29. package/src/models/LifeCycle.ts +0 -42
  30. package/src/models/ObjectField.ts +0 -48
  31. package/src/models/Query.ts +0 -97
  32. package/src/models/VoidField.ts +0 -64
  33. package/src/models/index.ts +0 -9
  34. package/src/models/types.ts +0 -9
  35. package/src/shared/checkers.ts +0 -85
  36. package/src/shared/constants.ts +0 -60
  37. package/src/shared/effective.ts +0 -69
  38. package/src/shared/externals.ts +0 -65
  39. package/src/shared/internals.ts +0 -965
  40. package/src/types.ts +0 -401
  41. package/tsconfig.build.json +0 -9
  42. package/tsconfig.json +0 -4
  43. package/vite.config.ts +0 -46
@@ -1,720 +0,0 @@
1
- import { expect, test, vi } from 'vitest'
2
-
3
- import {
4
- createForm,
5
- LifeCycleTypes,
6
- onFieldValueChange,
7
- onFormInitialValuesChange,
8
- onFormValuesChange,
9
- setValidateLanguage,
10
- } from '..'
11
- import { attach, sleep } from './shared'
12
-
13
- setValidateLanguage('en')
14
-
15
- test('创建表单实例', () => {
16
- const form = attach(createForm())
17
- expect(form).not.toBeUndefined()
18
- expect(form.initialized).toBeTruthy()
19
- expect(form.mounted).toBeTruthy()
20
- expect(form.values).toEqual({})
21
- expect(form.initialValues).toEqual({})
22
- })
23
-
24
- test('创建各类字段', () => {
25
- const form = attach(createForm())
26
- const normal = attach(form.createField({ name: 'normal', basePath: 'parent' })!)
27
- const normal2 = form.createField({ name: 'normal', basePath: 'parent' })!
28
- const array = attach(form.createArrayField({ name: 'array', basePath: 'parent' })!)
29
- const array2 = form.createArrayField({ name: 'array', basePath: 'parent' })!
30
- const object = attach(form.createObjectField({ name: 'object', basePath: 'parent' })!)
31
- const object2 = form.createObjectField({ name: 'object', basePath: 'parent' })!
32
- const voidField = attach(form.createVoidField({ name: 'void', basePath: 'parent' })!)
33
- const voidField2 = form.createVoidField({ name: 'void', basePath: 'parent' })!
34
- const children = attach(form.createField({ name: 'children', basePath: 'parent.void' })!)
35
-
36
- expect(normal).toBe(normal2)
37
- expect(array).toBe(array2)
38
- expect(object).toBe(object2)
39
- expect(voidField).toBe(voidField2)
40
- expect(normal.address.toString()).toEqual('parent.normal')
41
- expect(normal.path.toString()).toEqual('parent.normal')
42
- expect(array.address.toString()).toEqual('parent.array')
43
- expect(array.value).toEqual([])
44
- expect(object.address.toString()).toEqual('parent.object')
45
- expect(object.value).toEqual({})
46
- expect(voidField.address.toString()).toEqual('parent.void')
47
- expect(children.address.toString()).toEqual('parent.void.children')
48
- expect(children.path.toString()).toEqual('parent.children')
49
- expect(form.createField({ name: '' })).toBeUndefined()
50
- expect(form.createArrayField({ name: '' })).toBeUndefined()
51
- expect(form.createObjectField({ name: '' })).toBeUndefined()
52
- expect(form.createVoidField({ name: '' })).toBeUndefined()
53
- })
54
-
55
- test('设置与读取表单值', () => {
56
- const form = attach(createForm())
57
- form.setValues({
58
- aa: 123,
59
- cc: {
60
- kk: 321,
61
- },
62
- })
63
- expect(form.values.aa).toEqual(123)
64
- expect(form.values.cc.kk).toEqual(321)
65
-
66
- form.setValues(
67
- {
68
- cc: {
69
- mm: 'ooo',
70
- },
71
- },
72
- 'deepMerge'
73
- )
74
- expect(form.values.cc.kk).toEqual(321)
75
- expect(form.values.cc.mm).toEqual('ooo')
76
-
77
- form.setValues({ bb: '123' }, 'shallowMerge')
78
- expect(form.values.bb).toEqual('123')
79
-
80
- form.setValues({ aa: 456 }, 'overwrite')
81
- expect(form.values).toEqual({ aa: 456 })
82
- })
83
-
84
- test('按路径操作表单值', () => {
85
- const form = attach(createForm())
86
- form.setValues({ aa: 123, cc: { kk: 321 } })
87
-
88
- expect(form.getValuesIn('aa')).toEqual(123)
89
- expect(form.getValuesIn('cc.kk')).toEqual(321)
90
- expect(form.existValuesIn('cc.kk')).toBeTruthy()
91
-
92
- form.setValuesIn('cc.mm', 'ooo')
93
- expect(form.values.cc.mm).toEqual('ooo')
94
- expect(form.existValuesIn('cc.mm')).toBeTruthy()
95
-
96
- form.deleteValuesIn('cc.mm')
97
- expect(form.existValuesIn('cc.mm')).toBeFalsy()
98
-
99
- form.setInitialValuesIn('dd', 1)
100
- expect(form.initialValues.dd).toEqual(1)
101
- expect(form.getInitialValuesIn('dd')).toEqual(1)
102
- expect(form.existInitialValuesIn('dd')).toBeTruthy()
103
-
104
- form.deleteInitialValuesIn('dd')
105
- expect(form.existInitialValuesIn('dd')).toBeFalsy()
106
- })
107
-
108
- test('设置表单显示与交互模式', () => {
109
- const form = attach(createForm())
110
- expect(form.display).toEqual('visible')
111
- expect(form.pattern).toEqual('editable')
112
-
113
- form.setDisplay('hidden')
114
- expect(form.display).toEqual('hidden')
115
- expect(form.hidden).toBeTruthy()
116
-
117
- form.setDisplay('visible')
118
- expect(form.visible).toBeTruthy()
119
-
120
- form.setPattern('readOnly')
121
- expect(form.pattern).toEqual('readOnly')
122
- expect(form.readOnly).toBeTruthy()
123
-
124
- form.setPattern('disabled')
125
- expect(form.disabled).toBeTruthy()
126
-
127
- form.setPattern('readPretty')
128
- expect(form.readPretty).toBeTruthy()
129
-
130
- form.setPattern('editable')
131
- expect(form.editable).toBeTruthy()
132
- })
133
-
134
- test('校验表单并收集错误', async () => {
135
- const form = attach(createForm())
136
- const aa = attach(form.createField({ name: 'aa', required: true })!)
137
- const bb = attach(form.createField({ name: 'bb', required: true, value: '123' })!)
138
- const cc = attach(form.createField({ name: 'cc', required: true, pattern: 'readOnly' })!)
139
-
140
- await expect(form.validate()).rejects.toBeTruthy()
141
-
142
- expect(form.invalid).toBeTruthy()
143
- expect(aa.selfErrors).toEqual(['The field value is required'])
144
- expect(bb.selfErrors).toEqual([])
145
- expect(cc.selfErrors).toEqual([])
146
- expect(form.errors).toHaveLength(1)
147
- expect(form.errors[0]).toMatchObject({
148
- type: 'error',
149
- address: 'aa',
150
- path: 'aa',
151
- code: 'ValidateError',
152
- triggerType: 'onInput',
153
- messages: ['The field value is required'],
154
- })
155
-
156
- await aa.onInput('123')
157
- expect(aa.selfErrors).toEqual([])
158
- await form.validate()
159
- expect(form.valid).toBeTruthy()
160
- })
161
-
162
- test('提交表单', async () => {
163
- const form = attach(createForm({ values: { aa: '123' } }))
164
- const result = await form.submit(async values => ({ ...values, submitted: true }))
165
- expect(result).toEqual({ aa: '123', submitted: true })
166
- expect(form.submitting).toBeFalsy()
167
- })
168
-
169
- test('提交时校验失败抛出错误', async () => {
170
- const form = attach(createForm())
171
- attach(form.createField({ name: 'aa', required: true }))
172
- await expect(form.submit()).rejects.toBeTruthy()
173
- expect(form.valid).toBeFalsy()
174
- })
175
-
176
- test('重置表单', async () => {
177
- const form = attach(createForm())
178
- const field = attach(form.createField({ name: 'aa', initialValue: 'init' })!)
179
-
180
- await field.onInput('changed')
181
- expect(field.value).toEqual('changed')
182
- expect(field.modified).toBeTruthy()
183
- expect(form.modified).toBeTruthy()
184
-
185
- await form.reset()
186
- expect(field.value).toEqual('init')
187
- expect(field.modified).toBeFalsy()
188
- expect(form.modified).toBeFalsy()
189
- })
190
-
191
- test('读取与设置表单及字段状态', () => {
192
- const form = attach(createForm({ values: { aa: 1 } }))
193
- expect(form.getState().values).toEqual({ aa: 1 })
194
-
195
- form.setState({ display: 'hidden', pattern: 'readOnly' })
196
- expect(form.display).toEqual('hidden')
197
- expect(form.pattern).toEqual('readOnly')
198
-
199
- form.setState(state => {
200
- state.display = 'visible'
201
- })
202
- expect(form.display).toEqual('visible')
203
- expect(form.getFormState(state => state.display)).toEqual('visible')
204
-
205
- form.setFormState({ pattern: 'editable' })
206
- expect(form.pattern).toEqual('editable')
207
-
208
- const field = attach(form.createField({ name: 'bb' })!)
209
- form.setFieldState('bb', { title: 'BB' })
210
- expect(field.title).toEqual('BB')
211
- expect(form.getFieldState('bb', state => state.title)).toEqual('BB')
212
- })
213
-
214
- test('订阅与取消订阅表单生命周期', async () => {
215
- const form = attach(createForm())
216
- const types: string[] = []
217
- const id = form.subscribe(({ type }) => {
218
- types.push(type)
219
- })
220
-
221
- form.onMount()
222
- form.setLoading(true)
223
- await sleep(150)
224
- form.setLoading(false)
225
- form.unsubscribe(id!)
226
-
227
- expect(types).toContain(LifeCycleTypes.ON_FORM_MOUNT)
228
- expect(types).toContain(LifeCycleTypes.ON_FORM_LOADING)
229
- })
230
-
231
- test('设置表单加载与校验状态', async () => {
232
- const form = attach(createForm())
233
- expect(form.loading).toBeFalsy()
234
- form.setLoading(true)
235
- await sleep(150)
236
- expect(form.loading).toBeTruthy()
237
- form.setLoading(false)
238
-
239
- expect(form.validating).toBeFalsy()
240
- form.setValidating(true)
241
- await sleep(150)
242
- expect(form.validating).toBeTruthy()
243
- form.setValidating(false)
244
- })
245
-
246
- test('表单卸载后销毁字段', () => {
247
- const form = attach(createForm())
248
- const field = attach(form.createField({ name: 'aa' })!)
249
- expect(form.fields.aa).toBe(field)
250
-
251
- form.onUnmount()
252
- expect(form.unmounted).toBeTruthy()
253
- expect(form.fields.aa).toBeUndefined()
254
- })
255
-
256
- test('表单值与初始值变更触发生命周期', () => {
257
- let valuesChangeCount = 0
258
- let initialValuesChangeCount = 0
259
- const form = attach(
260
- createForm({
261
- effects() {
262
- onFormValuesChange(() => {
263
- valuesChangeCount++
264
- })
265
- onFormInitialValuesChange(() => {
266
- initialValuesChangeCount++
267
- })
268
- },
269
- })
270
- )
271
- expect(valuesChangeCount).toEqual(0)
272
- expect(initialValuesChangeCount).toEqual(0)
273
-
274
- form.setValues({ aa: '123' })
275
- expect(valuesChangeCount).toBeGreaterThan(0)
276
-
277
- form.setInitialValues({ aa: '321', bb: '123' })
278
- expect(initialValuesChangeCount).toBeGreaterThan(0)
279
- // 无字段承载时,初始值变更会通过 patchFormValues 回写 values
280
- expect(form.values.aa).toEqual('321')
281
- expect(form.values.bb).toEqual('123')
282
- })
283
-
284
- test('初始值变更按默认值规则回写表单值', () => {
285
- const form = attach(createForm())
286
- form.setValues({ aa: '123' })
287
-
288
- form.setInitialValuesIn('bb', '456')
289
- expect(form.initialValues.bb).toEqual('456')
290
- // values 中原本不存在的键会被初始值填充
291
- expect(form.values.bb).toEqual('456')
292
-
293
- form.setInitialValuesIn('aa', '999')
294
- // 已有值且无字段承载时同样会被更新
295
- expect(form.values.aa).toEqual('999')
296
- })
297
-
298
- test('覆盖赋值后变更监听仍然生效', () => {
299
- let valuesChangeCount = 0
300
- let initialValuesChangeCount = 0
301
- const form = attach(
302
- createForm({
303
- effects() {
304
- onFormValuesChange(() => {
305
- valuesChangeCount++
306
- })
307
- onFormInitialValuesChange(() => {
308
- initialValuesChangeCount++
309
- })
310
- },
311
- })
312
- )
313
-
314
- form.setValues({ aa: 1 }, 'overwrite')
315
- // 整体替换本身不触发旧对象的监听,关键是替换后的新对象仍能被监听
316
- form.setValuesIn('aa', 2)
317
- expect(valuesChangeCount).toBeGreaterThan(0)
318
- const countAfterMutation = valuesChangeCount
319
-
320
- form.setInitialValues({ bb: 1 }, 'overwrite')
321
- expect(initialValuesChangeCount).toEqual(0)
322
-
323
- // initialValues 被整体替换后,监听器同样应重新订阅到新对象
324
- form.setInitialValuesIn('bb', 2)
325
- expect(initialValuesChangeCount).toBeGreaterThan(0)
326
- expect(valuesChangeCount).toBeGreaterThan(countAfterMutation)
327
- })
328
-
329
- test('setEffects 注册的变更生命周期正常触发', () => {
330
- let initialValuesTriggerNum = 0
331
- let valuesTriggerNum = 0
332
- const form = attach(
333
- createForm<{ aa?: number; bb?: number }>({
334
- initialValues: { aa: 1 },
335
- values: { bb: 1 },
336
- })
337
- )
338
-
339
- form.setEffects(() => {
340
- onFormInitialValuesChange(() => {
341
- initialValuesTriggerNum++
342
- })
343
- onFormValuesChange(() => {
344
- valuesTriggerNum++
345
- })
346
- })
347
-
348
- expect(initialValuesTriggerNum).toEqual(0)
349
- expect(valuesTriggerNum).toEqual(0)
350
-
351
- form.initialValues.aa = 2
352
- form.values.bb = 2
353
- expect(initialValuesTriggerNum).toEqual(1)
354
- // initialValues 的回写会额外触发一次 values 变更
355
- expect(valuesTriggerNum).toEqual(2)
356
-
357
- form.setInitialValues({ aa: 3 })
358
- form.setValues({ bb: 3 })
359
- expect(initialValuesTriggerNum).toEqual(2)
360
- expect(valuesTriggerNum).toEqual(4)
361
-
362
- form.initialValues.aa = 4
363
- form.values.bb = 4
364
- expect(initialValuesTriggerNum).toEqual(3)
365
- expect(valuesTriggerNum).toEqual(6)
366
- })
367
-
368
- test('表单图快照导出与恢复', () => {
369
- const form = attach(createForm({ values: { aa: 1 } }))
370
- attach(form.createField({ name: 'aa', title: '字段一' }))
371
-
372
- const graph = form.graph.getGraph()
373
- expect(graph['']).toBeDefined()
374
- expect(graph.aa).toMatchObject({ address: 'aa', title: '字段一' })
375
-
376
- const restored = attach(createForm())
377
- restored.graph.setGraph(graph)
378
- expect(restored.fields.aa).not.toBeUndefined()
379
- expect(restored.fields.aa.title).toEqual('字段一')
380
- expect(restored.values.aa).toEqual(1)
381
- })
382
-
383
- test('setSubmitting 延迟生效', async () => {
384
- const form = attach(createForm())
385
- form.setSubmitting(true)
386
- expect(form.submitting).toBeFalsy()
387
- await sleep(150)
388
- expect(form.submitting).toBeTruthy()
389
- form.setSubmitting(false)
390
- expect(form.submitting).toBeFalsy()
391
- })
392
-
393
- test('setEffects/addEffects/removeEffects', () => {
394
- const form = attach(createForm())
395
- const valueChange = vi.fn()
396
- const valueChange2 = vi.fn()
397
-
398
- form.addEffects('e1', () => {
399
- onFieldValueChange('aa', valueChange)
400
- })
401
- const field = attach(form.createField({ name: 'aa' })!)
402
- field.setValue('123')
403
- expect(valueChange).toHaveBeenCalledTimes(1)
404
-
405
- form.removeEffects('e1')
406
- field.setValue('321')
407
- expect(valueChange).toHaveBeenCalledTimes(1)
408
-
409
- // 同一 id 重复注册不会叠加
410
- form.addEffects('e2', () => {
411
- onFieldValueChange('aa', valueChange)
412
- })
413
- form.addEffects('e2', () => {
414
- onFieldValueChange('aa', valueChange)
415
- })
416
- field.setValue('444')
417
- expect(valueChange).toHaveBeenCalledTimes(2)
418
-
419
- // setEffects 只替换 props.effects 产生的生命周期,addEffects 注册的 outer 生命周期仍保留
420
- form.setEffects(() => {
421
- onFieldValueChange('aa', valueChange2)
422
- })
423
- field.setValue('555')
424
- expect(valueChange).toHaveBeenCalledTimes(3)
425
- expect(valueChange2).toHaveBeenCalledTimes(1)
426
- })
427
-
428
- test('notify/subscribe/unsubscribe', () => {
429
- const form = attach(createForm())
430
- const subscribe = vi.fn()
431
- const id = form.subscribe(subscribe)
432
-
433
- form.notify(LifeCycleTypes.ON_FORM_SUBMIT)
434
- expect(subscribe).toHaveBeenCalledTimes(1)
435
-
436
- form.unsubscribe(id!)
437
- form.notify(LifeCycleTypes.ON_FORM_SUBMIT)
438
- expect(subscribe).toHaveBeenCalledTimes(1)
439
- })
440
-
441
- test('setState/getState 快照恢复', () => {
442
- const form = attach(createForm())
443
- const state = form.getState()
444
-
445
- form.setState(state => {
446
- state.pattern = 'disabled'
447
- state.values = { aa: 123 }
448
- })
449
- expect(form.pattern).toEqual('disabled')
450
- expect(form.disabled).toBeTruthy()
451
- expect(form.values.aa).toEqual(123)
452
-
453
- form.setState(state)
454
- expect(form.pattern).toEqual('editable')
455
- expect(form.disabled).toBeFalsy()
456
- expect(form.values.aa).toBeUndefined()
457
- })
458
-
459
- test('setFieldState/getFieldState 支持字段与 Query 入参', () => {
460
- const form = attach(createForm())
461
- const field = attach(form.createField({ name: 'aa' })!)
462
- const query = form.query('aa')
463
-
464
- form.setFieldState(query, { title: '通过 Query 设置' })
465
- expect(field.title).toEqual('通过 Query 设置')
466
- expect(form.getFieldState(query, state => state.title)).toEqual('通过 Query 设置')
467
-
468
- form.setFieldState(field, { title: '通过字段设置' })
469
- expect(form.getFieldState(field, state => state.title)).toEqual('通过字段设置')
470
- })
471
-
472
- test('setFieldState 支持正则与通配符批量设置', () => {
473
- const form = attach(createForm())
474
- const aa = attach(form.createField({ name: 'aa' })!)
475
- const ab = attach(form.createField({ name: 'ab' })!)
476
-
477
- form.setFieldState(/a/, { description: '匹配' })
478
- expect(aa.description).toEqual('匹配')
479
- expect(ab.description).toEqual('匹配')
480
-
481
- form.setFieldState('*', { description: '通配' })
482
- expect(aa.description).toEqual('通配')
483
- expect(ab.description).toEqual('通配')
484
- })
485
-
486
- test('clearErrors/clearWarnings/clearSuccesses', () => {
487
- const form = attach(createForm())
488
- const aa = attach(form.createField({ name: 'aa' })!)
489
- const bb = attach(form.createField({ name: 'bb' })!)
490
-
491
- aa.setFeedback({ type: 'error', messages: ['aa error'] })
492
- aa.setFeedback({ type: 'warning', messages: ['aa warning'] })
493
- aa.setFeedback({ type: 'success', messages: ['aa success'] })
494
- bb.setFeedback({ type: 'error', messages: ['bb error'] })
495
-
496
- expect(form.errors).toHaveLength(2)
497
- expect(form.warnings).toHaveLength(1)
498
- expect(form.successes).toHaveLength(1)
499
-
500
- form.clearErrors('aa')
501
- expect(form.errors).toHaveLength(1)
502
- expect(bb.selfErrors).toEqual(['bb error'])
503
-
504
- form.clearErrors()
505
- form.clearWarnings()
506
- form.clearSuccesses()
507
- expect(form.errors).toHaveLength(0)
508
- expect(form.warnings).toHaveLength(0)
509
- expect(form.successes).toHaveLength(0)
510
- })
511
-
512
- test('queryFeedbacks 按地址与类型过滤', () => {
513
- const form = attach(createForm())
514
- const aa = attach(form.createField({ name: 'aa' })!)
515
- attach(form.createField({ name: 'bb' })!)
516
-
517
- aa.setFeedback({ type: 'error', messages: ['aa error'] })
518
- aa.setFeedback({ type: 'warning', messages: ['aa warning'] })
519
-
520
- expect(form.queryFeedbacks({ type: 'error' })).toHaveLength(1)
521
- expect(form.queryFeedbacks({ type: 'warning' })).toHaveLength(1)
522
- expect(form.queryFeedbacks({ address: 'aa' })).toHaveLength(2)
523
- expect(form.queryFeedbacks({ path: 'aa' })).toHaveLength(2)
524
- expect(form.queryFeedbacks({ address: 'bb' })).toHaveLength(0)
525
- expect(form.queryFeedbacks({ address: '*' })).toHaveLength(2)
526
- })
527
-
528
- test('devtools 钩子在挂载与卸载时调用', () => {
529
- const inject = vi.fn()
530
- const unmount = vi.fn()
531
- ;(globalThis as any)['__FORMILY_DEV_TOOLS_HOOK__'] = { inject, unmount }
532
-
533
- const form = createForm()
534
- form.onMount()
535
- expect(inject).toHaveBeenCalledWith(form.id, form)
536
- form.onUnmount()
537
- expect(unmount).toHaveBeenCalledWith(form.id)
538
-
539
- delete (globalThis as any)['__FORMILY_DEV_TOOLS_HOOK__']
540
- })
541
-
542
- test('designable 表单允许重复创建同名字段', () => {
543
- const form = attach(createForm({ designable: true }))
544
- const first = attach(form.createField({ name: 'bb', initialValue: 123 })!)
545
- const second = attach(form.createField({ name: 'bb', initialValue: 321 })!)
546
- attach(form.createField({ name: 'aa', value: 123 }))
547
- attach(form.createField({ name: 'aa', value: 321 }))
548
-
549
- expect(second).not.toBe(first)
550
- expect(form.fields.bb).toBe(second)
551
- expect(form.values.aa).toEqual(321)
552
- expect(form.initialValues.bb).toEqual(321)
553
- })
554
-
555
- test('validate 跳过 display none 的字段', async () => {
556
- const form = attach(createForm())
557
- const field = attach(form.createField({ name: 'aa', required: true, display: 'none' })!)
558
-
559
- await form.validate()
560
- expect(field.selfErrors).toEqual([])
561
-
562
- field.setDisplay('visible')
563
- await expect(form.validate()).rejects.toBeTruthy()
564
- expect(field.selfErrors).toEqual(['The field value is required'])
565
- })
566
-
567
- test('validate 跳过未挂载字段', async () => {
568
- const form = attach(createForm())
569
- const mounted = attach(form.createField({ name: 'aa', required: true })!)
570
- const unmounted = form.createField({ name: 'bb', required: true })!
571
- unmounted.onUnmount()
572
-
573
- try {
574
- await form.validate()
575
- } catch {}
576
- expect(mounted.selfErrors).toHaveLength(1)
577
- // 卸载后的字段仍会被查询到,但 reaction 已清理,不会因 display 变更而误报
578
- expect(unmounted.mounted).toBeFalsy()
579
- })
580
-
581
- test('校验器抛错时表单状态可恢复', async () => {
582
- const form = attach(createForm())
583
- attach(
584
- form.createField({
585
- name: 'aa',
586
- validator() {
587
- throw new Error('runtime error')
588
- },
589
- })
590
- )
591
- try {
592
- await form.validate()
593
- } catch {}
594
- expect(form.invalid).toBeTruthy()
595
- expect(form.validating).toBeFalsy()
596
- })
597
-
598
- test('表单卸载不影响字段值', () => {
599
- const form = attach(createForm({ values: { aa: '123' } }))
600
- attach(form.createField({ name: 'aa' }))
601
- expect(form.values.aa).toEqual('123')
602
- form.onUnmount()
603
- expect(form.values.aa).toEqual('123')
604
- })
605
-
606
- test('clearFormGraph 默认清理值,可显式保留', () => {
607
- const form = attach(createForm({ values: { aa: '123' } }))
608
- attach(form.createField({ name: 'aa' }))
609
- form.clearFormGraph('*')
610
- expect(form.values.aa).toBeUndefined()
611
-
612
- const form2 = attach(createForm({ values: { aa: '123' } }))
613
- attach(form2.createField({ name: 'aa' }))
614
- form2.clearFormGraph('*', false)
615
- expect(form2.values.aa).toEqual('123')
616
- })
617
-
618
- test('merge 策略下 values/initialValues 引用保持不变', () => {
619
- const form = attach(createForm({ values: { aa: '123' }, initialValues: { aa: '123' } }))
620
- const values = form.values
621
- const initialValues = form.initialValues
622
-
623
- form.setValues({ bb: '321' })
624
- form.setInitialValues({ bb: '321' })
625
- expect(form.values === values).toBeTruthy()
626
- expect(form.initialValues === initialValues).toBeTruthy()
627
-
628
- form.setValues({ cc: 1 }, 'overwrite')
629
- expect(form.values === values).toBeFalsy()
630
- })
631
-
632
- test('display none 时移除表单值,恢复后写回', () => {
633
- const form = attach(createForm())
634
- const field = attach(form.createField({ name: 'aaa', initialValue: 123 })!)
635
- expect(form.values).toMatchObject({ aaa: 123 })
636
-
637
- field.display = 'none'
638
- expect(Object.prototype.hasOwnProperty.call(form.values, 'aaa')).toBeFalsy()
639
-
640
- field.display = 'visible'
641
- expect(Object.prototype.hasOwnProperty.call(form.values, 'aaa')).toBeTruthy()
642
-
643
- field.setValue(undefined)
644
- expect(Object.prototype.hasOwnProperty.call(form.values, 'aaa')).toBeTruthy()
645
- })
646
-
647
- test('数组初始值按空值规则处理', () => {
648
- const form = attach(
649
- createForm({
650
- initialValues: {
651
- aa: [0],
652
- bb: [''],
653
- cc: [],
654
- dd: [null],
655
- ee: [undefined],
656
- },
657
- })
658
- )
659
- ;['aa', 'bb', 'cc', 'dd', 'ee'].forEach(name => {
660
- form.createArrayField({ name })
661
- })
662
- expect(form.values.aa).toEqual([0])
663
- expect(form.values.bb).toEqual([''])
664
- expect(form.values.cc).toEqual([])
665
- // 元素全为 null/undefined 的数组按空值处理,会被数组字段的默认值 [] 覆盖
666
- expect(form.values.dd).toEqual([])
667
- expect(form.values.ee).toEqual([])
668
- })
669
-
670
- test('创建字段后初始值仍与已有表单值合并', () => {
671
- const form = attach(createForm())
672
- const array = attach(form.createArrayField({ name: 'array' })!)
673
- form.values.array = [{ aa: '321' }]
674
- const child = attach(form.createField({ name: 'aa', basePath: 'array.0', initialValue: '123' })!)
675
-
676
- expect(array.value).toEqual([{ aa: '321' }])
677
- expect(child.value).toEqual('321')
678
-
679
- const form2 = attach(createForm())
680
- const aa = attach(form2.createArrayField({ name: 'aa', initialValue: '111' })!)
681
- const array2 = attach(form2.createArrayField({ name: 'array' })!)
682
- const arr0 = attach(form2.createField({ name: 'aa', basePath: 'array.0', initialValue: '123' })!)
683
- form2.values.aa = '222'
684
- form2.values.array = [{ aa: '321' }]
685
- expect(array2.value).toEqual([{ aa: '321' }])
686
- expect(arr0.value).toEqual('321')
687
- expect(aa.value).toEqual('222')
688
- })
689
-
690
- test('initialValues 为空时不会覆盖 values', () => {
691
- const form = attach(createForm({ values: { array: [1, 2, 3] } }))
692
- attach(form.createObjectField({ name: 'array.0.1' }))
693
- expect(form.values).toEqual({ array: [1, 2, 3] })
694
- })
695
-
696
- test('重置数组与对象字段使用类型默认值', async () => {
697
- const form = attach(createForm({ values: { array: [{ value: 123 }], object: { value: 123 } } }))
698
- attach(form.createArrayField({ name: 'array', required: true }))
699
- attach(form.createObjectField({ name: 'object', required: true }))
700
-
701
- await form.reset('*', { forceClear: true })
702
- expect(form.values).toEqual({ array: [], object: {} })
703
- })
704
-
705
- test('查询空字段不会抛错并清理 fields', () => {
706
- const form = attach(createForm())
707
- ;(form.fields as any)['a'] = undefined
708
- expect(() => form.query('*').take()).not.toThrow()
709
- expect(Object.keys(form.fields)).toEqual([])
710
- })
711
-
712
- test('响应式 values 传入时不会被二次克隆', () => {
713
- const values: any = { aa: 1 }
714
- const form = attach(createForm({ values }))
715
- form.setValues({ bb: 2 })
716
- expect(form.values.aa).toEqual(1)
717
- expect(form.values.bb).toEqual(2)
718
- // 普通对象入参会先克隆,避免外部引用被意外修改
719
- expect(values.bb).toBeUndefined()
720
- })