@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,56 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { createForm } from '..'
4
- import { isVoidField } from '../shared/checkers'
5
- import { attach } from './shared'
6
-
7
- test('getGraph/setGraph', () => {
8
- const form = attach(createForm())
9
- attach(form.createField({ name: 'normal' }))
10
- attach(form.createArrayField({ name: 'array' }))
11
- attach(form.createObjectField({ name: 'object' }))
12
- attach(form.createVoidField({ name: 'void' }))
13
- form.query('normal').take(field => {
14
- if (isVoidField(field)) return
15
- field.selfErrors = ['error']
16
- })
17
-
18
- const graph = form.getFormGraph()
19
- form.clearFormGraph()
20
- form.setFormGraph(graph)
21
- const graph2 = form.getFormGraph()
22
- expect(graph).toEqual(graph2)
23
-
24
- form.setFormGraph({
25
- object: {
26
- value: 123,
27
- },
28
- })
29
- expect(form.query('object').get('value')).toEqual(123)
30
- })
31
-
32
- test('clearFormGraph', () => {
33
- const form = attach(createForm())
34
- attach(form.createField({ name: 'normal' }))
35
- attach(form.createArrayField({ name: 'array' }))
36
- attach(form.createObjectField({ name: 'object' }))
37
-
38
- form.clearFormGraph('normal')
39
- expect(form.fields['normal']).toBeUndefined()
40
- expect(form.fields['array']).not.toBeUndefined()
41
- })
42
-
43
- test('setGraph 复用已存在字段并还原表单状态', () => {
44
- const form = attach(createForm())
45
- const field = attach(form.createField({ name: 'normal' }))
46
-
47
- form.setFormGraph({
48
- '': { displayName: 'Form', display: 'hidden', pattern: 'readOnly' } as any,
49
- normal: { displayName: 'Field', title: '已存在', display: 'none' } as any,
50
- })
51
- expect(form.display).toEqual('hidden')
52
- expect(form.pattern).toEqual('readOnly')
53
- expect(form.fields['normal']).toBe(field)
54
- expect(field.title).toEqual('已存在')
55
- expect(field.display).toEqual('none')
56
- })
@@ -1,60 +0,0 @@
1
- import { expect, test, vi } from 'vitest'
2
-
3
- import { Heart, LifeCycle } from '../models'
4
-
5
- test('buildLifeCycles 忽略非 LifeCycle 项', () => {
6
- const heart = new Heart({
7
- lifecycles: [{} as any, [{}], 123],
8
- })
9
- expect(heart.lifecycles.length).toEqual(0)
10
- })
11
-
12
- test('clear heart', () => {
13
- const handler = vi.fn()
14
- const heart = new Heart({
15
- lifecycles: [new LifeCycle('event', handler)],
16
- })
17
- heart.publish('event')
18
- expect(handler).toHaveBeenCalledTimes(1)
19
- heart.clear()
20
- heart.publish('event')
21
- expect(handler).toHaveBeenCalledTimes(1)
22
- // 非字符串事件类型直接忽略
23
- heart.publish({})
24
- })
25
-
26
- test('set lifecycles', () => {
27
- const handler = vi.fn()
28
- const heart = new Heart()
29
- heart.setLifeCycles([new LifeCycle('event', handler)])
30
- heart.publish('event')
31
- expect(handler).toHaveBeenCalledTimes(1)
32
- heart.setLifeCycles()
33
- expect(heart.lifecycles).toEqual([])
34
- })
35
-
36
- test('add/remove lifecycle', () => {
37
- const handler = vi.fn()
38
- const heart = new Heart()
39
- heart.addLifeCycles('xxx', [new LifeCycle('event', handler)])
40
- heart.addLifeCycles('yyy')
41
- expect(heart.hasLifeCycles('xxx')).toBeTruthy()
42
- expect(heart.hasLifeCycles('yyy')).toBeFalsy()
43
-
44
- heart.publish('event')
45
- expect(handler).toHaveBeenCalledTimes(1)
46
-
47
- heart.removeLifeCycles('xxx')
48
- expect(heart.hasLifeCycles('xxx')).toBeFalsy()
49
- heart.publish('event')
50
- expect(handler).toHaveBeenCalledTimes(1)
51
- })
52
-
53
- test('publish 使用 context 中的订阅者', () => {
54
- const subscriber = vi.fn()
55
- const context = {}
56
- const heart = new Heart({ context })
57
- heart.subscribe(subscriber)
58
- heart.publish('event', { a: 1 })
59
- expect(subscriber).toHaveBeenCalledWith({ type: 'event', payload: { a: 1 } })
60
- })
@@ -1,138 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { createForm } from '..'
4
- import {
5
- allowAssignDefaultValue,
6
- deserialize,
7
- getTypedDefaultValue,
8
- getValuesFromEvent,
9
- isHTMLInputEvent,
10
- matchFeedback,
11
- patchFieldStates,
12
- serialize,
13
- } from '../shared/internals'
14
- import { attach } from './shared'
15
-
16
- test('getValuesFromEvent', () => {
17
- expect(getValuesFromEvent([{ target: { value: 123 } }])).toEqual([123])
18
- expect(getValuesFromEvent([{ target: { checked: true } }])).toEqual([true])
19
- expect(getValuesFromEvent([{ target: {} }])).toEqual([undefined])
20
- expect(getValuesFromEvent([{ target: null }])).toEqual([{ target: null }])
21
- expect(getValuesFromEvent([123])).toEqual([123])
22
- expect(getValuesFromEvent([null])).toEqual([null])
23
- })
24
-
25
- test('matchFeedback', () => {
26
- expect(matchFeedback()).toBeFalsy()
27
- expect(matchFeedback({ type: 'error' })).toBeFalsy()
28
- expect(matchFeedback({ type: 'error' }, { type: 'error' })).toBeTruthy()
29
- expect(matchFeedback({ type: 'error' }, { type: 'warning' })).toBeFalsy()
30
- expect(matchFeedback({ code: 'A' }, { code: 'A' })).toBeTruthy()
31
- expect(matchFeedback({ code: 'A' }, { code: 'B' })).toBeFalsy()
32
- expect(matchFeedback({ triggerType: 'onInput' }, { triggerType: 'onInput' })).toBeTruthy()
33
- expect(matchFeedback({ triggerType: 'onInput' }, { triggerType: 'onBlur' })).toBeFalsy()
34
- expect(matchFeedback({ address: 'aa', path: 'aa' }, { address: 'aa', path: 'aa' })).toBeTruthy()
35
- expect(matchFeedback({ address: 'aa.*' }, { address: 'aa.bb' })).toBeTruthy()
36
- })
37
-
38
- test('patchFieldStates 忽略未知类型与空 payload', () => {
39
- const fields = {}
40
- patchFieldStates(fields, [{ type: 'update', address: 'aaa', payload: undefined }])
41
- patchFieldStates(fields, [{ type: 'update3' as any, address: 'aaa', payload: undefined }])
42
- expect(fields).toEqual({})
43
- })
44
-
45
- test('patchFieldStates update 会迁移字段并清理旧地址', () => {
46
- const form = attach(createForm())
47
- const field = attach(form.createField({ name: 'field1' })!)
48
-
49
- patchFieldStates(form.fields, [
50
- {
51
- type: 'update',
52
- address: 'field2',
53
- oldAddress: 'field1',
54
- payload: field,
55
- },
56
- ])
57
-
58
- expect(Object.keys(form.fields)).toEqual(['field2'])
59
- expect(form.fields['field2']).toBe(field)
60
- expect(field.address.toString()).toEqual('field2')
61
- })
62
-
63
- test('patchFieldStates remove 销毁字段但保留表单值', () => {
64
- const form = attach(createForm({ values: { aa: 123 } }))
65
- attach(form.createField({ name: 'aa' }))
66
-
67
- patchFieldStates(form.fields, [{ type: 'remove', address: 'aa' }])
68
-
69
- expect(form.fields['aa']).toBeUndefined()
70
- expect(form.values.aa).toEqual(123)
71
- })
72
-
73
- test('deserialize', () => {
74
- expect(deserialize(null, null)).toBeUndefined()
75
- expect(deserialize({}, { parent: null })).toEqual({})
76
- expect(deserialize({}, { address: 'aa' })).toEqual({})
77
-
78
- const model: any = { a: 1 }
79
- expect(deserialize(model, { a: 2 })).toEqual({ a: 2 })
80
- expect(model.a).toEqual(2)
81
-
82
- // 函数型 setter 直接调用
83
- function fn(target: any) {
84
- target.b = 3
85
- }
86
- expect(deserialize(model, fn)).toEqual({ a: 2, b: 3 })
87
-
88
- // 互斥属性:显式传入无效 selfPattern 时 pattern 被跳过,selfPattern 生效
89
- const form = attach(createForm())
90
- const field = attach(form.createField({ name: 'aa', pattern: 'readOnly' })!)
91
- deserialize(field, { selfPattern: undefined, pattern: 'disabled' })
92
- expect(field.pattern).toEqual('editable')
93
- })
94
-
95
- test('serialize', () => {
96
- const form = attach(createForm({ values: { aa: 1 } }))
97
- const field = attach(form.createField({ name: 'aa', title: 'AA' })!)
98
-
99
- const state = serialize(field)
100
- expect(state.address).toEqual('aa')
101
- expect(state.path).toEqual('aa')
102
- expect(state.title).toEqual('AA')
103
- expect(state.form).toBeUndefined()
104
- expect(state.setValue).toBeUndefined()
105
-
106
- expect(serialize(field, (target: any) => target.title)).toEqual('AA')
107
- })
108
-
109
- test('isHTMLInputEvent', () => {
110
- expect(isHTMLInputEvent({ target: { checked: true } })).toBeTruthy()
111
- expect(isHTMLInputEvent({ target: { value: 123 } })).toBeTruthy()
112
- expect(isHTMLInputEvent({ target: { tagName: 'INPUT', value: null } })).toBeTruthy()
113
- expect(isHTMLInputEvent({ target: { tagName: 'INPUT' } })).toBeFalsy()
114
- expect(isHTMLInputEvent({ target: { tagName: 'DIV' } })).toBeFalsy()
115
- expect(isHTMLInputEvent({ target: {}, stopPropagation() {} })).toBeFalsy()
116
- expect(isHTMLInputEvent({})).toBeFalsy()
117
- })
118
-
119
- test('allowAssignDefaultValue', () => {
120
- expect(allowAssignDefaultValue(undefined, undefined)).toBeFalsy()
121
- expect(allowAssignDefaultValue(undefined, 1)).toBeTruthy()
122
- expect(allowAssignDefaultValue('', '')).toBeFalsy()
123
- expect(allowAssignDefaultValue(0, 0)).toBeFalsy()
124
- expect(allowAssignDefaultValue([], [1])).toBeTruthy()
125
- expect(allowAssignDefaultValue([1], [2])).toBeFalsy()
126
- expect(allowAssignDefaultValue({}, { a: 1 })).toBeTruthy()
127
- expect(allowAssignDefaultValue({ a: 1 }, { a: 2 })).toBeFalsy()
128
- })
129
-
130
- test('getTypedDefaultValue', () => {
131
- const form = attach(createForm())
132
- const normal = attach(form.createField({ name: 'normal' })!)
133
- const array = attach(form.createArrayField({ name: 'array' })!)
134
- const object = attach(form.createObjectField({ name: 'object' })!)
135
- expect(getTypedDefaultValue(normal)).toBeUndefined()
136
- expect(getTypedDefaultValue(array)).toEqual([])
137
- expect(getTypedDefaultValue(object)).toEqual({})
138
- })
@@ -1,51 +0,0 @@
1
- import { expect, test, vi } from 'vitest'
2
-
3
- import { LifeCycle } from '../models'
4
-
5
- test('create lifecycle', () => {
6
- const handler1 = vi.fn()
7
- const lifecycle1 = new LifeCycle(handler1)
8
- lifecycle1.notify('event1')
9
- expect(handler1).toHaveBeenCalledTimes(1)
10
- expect(handler1).toHaveBeenCalledWith({ type: 'event1', payload: undefined }, undefined)
11
-
12
- lifecycle1.notify('event11', 'payload1')
13
- expect(handler1).toHaveBeenCalledTimes(2)
14
- expect(handler1).toHaveBeenCalledWith({ type: 'event11', payload: 'payload1' }, undefined)
15
-
16
- const context: any = {}
17
- lifecycle1.notify('event12', 'payload11', context)
18
- expect(handler1).toHaveBeenCalledTimes(3)
19
- expect(handler1).toHaveBeenCalledWith({ type: 'event12', payload: 'payload11' }, context)
20
-
21
- const handler2 = vi.fn()
22
- const lifecycle2 = new LifeCycle('event2', handler2)
23
- lifecycle2.notify('event1')
24
- expect(handler2).not.toHaveBeenCalled()
25
- lifecycle2.notify('event2')
26
- expect(handler2).toHaveBeenCalledTimes(1)
27
-
28
- const handler31 = vi.fn()
29
- const handler32 = vi.fn()
30
- const lifecycle3 = new LifeCycle({
31
- event31: handler31,
32
- event32: handler32,
33
- })
34
- lifecycle3.notify('event3')
35
- expect(handler31).not.toHaveBeenCalled()
36
- expect(handler32).not.toHaveBeenCalled()
37
- lifecycle3.notify('event31')
38
- expect(handler31).toHaveBeenCalledTimes(1)
39
- expect(handler32).not.toHaveBeenCalled()
40
- lifecycle3.notify('event32')
41
- expect(handler31).toHaveBeenCalledTimes(1)
42
- expect(handler32).toHaveBeenCalledTimes(1)
43
- })
44
-
45
- test('notify 非字符串类型不触发 handler', () => {
46
- const handler = vi.fn()
47
- const lifecycle = new LifeCycle(handler)
48
- lifecycle.notify(123)
49
- lifecycle.notify({})
50
- expect(handler).not.toHaveBeenCalled()
51
- })
@@ -1,107 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { createForm, isObjectField } from '..'
4
- import { attach } from './shared'
5
-
6
- test('创建对象字段', () => {
7
- const form = attach(createForm())
8
- const field = attach(form.createObjectField({ name: 'object' })!)
9
- expect(isObjectField(field)).toBeTruthy()
10
- expect(field.value).toEqual({})
11
- expect(form.values.object).toEqual({})
12
- })
13
-
14
- test('对象字段添加与删除属性', async () => {
15
- const form = attach(createForm())
16
- const field = attach(form.createObjectField({ name: 'object' })!)
17
-
18
- await field.addProperty('aa', 123)
19
- expect(field.value?.aa).toEqual(123)
20
- expect(field.existProperty('aa')).toBeTruthy()
21
-
22
- await field.addProperty('bb', '456')
23
- expect(field.value).toEqual({ aa: 123, bb: '456' })
24
-
25
- await field.removeProperty('aa')
26
- expect(field.value).toEqual({ bb: '456' })
27
- expect(field.existProperty('aa')).toBeFalsy()
28
- })
29
-
30
- test('对象字段的子字段路径与记录', () => {
31
- const form = attach(createForm())
32
- const object = attach(form.createObjectField({ name: 'object' })!)
33
- const child = attach(form.createField({ name: 'aa', basePath: 'object' })!)
34
-
35
- expect(child.address.toString()).toEqual('object.aa')
36
- expect(child.path.toString()).toEqual('object.aa')
37
- expect(child.parent).toBe(object)
38
- expect(child.record).toEqual({})
39
- })
40
-
41
- test('重置对象字段', async () => {
42
- const form = attach(createForm())
43
- const field = attach(form.createObjectField({ name: 'object', initialValue: { aa: 1 } })!)
44
-
45
- expect(field.value).toEqual({ aa: 1 })
46
- await field.addProperty('bb', 2)
47
- expect(field.value).toEqual({ aa: 1, bb: 2 })
48
-
49
- await field.reset()
50
- expect(field.value).toEqual({ aa: 1 })
51
- })
52
-
53
- test('删除对象属性时自动清理对应子字段', () => {
54
- const form = attach(createForm())
55
- const object = attach(form.createObjectField({ name: 'obj', initialValue: { aa: 'aa', bb: 'bb' } })!)
56
- attach(form.createField({ name: 'aa', basePath: 'obj' }))
57
- attach(form.createField({ name: 'bb', basePath: 'obj' }))
58
-
59
- object.addProperty('cc', '123')
60
- attach(form.createField({ name: 'cc', basePath: 'obj' }))
61
- expect(form.fields['obj.cc']).not.toBeUndefined()
62
- expect(object.existProperty('cc')).toBeTruthy()
63
-
64
- object.removeProperty('cc')
65
- expect(object.existProperty('cc')).toBeFalsy()
66
- expect(form.fields['obj.cc']).toBeUndefined()
67
- // 其余子字段不受影响
68
- expect(form.fields['obj.aa']).not.toBeUndefined()
69
- expect(form.fields['obj.bb']).not.toBeUndefined()
70
- })
71
-
72
- test('整体替换对象值时清理已消失的附加属性子字段', () => {
73
- const form = attach(createForm())
74
- const object = attach(form.createObjectField({ name: 'obj', initialValue: { aa: 'aa' } })!)
75
- attach(form.createField({ name: 'aa', basePath: 'obj' }))
76
-
77
- object.addProperty('cc', 'cc')
78
- attach(form.createField({ name: 'cc', basePath: 'obj' }))
79
- expect(form.fields['obj.cc']).not.toBeUndefined()
80
-
81
- object.setValue({ aa: 'aa' })
82
- expect(object.value).toEqual({ aa: 'aa' })
83
- expect(form.fields['obj.cc']).toBeUndefined()
84
- expect(form.fields['obj.aa']).not.toBeUndefined()
85
- })
86
-
87
- test('删除不存在的属性不抛错', async () => {
88
- const form = attach(createForm())
89
- const field = attach(form.createObjectField({ name: 'obj', value: { aa: 1 } })!)
90
-
91
- expect(field.existProperty('not-exist')).toBeFalsy()
92
- await field.removeProperty('not-exist')
93
- expect(field.value).toEqual({ aa: 1 })
94
- })
95
-
96
- test('子字段类型变化时补充默认值', () => {
97
- const form = attach(createForm())
98
- const object = attach(form.createObjectField({ name: 'obj' })!)
99
- const child = attach(form.createField({ name: 'aa', basePath: 'obj' })!)
100
-
101
- expect(child.value).toBeUndefined()
102
- object.setValue({ aa: '123' })
103
- expect(child.value).toEqual('123')
104
-
105
- object.setValue({})
106
- expect(child.value).toBeUndefined()
107
- })
@@ -1,100 +0,0 @@
1
- import { expect, test } from 'vitest'
2
-
3
- import { createForm } from '..'
4
- import { attach } from './shared'
5
-
6
- function createQueryForm() {
7
- const form = attach(createForm())
8
- const object = attach(form.createObjectField({ name: 'object' })!)
9
- const voidField = attach(form.createVoidField({ name: 'void', basePath: 'object' })!)
10
- const normal = attach(form.createField({ name: 'normal', basePath: 'object.void' })!)
11
- const array = attach(form.createArrayField({ name: 'array' })!)
12
- return { form, object, voidField, normal, array }
13
- }
14
-
15
- test('绝对路径匹配', () => {
16
- const { form } = createQueryForm()
17
- expect(form.query('object').take()).not.toBeUndefined()
18
- expect(form.query('object.void').take()).not.toBeUndefined()
19
- expect(form.query('object.void.normal').take()).not.toBeUndefined()
20
- expect(form.query('object.normal').take()).not.toBeUndefined()
21
- expect(form.query('array').take()).not.toBeUndefined()
22
- expect(form.query('*').take()).not.toBeUndefined()
23
- expect(form.query('*(oo)').take()).toBeUndefined()
24
- expect(form.query('*(oo)').map()).toEqual([])
25
- })
26
-
27
- test('通配符匹配', () => {
28
- const { form } = createQueryForm()
29
- expect(form.query('object.*').map(field => field.path.toString())).toEqual(['object.void', 'object.normal'])
30
- expect(form.query('*').map(field => field.path.toString())).toEqual([
31
- 'object',
32
- 'object.void',
33
- 'object.normal',
34
- 'array',
35
- ])
36
- })
37
-
38
- test('相对路径匹配', () => {
39
- const { object, normal } = createQueryForm()
40
- expect(object.query('*').map(field => field.path.toString())).toContain('object.normal')
41
- expect(normal.query('*').map(field => field.address.toString())).toContain('object.void.normal')
42
-
43
- // 跨 void 包装的相对查询
44
- const form = attach(createForm())
45
- attach(form.createVoidField({ name: 'void' }))
46
- const aa = attach(form.createField({ name: 'aa', basePath: 'void' })!)
47
- attach(form.createVoidField({ name: 'mm' }))
48
- const bb = attach(form.createField({ name: 'bb', basePath: 'mm' })!)
49
- expect(bb.query('.aa').take()).toBe(aa)
50
- })
51
-
52
- test('take/map/forEach/reduce 的取值与遍历', () => {
53
- const { form } = createQueryForm()
54
- const query = form.query('object.*')
55
-
56
- expect(query.take(field => field.address.toString())).toEqual('object.void')
57
- expect(query.map(field => field.address.toString())).toEqual(['object.void', 'object.void.normal'])
58
-
59
- const visited: string[] = []
60
- query.forEach(field => {
61
- visited.push(field.address.toString())
62
- })
63
- expect(visited).toEqual(['object.void', 'object.void.normal'])
64
-
65
- expect(query.reduce((count, field) => (field ? count + 1 : count), 0)).toEqual(2)
66
-
67
- // 未命中的查询返回空结果
68
- expect(form.query('not-exist').take()).toBeUndefined()
69
- expect(form.query('not-exist').map()).toEqual([])
70
- })
71
-
72
- test('get/getIn/value/initialValue', () => {
73
- const { form } = createQueryForm()
74
- expect(form.query('object.void').get('value')).toBeUndefined()
75
- expect(form.query('object.void').get('inputValue')).toBeUndefined()
76
- expect(form.query('array').get('value')).toEqual([])
77
- expect(form.query('array').get('initialValue')).toBeUndefined()
78
-
79
- form.setFieldState('array', state => {
80
- state.value = [111]
81
- state.initialValue = [111]
82
- state.inputValue = [111]
83
- })
84
- expect(form.query('array').get('value')).toEqual([111])
85
- expect(form.query('array').get('initialValue')).toEqual([111])
86
- expect(form.query('array').get('inputValue')).toEqual([111])
87
- expect(form.query('array').getIn('inputValue')).toEqual([111])
88
- expect(form.query('array').value()).toEqual([111])
89
- expect(form.query('array').initialValue()).toEqual([111])
90
-
91
- expect(form.query('opo').get('value')).toBeUndefined()
92
- expect(form.query('opo').getIn('value')).toBeUndefined()
93
- })
94
-
95
- test('查询时会清理 fields 中的空值', () => {
96
- const { form } = createQueryForm()
97
- form.fields['stale'] = undefined as any
98
- form.query('*')
99
- expect('stale' in form.fields).toBeFalsy()
100
- })
@@ -1,10 +0,0 @@
1
- export function attach<T extends { onMount: () => void }>(target: T | undefined): T {
2
- target?.onMount()
3
- return target as T
4
- }
5
-
6
- export function sleep(duration = 100) {
7
- return new Promise(resolve => {
8
- setTimeout(resolve, duration)
9
- })
10
- }
@@ -1,52 +0,0 @@
1
- import { expect, expectTypeOf, test } from 'vitest'
2
-
3
- import type { ArrayField, Field, GeneralField, ObjectField, VoidField } from '..'
4
-
5
- interface IUser {
6
- name: string
7
- age: number
8
- }
9
-
10
- test('Field 泛型:value / componentProps / decoratorProps', () => {
11
- expectTypeOf<Field<string>['value']>().toEqualTypeOf<string | undefined>()
12
- expectTypeOf<Field<string>['initialValue']>().toEqualTypeOf<string>()
13
- expectTypeOf<Field<string, { placeholder?: string }>['componentProps']>().toEqualTypeOf<{
14
- placeholder?: string
15
- }>()
16
- expectTypeOf<Field<string, any, { label?: string }>['decoratorProps']>().toEqualTypeOf<{ label?: string }>()
17
- })
18
-
19
- test('ArrayField 泛型:value / record / records', () => {
20
- expectTypeOf<ArrayField<IUser[]>['value']>().toEqualTypeOf<IUser[] | undefined>()
21
- expectTypeOf<ArrayField<IUser[], any, any, IUser>['record']>().toEqualTypeOf<IUser>()
22
- expectTypeOf<ArrayField<IUser[], any, any, IUser>['records']>().toEqualTypeOf<IUser[]>()
23
- })
24
-
25
- test('ObjectField 泛型:value', () => {
26
- expectTypeOf<ObjectField<IUser>['value']>().toEqualTypeOf<IUser | undefined>()
27
- })
28
-
29
- test('泛型字段可直接作为 GeneralField 形参', () => {
30
- expectTypeOf<Field<string, { placeholder?: string }>>().toMatchTypeOf<GeneralField>()
31
- expectTypeOf<ArrayField<IUser[]>>().toMatchTypeOf<GeneralField>()
32
- expectTypeOf<ObjectField<IUser>>().toMatchTypeOf<GeneralField>()
33
- expectTypeOf<VoidField<{ label?: string }>>().toMatchTypeOf<GeneralField>()
34
- })
35
-
36
- test('VoidField 没有 value', () => {
37
- const field = {} as VoidField
38
- // @ts-expect-error VoidField 不存在 value
39
- const value = field.value
40
- expect(value).toBeUndefined()
41
- })
42
-
43
- test('setComponentProps 只接受对应 props', () => {
44
- const setComponentProps: Field<string, { placeholder?: string }>['setComponentProps'] = () => {}
45
- setComponentProps({ placeholder: '请输入' })
46
- // @ts-expect-error 未声明的 props
47
- setComponentProps({ wrong: true })
48
- })
49
-
50
- test('类型测试文件可执行', () => {
51
- expect(true).toBe(true)
52
- })