@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,595 +0,0 @@
1
- import { expect, test, vi } from 'vitest'
2
-
3
- import {
4
- createEffectContext,
5
- createForm,
6
- isVoidField,
7
- onFieldChange,
8
- onFieldInit,
9
- onFieldInitialValueChange,
10
- onFieldInputValueChange,
11
- onFieldLoading,
12
- onFieldMount,
13
- onFieldReact,
14
- onFieldReset,
15
- onFieldSubmit,
16
- onFieldSubmitEnd,
17
- onFieldSubmitFailed,
18
- onFieldSubmitStart,
19
- onFieldSubmitSuccess,
20
- onFieldSubmitValidateEnd,
21
- onFieldSubmitValidateFailed,
22
- onFieldSubmitValidateStart,
23
- onFieldSubmitValidateSuccess,
24
- onFieldUnmount,
25
- onFieldValidateEnd,
26
- onFieldValidateFailed,
27
- onFieldValidateStart,
28
- onFieldValidateSuccess,
29
- onFieldValueChange,
30
- onFieldValidating,
31
- onFormInit,
32
- onFormInitialValuesChange,
33
- onFormInputChange,
34
- onFormLoading,
35
- onFormMount,
36
- onFormReact,
37
- onFormReset,
38
- onFormSubmit,
39
- onFormSubmitEnd,
40
- onFormSubmitFailed,
41
- onFormSubmitStart,
42
- onFormSubmitSuccess,
43
- onFormSubmitValidateEnd,
44
- onFormSubmitValidateFailed,
45
- onFormSubmitValidateStart,
46
- onFormSubmitValidateSuccess,
47
- onFormUnmount,
48
- onFormValidateEnd,
49
- onFormValidateFailed,
50
- onFormValidateStart,
51
- onFormValidateSuccess,
52
- onFormValuesChange,
53
- } from '..'
54
- import { runEffects } from '../shared/effective'
55
- import { attach, sleep } from './shared'
56
-
57
- test('onFormInit/onFormMount/onFormUnmount', () => {
58
- const init = vi.fn()
59
- const mount = vi.fn()
60
- const unmount = vi.fn()
61
- const form = attach(
62
- createForm({
63
- effects() {
64
- onFormInit(init)
65
- onFormMount(mount)
66
- onFormUnmount(unmount)
67
- },
68
- })
69
- )
70
- expect(init).toHaveBeenCalledTimes(1)
71
- expect(mount).toHaveBeenCalledTimes(1)
72
- expect(unmount).not.toHaveBeenCalled()
73
- form.onUnmount()
74
- expect(unmount).toHaveBeenCalledTimes(1)
75
- })
76
-
77
- test('onFormValuesChange/onFormInitialValuesChange', () => {
78
- const valuesChange = vi.fn()
79
- const initialValuesChange = vi.fn()
80
- const form = attach(
81
- createForm({
82
- effects() {
83
- onFormValuesChange(valuesChange)
84
- onFormInitialValuesChange(initialValuesChange)
85
- },
86
- })
87
- )
88
- expect(valuesChange).not.toHaveBeenCalled()
89
- expect(initialValuesChange).not.toHaveBeenCalled()
90
-
91
- form.setValues({ aa: '123' })
92
- expect(form.values.aa).toEqual('123')
93
- expect(valuesChange).toHaveBeenCalled()
94
-
95
- form.setInitialValues({ aa: '321', bb: '123' })
96
- expect(form.values.aa).toEqual('321')
97
- expect(form.values.bb).toEqual('123')
98
- expect(initialValuesChange).toHaveBeenCalled()
99
- })
100
-
101
- test('onFormLoading', async () => {
102
- const loading = vi.fn()
103
- const form = attach(
104
- createForm({
105
- effects() {
106
- onFormLoading(loading)
107
- },
108
- })
109
- )
110
- form.setLoading(true)
111
- await sleep(150)
112
- expect(loading).toHaveBeenCalledTimes(1)
113
- form.setLoading(false)
114
- })
115
-
116
- test('onFormInputChange', () => {
117
- const inputChange = vi.fn()
118
- const valuesChange = vi.fn()
119
- const form = attach(
120
- createForm({
121
- effects() {
122
- onFormValuesChange(valuesChange)
123
- onFormInputChange(inputChange)
124
- },
125
- })
126
- )
127
- const field = attach(form.createField({ name: 'aa' })!)
128
-
129
- expect(inputChange).not.toHaveBeenCalled()
130
- expect(valuesChange).not.toHaveBeenCalled()
131
-
132
- field.setValue('123')
133
- expect(inputChange).not.toHaveBeenCalled()
134
- expect(valuesChange).toHaveBeenCalledTimes(1)
135
-
136
- field.onInput('123')
137
- expect(inputChange).toHaveBeenCalled()
138
- expect(valuesChange).toHaveBeenCalledTimes(1)
139
-
140
- field.onInput('321')
141
- expect(inputChange).toHaveBeenCalledTimes(2)
142
- expect(valuesChange).toHaveBeenCalledTimes(2)
143
- })
144
-
145
- test('onFormReact', () => {
146
- const react = vi.fn()
147
- const form = attach(
148
- createForm({
149
- effects() {
150
- onFormReact(form => {
151
- if (form.values.aa) react()
152
- })
153
- },
154
- })
155
- )
156
- expect(react).not.toHaveBeenCalled()
157
- form.setValues({ aa: 123 })
158
- expect(react).toHaveBeenCalledTimes(1)
159
- form.onUnmount()
160
-
161
- // 不传 callback 也不会抛错
162
- const form2 = attach(
163
- createForm({
164
- effects() {
165
- onFormReact()
166
- },
167
- })
168
- )
169
- form2.onUnmount()
170
- })
171
-
172
- test('onFormReset', async () => {
173
- const reset = vi.fn()
174
- const form = attach(
175
- createForm({
176
- initialValues: { aa: 123 },
177
- effects() {
178
- onFormReset(reset)
179
- },
180
- })
181
- )
182
- const field = attach(form.createField({ name: 'aa' })!)
183
- field.setValue('xxxx')
184
- expect(field.value).toEqual('xxxx')
185
- expect(reset).not.toHaveBeenCalled()
186
-
187
- await form.reset()
188
- expect(field.value).toEqual(123)
189
- expect(form.values.aa).toEqual(123)
190
- expect(reset).toHaveBeenCalledTimes(1)
191
- })
192
-
193
- test('onFormSubmit', async () => {
194
- const submit = vi.fn()
195
- const submitStart = vi.fn()
196
- const submitEnd = vi.fn()
197
- const submitSuccess = vi.fn()
198
- const submitFailed = vi.fn()
199
- const submitValidateStart = vi.fn()
200
- const submitValidateFailed = vi.fn()
201
- const submitValidateSuccess = vi.fn()
202
- const submitValidateEnd = vi.fn()
203
- const form = attach(
204
- createForm({
205
- effects() {
206
- onFormSubmitStart(submitStart)
207
- onFormSubmit(submit)
208
- onFormSubmitEnd(submitEnd)
209
- onFormSubmitFailed(submitFailed)
210
- onFormSubmitSuccess(submitSuccess)
211
- onFormSubmitValidateStart(submitValidateStart)
212
- onFormSubmitValidateFailed(submitValidateFailed)
213
- onFormSubmitValidateSuccess(submitValidateSuccess)
214
- onFormSubmitValidateEnd(submitValidateEnd)
215
- },
216
- })
217
- )
218
- const field = attach(form.createField({ name: 'aa', required: true })!)
219
-
220
- try {
221
- await form.submit()
222
- } catch {}
223
- expect(submitStart).toHaveBeenCalledTimes(1)
224
- expect(submit).toHaveBeenCalledTimes(1)
225
- expect(submitEnd).toHaveBeenCalledTimes(1)
226
- expect(submitSuccess).not.toHaveBeenCalled()
227
- expect(submitFailed).toHaveBeenCalledTimes(1)
228
- expect(submitValidateStart).toHaveBeenCalledTimes(1)
229
- expect(submitValidateFailed).toHaveBeenCalledTimes(1)
230
- expect(submitValidateSuccess).not.toHaveBeenCalled()
231
- expect(submitValidateEnd).toHaveBeenCalledTimes(1)
232
-
233
- field.onInput('123')
234
- try {
235
- await form.submit()
236
- } catch {}
237
- expect(submitStart).toHaveBeenCalledTimes(2)
238
- expect(submit).toHaveBeenCalledTimes(2)
239
- expect(submitEnd).toHaveBeenCalledTimes(2)
240
- expect(submitSuccess).toHaveBeenCalledTimes(1)
241
- expect(submitFailed).toHaveBeenCalledTimes(1)
242
- expect(submitValidateStart).toHaveBeenCalledTimes(2)
243
- expect(submitValidateFailed).toHaveBeenCalledTimes(1)
244
- expect(submitValidateSuccess).toHaveBeenCalledTimes(1)
245
- expect(submitValidateEnd).toHaveBeenCalledTimes(2)
246
- })
247
-
248
- test('onFormValidate', async () => {
249
- const validateStart = vi.fn()
250
- const validateEnd = vi.fn()
251
- const validateFailed = vi.fn()
252
- const validateSuccess = vi.fn()
253
- const form = attach(
254
- createForm({
255
- effects() {
256
- onFormValidateStart(validateStart)
257
- onFormValidateEnd(validateEnd)
258
- onFormValidateFailed(validateFailed)
259
- onFormValidateSuccess(validateSuccess)
260
- },
261
- })
262
- )
263
- const field = attach(form.createField({ name: 'aa', required: true })!)
264
-
265
- try {
266
- await form.validate()
267
- } catch {}
268
- expect(validateStart).toHaveBeenCalledTimes(1)
269
- expect(validateEnd).toHaveBeenCalledTimes(1)
270
- expect(validateFailed).toHaveBeenCalledTimes(1)
271
- expect(validateSuccess).not.toHaveBeenCalled()
272
-
273
- field.onInput('123')
274
- try {
275
- await form.validate()
276
- } catch {}
277
- expect(validateStart).toHaveBeenCalledTimes(2)
278
- expect(validateEnd).toHaveBeenCalledTimes(2)
279
- expect(validateFailed).toHaveBeenCalledTimes(1)
280
- expect(validateSuccess).toHaveBeenCalledTimes(1)
281
- })
282
-
283
- test('onFieldInit/onFieldMount/onFieldUnmount', () => {
284
- const fieldInit = vi.fn()
285
- const fieldMount = vi.fn()
286
- const fieldUnmount = vi.fn()
287
- const form = attach(
288
- createForm({
289
- effects() {
290
- onFieldInit('aa', fieldInit)
291
- onFieldMount('aa', fieldMount)
292
- onFieldUnmount('aa', fieldUnmount)
293
- },
294
- })
295
- )
296
- const field = attach(form.createField({ name: 'aa' })!)
297
- expect(fieldInit).toHaveBeenCalledTimes(1)
298
- expect(fieldMount).toHaveBeenCalledTimes(1)
299
- expect(fieldUnmount).not.toHaveBeenCalled()
300
-
301
- field.onUnmount()
302
- expect(fieldUnmount).toHaveBeenCalledTimes(1)
303
- })
304
-
305
- test('onFieldInit 已存在字段时立即回调', () => {
306
- const form = attach(createForm())
307
- const field = attach(form.createField({ name: 'aa' })!)
308
- const init = vi.fn()
309
- form.setEffects(() => {
310
- onFieldInit('aa', init)
311
- })
312
- expect(init).toHaveBeenCalledWith(field, form)
313
- })
314
-
315
- test('onFieldChange', async () => {
316
- const fieldChange = vi.fn()
317
- const valueChange = vi.fn()
318
- const form = attach(
319
- createForm({
320
- effects() {
321
- onFieldChange(
322
- 'aa',
323
- ['value', 'disabled', 'initialized', 'inputValue', 'loading', 'visible', 'editable'],
324
- fieldChange
325
- )
326
- onFieldChange('aa', valueChange)
327
- },
328
- })
329
- )
330
- const field = attach(form.createField({ name: 'aa' })!)
331
-
332
- expect(fieldChange).toHaveBeenCalledTimes(1)
333
- field.setValue('123')
334
- expect(fieldChange).toHaveBeenCalledTimes(2)
335
- field.onInput('321')
336
- expect(fieldChange).toHaveBeenCalledTimes(3)
337
- field.setLoading(true)
338
- await sleep(150)
339
- expect(fieldChange).toHaveBeenCalledTimes(4)
340
- field.setPattern('disabled')
341
- expect(fieldChange).toHaveBeenCalledTimes(5)
342
- field.setDisplay('none')
343
- expect(fieldChange).toHaveBeenCalledTimes(6)
344
- form.onUnmount()
345
-
346
- expect(valueChange).toHaveBeenCalledTimes(4)
347
- })
348
-
349
- test('onFieldInitialValueChange/onFieldValueChange/onFieldInputValueChange', () => {
350
- const fieldValueChange = vi.fn()
351
- const fieldInitialValueChange = vi.fn()
352
- const fieldInputValueChange = vi.fn()
353
- const notTrigger = vi.fn()
354
- const form = attach(
355
- createForm({
356
- effects() {
357
- onFieldInitialValueChange('aa', fieldInitialValueChange)
358
- onFieldValueChange('aa', fieldValueChange)
359
- onFieldInputValueChange('aa', fieldInputValueChange)
360
- onFieldValueChange('xx', notTrigger)
361
- },
362
- })
363
- )
364
- const field = attach(form.createField({ name: 'aa' })!)
365
-
366
- field.setValue('123')
367
- expect(fieldValueChange).toHaveBeenCalledTimes(1)
368
- expect(fieldInitialValueChange).not.toHaveBeenCalled()
369
- expect(fieldInputValueChange).not.toHaveBeenCalled()
370
-
371
- field.setInitialValue('xxx')
372
- expect(fieldValueChange).toHaveBeenCalledTimes(2)
373
- expect(fieldInitialValueChange).toHaveBeenCalledTimes(1)
374
- expect(fieldInputValueChange).not.toHaveBeenCalled()
375
-
376
- field.onInput('321')
377
- expect(fieldValueChange).toHaveBeenCalledTimes(3)
378
- expect(fieldInitialValueChange).toHaveBeenCalledTimes(1)
379
- expect(fieldInputValueChange).toHaveBeenCalledTimes(1)
380
- expect(notTrigger).not.toHaveBeenCalled()
381
- })
382
-
383
- test('onFieldReact', () => {
384
- const react = vi.fn()
385
- const form = attach(
386
- createForm({
387
- effects() {
388
- onFieldReact('aa', field => {
389
- if (isVoidField(field)) return
390
- if (field.value) react('value')
391
- if (field.display === 'hidden') react('display')
392
- })
393
- onFieldReact('aa', null!)
394
- },
395
- })
396
- )
397
- const field = attach(form.createField({ name: 'aa' })!)
398
- expect(react).not.toHaveBeenCalled()
399
-
400
- form.setValues({ aa: 123 })
401
- expect(react).toHaveBeenCalledWith('value')
402
-
403
- field.setDisplay('hidden')
404
- expect(react).toHaveBeenCalledWith('display')
405
- form.onUnmount()
406
- })
407
-
408
- test('onFieldLoading', async () => {
409
- const loading = vi.fn()
410
- const form = attach(
411
- createForm({
412
- effects() {
413
- onFieldLoading('aa', loading)
414
- },
415
- })
416
- )
417
- const field = attach(form.createField({ name: 'aa' })!)
418
- field.setLoading(true)
419
- await sleep(150)
420
- expect(loading).toHaveBeenCalledTimes(1)
421
- field.setLoading(false)
422
- })
423
-
424
- test('onFieldReset', async () => {
425
- const reset = vi.fn()
426
- const form = attach(
427
- createForm({
428
- effects() {
429
- onFieldReset('aa', reset)
430
- },
431
- })
432
- )
433
- const field = attach(form.createField({ name: 'aa', initialValue: 123 })!)
434
- await field.reset()
435
- expect(reset).toHaveBeenCalledTimes(1)
436
- expect(field.value).toEqual(123)
437
- })
438
-
439
- test('onFieldValidate', async () => {
440
- const validateStart = vi.fn()
441
- const validateFailed = vi.fn()
442
- const validateSuccess = vi.fn()
443
- const validateEnd = vi.fn()
444
- const validating = vi.fn()
445
- const form = attach(
446
- createForm({
447
- effects() {
448
- onFieldValidateStart('aa', validateStart)
449
- onFieldValidateEnd('aa', validateEnd)
450
- onFieldValidateFailed('aa', validateFailed)
451
- onFieldValidateSuccess('aa', validateSuccess)
452
- onFieldValidating('aa', validating)
453
- },
454
- })
455
- )
456
- const field = attach(form.createField({ name: 'aa', required: true })!)
457
-
458
- try {
459
- await field.validate()
460
- } catch {}
461
- expect(validateStart).toHaveBeenCalledTimes(1)
462
- expect(validateFailed).toHaveBeenCalledTimes(1)
463
- expect(validateSuccess).not.toHaveBeenCalled()
464
- expect(validateEnd).toHaveBeenCalledTimes(1)
465
-
466
- field.setValue('123')
467
- try {
468
- await field.validate()
469
- } catch {}
470
- expect(validateStart).toHaveBeenCalledTimes(2)
471
- expect(validateFailed).toHaveBeenCalledTimes(1)
472
- expect(validateSuccess).toHaveBeenCalledTimes(1)
473
- expect(validateEnd).toHaveBeenCalledTimes(2)
474
- })
475
-
476
- test('onFieldSubmit', async () => {
477
- const submit = vi.fn()
478
- const submitStart = vi.fn()
479
- const submitEnd = vi.fn()
480
- const submitSuccess = vi.fn()
481
- const submitFailed = vi.fn()
482
- const submitValidateStart = vi.fn()
483
- const submitValidateFailed = vi.fn()
484
- const submitValidateSuccess = vi.fn()
485
- const submitValidateEnd = vi.fn()
486
- const form = attach(
487
- createForm({
488
- effects() {
489
- onFieldSubmitStart('aa', submitStart)
490
- onFieldSubmit('aa', submit)
491
- onFieldSubmitEnd('aa', submitEnd)
492
- onFieldSubmitSuccess('aa', submitSuccess)
493
- onFieldSubmitFailed('aa', submitFailed)
494
- onFieldSubmitValidateStart('aa', submitValidateStart)
495
- onFieldSubmitValidateFailed('aa', submitValidateFailed)
496
- onFieldSubmitValidateSuccess('aa', submitValidateSuccess)
497
- onFieldSubmitValidateEnd('aa', submitValidateEnd)
498
- },
499
- })
500
- )
501
- const field = attach(form.createField({ name: 'aa', required: true })!)
502
- try {
503
- await field.submit()
504
- } catch {}
505
- expect(submitStart).toHaveBeenCalledTimes(1)
506
- expect(submit).toHaveBeenCalledTimes(1)
507
- expect(submitEnd).toHaveBeenCalledTimes(1)
508
- expect(submitSuccess).not.toHaveBeenCalled()
509
- expect(submitFailed).toHaveBeenCalledTimes(1)
510
- expect(submitValidateStart).toHaveBeenCalledTimes(1)
511
- expect(submitValidateFailed).toHaveBeenCalledTimes(1)
512
- expect(submitValidateSuccess).not.toHaveBeenCalled()
513
- expect(submitValidateEnd).toHaveBeenCalledTimes(1)
514
-
515
- field.setValue('123')
516
- const result = await field.submit(async value => ({ value }))
517
- expect(result).toEqual({ value: '123' })
518
- expect(submitSuccess).toHaveBeenCalledTimes(1)
519
- })
520
-
521
- test('异步调用 effect hook 会抛错', async () => {
522
- const valueChange = vi.fn()
523
- let error: unknown
524
- const form = attach(
525
- createForm({
526
- effects() {
527
- setTimeout(() => {
528
- try {
529
- onFieldValueChange('aa', valueChange)
530
- } catch (e) {
531
- error = e
532
- }
533
- }, 0)
534
- },
535
- })
536
- )
537
- const field = attach(form.createField({ name: 'aa' })!)
538
- await sleep(10)
539
- field.setValue('123')
540
- expect(valueChange).not.toHaveBeenCalled()
541
- expect(error).not.toBeUndefined()
542
- })
543
-
544
- test('effect context', async () => {
545
- const context = createEffectContext<number>()
546
- const context2 = createEffectContext<number>()
547
- const context3 = createEffectContext<number>(123)
548
- let results: any
549
- let error: any
550
- let error2: any
551
- function consumer() {
552
- results = context.consume()
553
- }
554
- function consumer2() {
555
- setTimeout(() => {
556
- try {
557
- results = context2.consume()
558
- } catch (e) {
559
- error2 = e
560
- }
561
- }, 0)
562
- }
563
- const context3Results = { value: undefined as number | undefined }
564
- attach(
565
- createForm({
566
- effects() {
567
- context.provide(123)
568
- context3.provide()
569
- context3Results.value = context3.consume()
570
- consumer()
571
- setTimeout(() => {
572
- try {
573
- context2.provide(123)
574
- } catch (e) {
575
- error = e
576
- }
577
- }, 0)
578
- consumer2()
579
- },
580
- })
581
- )
582
- await sleep(10)
583
- expect(results).toEqual(123)
584
- expect(context3Results.value).toEqual(123)
585
- expect(error).not.toBeUndefined()
586
- expect(error2).not.toBeUndefined()
587
- })
588
-
589
- test('runEffects', () => {
590
- expect(
591
- runEffects(123, () => {
592
- onFormMount(() => {})
593
- }).length
594
- ).toEqual(1)
595
- })
@@ -1,101 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { createForm } from '..'
4
- import {
5
- createEffectHook,
6
- isArrayField,
7
- isArrayFieldState,
8
- isDataField,
9
- isDataFieldState,
10
- isField,
11
- isFieldState,
12
- isForm,
13
- isFormState,
14
- isGeneralField,
15
- isGeneralFieldState,
16
- isObjectField,
17
- isObjectFieldState,
18
- isQuery,
19
- isVoidField,
20
- isVoidFieldState,
21
- } from '../shared/externals'
22
- import { attach } from './shared'
23
-
24
- test('type checkers', () => {
25
- const form = attach(createForm())
26
- const normal = attach(form.createField({ name: 'normal' })!)
27
- const array = attach(form.createArrayField({ name: 'array' })!)
28
- const object = attach(form.createObjectField({ name: 'object' })!)
29
- const void_ = attach(form.createVoidField({ name: 'void' })!)
30
-
31
- expect(isField(normal)).toBeTruthy()
32
- expect(isFieldState(normal.getState())).toBeTruthy()
33
- expect(isFieldState(null)).toBeFalsy()
34
- expect(isFieldState({})).toBeFalsy()
35
- expect(isFieldState(normal)).toBeFalsy()
36
-
37
- expect(isArrayField(array)).toBeTruthy()
38
- expect(isArrayFieldState(array.getState())).toBeTruthy()
39
- expect(isArrayFieldState(null)).toBeFalsy()
40
- expect(isArrayFieldState({})).toBeFalsy()
41
- expect(isArrayFieldState(array)).toBeFalsy()
42
-
43
- expect(isObjectField(object)).toBeTruthy()
44
- expect(isObjectFieldState(object.getState())).toBeTruthy()
45
- expect(isObjectFieldState(null)).toBeFalsy()
46
- expect(isObjectFieldState({})).toBeFalsy()
47
- expect(isObjectFieldState(object)).toBeFalsy()
48
-
49
- expect(isVoidField(void_)).toBeTruthy()
50
- expect(isVoidFieldState(void_.getState())).toBeTruthy()
51
- expect(isVoidFieldState(null)).toBeFalsy()
52
- expect(isVoidFieldState({})).toBeFalsy()
53
- expect(isVoidFieldState(void_)).toBeFalsy()
54
-
55
- expect(isDataField(void_)).toBeFalsy()
56
- expect(isDataFieldState(void_.getState())).toBeFalsy()
57
- expect(isDataField(normal)).toBeTruthy()
58
- expect(isDataFieldState(normal.getState())).toBeTruthy()
59
- expect(isDataField(array)).toBeTruthy()
60
- expect(isDataField(object)).toBeTruthy()
61
-
62
- expect(isGeneralField(normal)).toBeTruthy()
63
- expect(isGeneralField(array)).toBeTruthy()
64
- expect(isGeneralField(object)).toBeTruthy()
65
- expect(isGeneralField(void_)).toBeTruthy()
66
- expect(isGeneralField(form)).toBeFalsy()
67
-
68
- expect(isGeneralFieldState(normal.getState())).toBeTruthy()
69
- expect(isGeneralFieldState(array.getState())).toBeTruthy()
70
- expect(isGeneralFieldState(object.getState())).toBeTruthy()
71
- expect(isGeneralFieldState(void_.getState())).toBeTruthy()
72
- expect(isGeneralFieldState(null)).toBeFalsy()
73
- expect(isGeneralFieldState({})).toBeFalsy()
74
- expect(isGeneralFieldState(void_)).toBeFalsy()
75
-
76
- expect(isForm(form)).toBeTruthy()
77
- expect(isFormState(form.getState())).toBeTruthy()
78
- expect(isFormState({})).toBeFalsy()
79
- expect(isFormState(form)).toBeFalsy()
80
- expect(isFormState(null)).toBeFalsy()
81
-
82
- expect(isQuery(form.query('*'))).toBeTruthy()
83
- expect(isQuery(null)).toBeFalsy()
84
- })
85
-
86
- test('createEffectHook', () => {
87
- try {
88
- createEffectHook('xxx')()
89
- } catch {}
90
- const form = attach(
91
- createForm({
92
- effects() {
93
- createEffectHook('xxx')()
94
- createEffectHook('yyy', () => () => {})()
95
- },
96
- })
97
- )
98
- // 显式派发自定义事件不应抛错
99
- form.notify('xxx')
100
- form.notify('yyy')
101
- })