@formvk/vue 0.1.0 → 1.1.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.
@@ -1,230 +0,0 @@
1
- import { onFieldValueChange } from '@formvk/core'
2
- import { mount } from '@vue/test-utils'
3
- import { describe, expect, test, vi } from 'vitest'
4
- import { defineComponent, nextTick, provide, shallowRef } from 'vue'
5
-
6
- import {
7
- FormProvider,
8
- SchemaField,
9
- createForm,
10
- useAttach,
11
- useField,
12
- useFieldSchema,
13
- useForm,
14
- useFormEffects,
15
- useParentForm,
16
- } from '..'
17
- import { FieldSymbol, FormSymbol } from '../shared/context'
18
- import { Input, mountWithForm } from './shared'
19
-
20
- describe('hooks', () => {
21
- test('无 Provider 时 useForm / useField / useFieldSchema 返回空 ref 且不报错', () => {
22
- const Probe = defineComponent({
23
- name: 'EmptyProbe',
24
- setup() {
25
- const form = useForm()
26
- const field = useField()
27
- const schema = useFieldSchema()
28
- return () => (
29
- <div class="empty-probe">{`${String(form.value)}-${String(field.value)}-${String(schema.value)}`}</div>
30
- )
31
- },
32
- })
33
-
34
- const wrapper = mount(Probe)
35
- expect(wrapper.find('.empty-probe').text()).toBe('undefined-undefined-undefined')
36
- })
37
-
38
- test('useFieldSchema 在 RecursionField 内拿到当前节点 schema', () => {
39
- const SchemaProbe = defineComponent({
40
- name: 'SchemaProbe',
41
- setup() {
42
- const schemaRef = useFieldSchema()
43
- return () => (
44
- <div class="schema-probe">{`${String(schemaRef.value?.type)}:${String(schemaRef.value?.name)}`}</div>
45
- )
46
- },
47
- })
48
-
49
- const form = createForm()
50
- const wrapper = mountWithForm(form, () => (
51
- <SchemaField
52
- components={{ SchemaProbe }}
53
- schema={{
54
- type: 'object',
55
- properties: {
56
- a: { type: 'string', component: 'SchemaProbe' },
57
- },
58
- }}
59
- />
60
- ))
61
-
62
- expect(wrapper.find('.schema-probe').text()).toBe('string:a')
63
- })
64
-
65
- test('useFormEffects 注册 effects,卸载后移除生命周期', async () => {
66
- let count = 0
67
- const EffectsProbe = defineComponent({
68
- name: 'EffectsProbe',
69
- setup() {
70
- useFormEffects(() => {
71
- onFieldValueChange('*', () => {
72
- count++
73
- })
74
- })
75
- return () => <div class="effects-probe" />
76
- },
77
- })
78
-
79
- const form = createForm()
80
- const wrapper = mountWithForm(form, () => (
81
- <>
82
- <EffectsProbe />
83
- <SchemaField
84
- components={{ Input }}
85
- schema={{
86
- type: 'object',
87
- properties: {
88
- a: { type: 'string', component: 'Input' },
89
- },
90
- }}
91
- />
92
- </>
93
- ))
94
-
95
- form.setValuesIn('a', 'first')
96
- expect(count).toBe(1)
97
-
98
- wrapper.unmount()
99
- form.setValuesIn('a', 'second')
100
- expect(count).toBe(1)
101
- })
102
-
103
- test('form 实例替换后 effects 重新注册到新表单', async () => {
104
- const formA = createForm()
105
- const formB = createForm()
106
- const addA = vi.spyOn(formA, 'addEffects')
107
- const removeA = vi.spyOn(formA, 'removeEffects')
108
- const addB = vi.spyOn(formB, 'addEffects')
109
-
110
- const EffectsProbe = defineComponent({
111
- name: 'EffectsProbe',
112
- setup() {
113
- useFormEffects(() => {
114
- onFieldValueChange('*', () => {})
115
- })
116
- return () => <div class="effects-probe" />
117
- },
118
- })
119
-
120
- const current = shallowRef(formA)
121
- mount(
122
- defineComponent({
123
- setup() {
124
- return () => (
125
- <FormProvider form={current.value}>
126
- <EffectsProbe />
127
- </FormProvider>
128
- )
129
- },
130
- })
131
- )
132
-
133
- expect(addA).toHaveBeenCalledTimes(1)
134
-
135
- current.value = formB
136
- await nextTick()
137
-
138
- expect(removeA).toHaveBeenCalled()
139
- expect(addB).toHaveBeenCalledTimes(1)
140
- })
141
-
142
- test('useParentForm 响应字段引用替换', async () => {
143
- const form = createForm()
144
- const fieldRef = shallowRef<any>()
145
-
146
- const ParentProbe = defineComponent({
147
- name: 'ParentProbe',
148
- setup() {
149
- const parent = useParentForm()
150
- return () => <div class="parent-probe">{parent.value?.displayName}</div>
151
- },
152
- })
153
-
154
- const wrapper = mount(
155
- defineComponent({
156
- setup() {
157
- provide(FormSymbol, shallowRef(form))
158
- provide(FieldSymbol, fieldRef)
159
- return () => <ParentProbe />
160
- },
161
- })
162
- )
163
-
164
- fieldRef.value = form.createObjectField({ name: 'obj' })
165
- await nextTick()
166
- expect(wrapper.find('.parent-probe').text()).toBe('ObjectField')
167
-
168
- fieldRef.value = form.createField({ name: 'plain' })
169
- await nextTick()
170
- expect(wrapper.find('.parent-probe').text()).toBe('Form')
171
- })
172
-
173
- test('useAttach 切换 target 时旧值 onUnmount、新值 onMount', async () => {
174
- const targetA = { onMount: vi.fn(), onUnmount: vi.fn() }
175
- const targetB = { onMount: vi.fn(), onUnmount: vi.fn() }
176
- const targetRef = shallowRef(targetA)
177
-
178
- const AttachProbe = defineComponent({
179
- name: 'AttachProbe',
180
- setup() {
181
- useAttach(targetRef)
182
- return () => <div class="attach-probe" />
183
- },
184
- })
185
-
186
- const wrapper = mount(AttachProbe)
187
- expect(targetA.onMount).toHaveBeenCalledTimes(1)
188
-
189
- targetRef.value = targetB
190
- await nextTick()
191
- await nextTick()
192
-
193
- expect(targetA.onUnmount).toHaveBeenCalledTimes(1)
194
- expect(targetB.onMount).toHaveBeenCalledTimes(1)
195
-
196
- wrapper.unmount()
197
- expect(targetB.onUnmount).toHaveBeenCalled()
198
- })
199
-
200
- test('useAttach 目标置空时不报错', async () => {
201
- const targetRef = shallowRef<any>({ onMount: vi.fn(), onUnmount: vi.fn() })
202
- const DetachProbe = defineComponent({
203
- name: 'DetachProbe',
204
- setup() {
205
- useAttach(targetRef)
206
- return () => <div class="detach-probe" />
207
- },
208
- })
209
-
210
- const wrapper = mount(DetachProbe)
211
- targetRef.value = undefined
212
- await nextTick()
213
-
214
- expect(targetRef.value).toBeUndefined()
215
- wrapper.unmount()
216
- })
217
-
218
- test('useFormEffects 无表单上下文时安全跳过', () => {
219
- const EmptyEffectsProbe = defineComponent({
220
- name: 'EmptyEffectsProbe',
221
- setup() {
222
- useFormEffects(() => {})
223
- return () => <div class="empty-effects" />
224
- },
225
- })
226
-
227
- const wrapper = mount(EmptyEffectsProbe)
228
- expect(wrapper.find('.empty-effects').exists()).toBe(true)
229
- })
230
- })