@formvk/vue 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.
@@ -1,694 +0,0 @@
1
- import { mount } from '@vue/test-utils'
2
- import { describe, expect, test, vi } from 'vitest'
3
- import { defineComponent, h, nextTick } from 'vue'
4
-
5
- import { FormProvider, SchemaField, connect, createForm, mapProps, mapReadPretty } from '..'
6
- import { ReactiveField } from '../components/ReactiveField'
7
- import { Container, Decorator, Input, mountWithForm } from './shared'
8
-
9
- const Text = defineComponent({
10
- name: 'Text',
11
- props: ['value', 'text', 'title'],
12
- setup(props) {
13
- return () => (
14
- <span class="text" data-value={props.value} data-text={props.text}>
15
- {props.title ?? props.value}
16
- </span>
17
- )
18
- },
19
- })
20
-
21
- const ReadPretty = defineComponent({
22
- name: 'ReadPretty',
23
- setup() {
24
- return () => <span class="read-pretty">readPretty</span>
25
- },
26
- })
27
-
28
- const SlotViewer = defineComponent({
29
- name: 'SlotViewer',
30
- setup(_, { slots }) {
31
- return () => (
32
- <div class="slot-viewer">
33
- <div class="named">{slots.label?.()}</div>
34
- <div class="default">{slots.default?.()}</div>
35
- </div>
36
- )
37
- },
38
- })
39
-
40
- const components = { Input, Decorator, Container, SlotViewer }
41
-
42
- describe('schema 驱动的字段渲染', () => {
43
- test('数据字段基础渲染:decorator + component', () => {
44
- const form = createForm({ values: { username: 'init' } })
45
- const wrapper = mountWithForm(form, () => (
46
- <SchemaField
47
- components={components}
48
- schema={{
49
- type: 'object',
50
- properties: {
51
- username: {
52
- type: 'string',
53
- decorator: 'Decorator',
54
- component: 'Input',
55
- },
56
- },
57
- }}
58
- />
59
- ))
60
-
61
- expect(wrapper.find('.decorator').exists()).toBe(true)
62
- expect(wrapper.find('input').element.value).toBe('init')
63
- // 无名 object 根节点用 display: contents 包裹子节点
64
- expect(wrapper.html()).toContain('display: contents')
65
- })
66
-
67
- test('component / decorator 支持字符串解析与组件对象', () => {
68
- const form = createForm()
69
- const wrapper = mountWithForm(form, () => (
70
- <SchemaField
71
- components={components}
72
- schema={{
73
- type: 'object',
74
- properties: {
75
- byName: { type: 'string', decorator: 'Decorator', component: 'Input' },
76
- byObject: { type: 'string', decorator: Decorator, component: Input },
77
- },
78
- }}
79
- />
80
- ))
81
-
82
- expect(wrapper.findAll('.decorator')).toHaveLength(2)
83
- expect(wrapper.findAll('input')).toHaveLength(2)
84
- })
85
-
86
- test('无 decorator 只渲染 component;无 component 时渲染 content', () => {
87
- const form = createForm()
88
- const wrapper = mountWithForm(form, () => (
89
- <SchemaField
90
- components={components}
91
- schema={{
92
- type: 'object',
93
- properties: {
94
- plain: { type: 'string', component: 'Input' },
95
- contentOnly: { type: 'string', component: 'SlotViewer', content: 'hello' },
96
- },
97
- }}
98
- />
99
- ))
100
-
101
- expect(wrapper.find('.decorator').exists()).toBe(false)
102
- expect(wrapper.find('input').exists()).toBe(true)
103
- expect(wrapper.find('.slot-viewer .default').text()).toBe('hello')
104
- })
105
-
106
- test('object / array / void 节点的插槽透传', () => {
107
- const form = createForm()
108
- const wrapper = mountWithForm(form, () => (
109
- <SchemaField
110
- components={components}
111
- schema={{
112
- type: 'object',
113
- properties: {
114
- obj: {
115
- type: 'object',
116
- component: 'Container',
117
- properties: {
118
- a: { type: 'string', component: 'Input' },
119
- },
120
- },
121
- list: {
122
- type: 'array',
123
- component: 'Container',
124
- items: { type: 'string', component: 'Input' },
125
- },
126
- voidField: {
127
- type: 'void',
128
- component: 'Container',
129
- properties: {
130
- b: { type: 'string', component: 'Input' },
131
- },
132
- },
133
- },
134
- }}
135
- />
136
- ))
137
-
138
- const containers = wrapper.findAll('.container')
139
- expect(containers).toHaveLength(3)
140
- expect(containers[0].find('input').exists()).toBe(true)
141
- expect(containers[2].find('input').exists()).toBe(true)
142
- expect((form.fields['list'] as any).displayName).toBe('ArrayField')
143
- })
144
-
145
- test('value 受控:字段值与输入事件双向同步', async () => {
146
- const form = createForm<{ username?: string }>({ values: { username: 'init' } })
147
- const wrapper = mountWithForm(form, () => (
148
- <SchemaField
149
- components={components}
150
- schema={{
151
- type: 'object',
152
- properties: {
153
- username: { type: 'string', component: 'Input' },
154
- },
155
- }}
156
- />
157
- ))
158
-
159
- const input = wrapper.find('input')
160
- expect(input.element.value).toBe('init')
161
-
162
- await input.setValue('changed')
163
- expect(form.values.username).toBe('changed')
164
-
165
- form.setValuesIn('username', 'remote')
166
- await nextTick()
167
- expect(input.element.value).toBe('remote')
168
- })
169
-
170
- test('componentProps 中 onChange / @change 都驱动字段,@change 优先', async () => {
171
- const onChange = vi.fn()
172
- const atChange = vi.fn()
173
- const form = createForm<{ username?: string; nickname?: string }>()
174
- const wrapper = mountWithForm(form, () => (
175
- <SchemaField
176
- components={components}
177
- schema={{
178
- type: 'object',
179
- properties: {
180
- username: {
181
- type: 'string',
182
- component: 'Input',
183
- componentProps: { onChange },
184
- },
185
- nickname: {
186
- type: 'string',
187
- component: 'Input',
188
- componentProps: { onChange, '@change': atChange },
189
- },
190
- },
191
- }}
192
- />
193
- ))
194
-
195
- const inputs = wrapper.findAll('input')
196
- await inputs[0].setValue('a')
197
- expect(form.values.username).toBe('a')
198
- expect(onChange).toHaveBeenCalled()
199
-
200
- await inputs[1].setValue('b')
201
- expect(form.values.nickname).toBe('b')
202
- expect(atChange).toHaveBeenCalled()
203
- })
204
-
205
- test('onFocus / onBlur 触发字段 focus / blur', async () => {
206
- const form = createForm()
207
- const wrapper = mountWithForm(form, () => (
208
- <SchemaField
209
- components={components}
210
- schema={{
211
- type: 'object',
212
- properties: {
213
- username: { type: 'string', component: 'Input' },
214
- },
215
- }}
216
- />
217
- ))
218
-
219
- const field = form.fields['username'] as any
220
- await wrapper.find('input').trigger('focus')
221
- expect(field.active).toBe(true)
222
-
223
- await wrapper.find('input').trigger('blur')
224
- expect(field.active).toBe(false)
225
- })
226
-
227
- test('pattern 驱动 disabled / readOnly', async () => {
228
- const form = createForm()
229
- const wrapper = mountWithForm(form, () => (
230
- <SchemaField
231
- components={components}
232
- schema={{
233
- type: 'object',
234
- properties: {
235
- username: { type: 'string', component: 'Input' },
236
- },
237
- }}
238
- />
239
- ))
240
-
241
- form.setPattern('disabled')
242
- await nextTick()
243
- expect(wrapper.find('input').element.hasAttribute('disabled')).toBe(true)
244
-
245
- form.setPattern('readOnly')
246
- await nextTick()
247
- expect(wrapper.find('input').element.hasAttribute('readonly')).toBe(true)
248
- expect(wrapper.find('input').element.hasAttribute('disabled')).toBe(false)
249
-
250
- form.setPattern('readPretty')
251
- await nextTick()
252
- expect(wrapper.find('input').element.hasAttribute('disabled')).toBe(true)
253
- })
254
-
255
- test('display 从 none 切到 visible 时重新渲染', async () => {
256
- const form = createForm()
257
- const wrapper = mountWithForm(form, () => (
258
- <SchemaField
259
- components={components}
260
- schema={{
261
- type: 'object',
262
- properties: {
263
- username: { type: 'string', component: 'Input', display: 'none' },
264
- },
265
- }}
266
- />
267
- ))
268
-
269
- expect(wrapper.find('input').exists()).toBe(false)
270
-
271
- form.setFieldState('username', state => {
272
- state.display = 'visible'
273
- })
274
- await nextTick()
275
- expect(wrapper.find('input').exists()).toBe(true)
276
- })
277
-
278
- test('content 支持字符串、具名插槽对象、VNode', () => {
279
- const form = createForm()
280
- const wrapper = mountWithForm(form, () => (
281
- <SchemaField
282
- components={components}
283
- schema={{
284
- type: 'object',
285
- properties: {
286
- text: { type: 'string', component: 'SlotViewer', content: 'plain' },
287
- named: {
288
- type: 'string',
289
- component: 'SlotViewer',
290
- content: { label: 'LBL', default: 'DEF' },
291
- },
292
- vnode: {
293
- type: 'string',
294
- component: 'SlotViewer',
295
- content: <span class="vnode-content">V</span>,
296
- },
297
- },
298
- }}
299
- />
300
- ))
301
-
302
- const viewers = wrapper.findAll('.slot-viewer')
303
- expect(viewers[0].find('.default').text()).toBe('plain')
304
- expect(viewers[1].find('.named').text()).toBe('LBL')
305
- expect(viewers[1].find('.default').text()).toBe('DEF')
306
- expect(viewers[2].find('.vnode-content').text()).toBe('V')
307
- })
308
-
309
- test('connect + mapProps(含 value 删除分支)与 mapReadPretty', async () => {
310
- const Connected = connect(Text, mapProps({ value: 'value', title: 'title' }), mapReadPretty(ReadPretty))
311
- const ValueMapped = connect(Text, mapProps({ value: 'text' }))
312
-
313
- const form = createForm({ values: { mapped: 'mapped-value' } })
314
- const wrapper = mountWithForm(form, () => (
315
- <SchemaField
316
- components={{ Connected, ValueMapped }}
317
- schema={{
318
- type: 'object',
319
- properties: {
320
- mapped: {
321
- type: 'string',
322
- title: 'Title',
323
- component: 'Connected',
324
- },
325
- deleted: {
326
- type: 'string',
327
- component: 'ValueMapped',
328
- },
329
- },
330
- }}
331
- />
332
- ))
333
-
334
- const mapped = wrapper.find('.text')
335
- expect(mapped.text()).toBe('Title')
336
- expect(mapped.attributes('data-value')).toBe('mapped-value')
337
-
338
- form.setPattern('readPretty')
339
- await nextTick()
340
- expect(wrapper.find('.read-pretty').exists()).toBe(true)
341
-
342
- form.setPattern('editable')
343
- await nextTick()
344
- const valueMapped = wrapper.findAll('.text').at(-1)!
345
- expect(valueMapped.attributes('data-text')).toBeUndefined()
346
- expect(valueMapped.attributes('data-value')).toBeUndefined()
347
- })
348
-
349
- test('schema 变化后创建新字段并卸载旧字段', async () => {
350
- const form = createForm()
351
- let schema: any = {
352
- type: 'object',
353
- properties: {
354
- a: { type: 'string', component: 'Input' },
355
- },
356
- }
357
- const wrapper = mountWithForm(form, () => <SchemaField components={components} schema={schema} />)
358
-
359
- const first = form.fields['a'] as any
360
- expect(first.mounted).toBe(true)
361
-
362
- schema = {
363
- type: 'object',
364
- properties: {
365
- b: { type: 'string', component: 'Input' },
366
- },
367
- }
368
- // 触发挂载组件的重渲染
369
- wrapper.vm.$forceUpdate()
370
- await nextTick()
371
- await nextTick()
372
-
373
- expect(first.unmounted).toBe(true)
374
- expect((form.fields['b'] as any)?.displayName).toBe('Field')
375
- expect(wrapper.find('input').element.getAttribute('data-testid')).toBe('b')
376
- })
377
- })
378
-
379
- describe('边界与降级渲染', () => {
380
- test('content 的空值与数字内容不渲染', () => {
381
- const form = createForm()
382
- const wrapper = mountWithForm(form, () => (
383
- <SchemaField
384
- components={components}
385
- schema={{
386
- type: 'object',
387
- properties: {
388
- partial: {
389
- type: 'string',
390
- component: 'SlotViewer',
391
- content: { label: null, default: 5 } as any,
392
- },
393
- numeric: { type: 'string', component: 'SlotViewer', content: 123 as any },
394
- },
395
- }}
396
- />
397
- ))
398
-
399
- const viewers = wrapper.findAll('.slot-viewer')
400
- expect(viewers[0].find('.named').text()).toBe('')
401
- expect(viewers[0].find('.default').text()).toBe('5')
402
- expect(viewers[1].find('.default').text()).toBe('')
403
- })
404
-
405
- test('ReactiveField content 支持函数组件与对象组件', async () => {
406
- const FnContent = () => <span class="direct-fn">fn</span>
407
- const ObjectContent = defineComponent({
408
- setup() {
409
- return () => <span class="direct-object">object</span>
410
- },
411
- })
412
- const Viewer = defineComponent({
413
- setup(_, { slots }) {
414
- return () => <div class="direct-viewer">{slots.default?.()}</div>
415
- },
416
- })
417
- const form = createForm()
418
- const Direct = defineComponent({
419
- setup() {
420
- return () =>
421
- h(FormProvider, { form }, () =>
422
- h(ReactiveField as any, { fieldProps: { name: 'directContent' } }, { default: undefined } as any)
423
- )
424
- },
425
- })
426
- const wrapper = mount(Direct)
427
-
428
- const field = form.fields['directContent'] as any
429
- field.component = Viewer
430
- field.content = FnContent
431
- await nextTick()
432
- expect(wrapper.find('.direct-viewer .direct-fn').text()).toBe('fn')
433
-
434
- field.content = ObjectContent
435
- await nextTick()
436
- expect(wrapper.find('.direct-viewer .direct-object').text()).toBe('object')
437
- })
438
-
439
- test('object 无 component / decorator 且多子节点时以 Fragment 渲染', () => {
440
- const form = createForm()
441
- const wrapper = mountWithForm(form, () => (
442
- <SchemaField
443
- components={components}
444
- schema={{
445
- type: 'object',
446
- properties: {
447
- obj: {
448
- type: 'object',
449
- properties: {
450
- a: { type: 'string', component: 'Input' },
451
- b: { type: 'string', component: 'Input' },
452
- },
453
- },
454
- },
455
- }}
456
- />
457
- ))
458
-
459
- expect(wrapper.findAll('input')).toHaveLength(2)
460
- expect((form.fields['obj'] as any).displayName).toBe('ObjectField')
461
- })
462
-
463
- test('object 无子属性时不渲染内容', () => {
464
- const form = createForm()
465
- const wrapper = mountWithForm(form, () => (
466
- <SchemaField components={components} schema={{ type: 'object', properties: { blank: { type: 'object' } } }} />
467
- ))
468
-
469
- expect(wrapper.find('input').exists()).toBe(false)
470
- expect((form.fields['blank'] as any).displayName).toBe('ObjectField')
471
- })
472
-
473
- test('object 无子属性且 content 默认插槽为空时渲染为空', () => {
474
- const form = createForm()
475
- const wrapper = mountWithForm(form, () => (
476
- <SchemaField
477
- components={components}
478
- schema={{
479
- type: 'object',
480
- properties: {
481
- blank: { type: 'object', content: { default: [] } as any },
482
- },
483
- }}
484
- />
485
- ))
486
-
487
- expect(wrapper.find('input').exists()).toBe(false)
488
- expect(wrapper.find('.slot-viewer').exists()).toBe(false)
489
- })
490
-
491
- test('未注册的组件名按原生标签渲染', () => {
492
- const form = createForm()
493
- const wrapper = mountWithForm(form, () => (
494
- <SchemaField
495
- components={components}
496
- schema={{
497
- type: 'object',
498
- properties: {
499
- native: { type: 'string', component: 'section', content: 'native-text' },
500
- },
501
- }}
502
- />
503
- ))
504
-
505
- expect(wrapper.find('section').text()).toBe('native-text')
506
- })
507
-
508
- test('object 同时配置 content 与子属性时合并插槽内容', () => {
509
- const form = createForm()
510
- const wrapper = mountWithForm(form, () => (
511
- <SchemaField
512
- components={components}
513
- schema={{
514
- type: 'object',
515
- properties: {
516
- box: {
517
- type: 'object',
518
- component: 'Container',
519
- content: 'extra',
520
- properties: {
521
- a: { type: 'string', component: 'Input' },
522
- },
523
- },
524
- },
525
- }}
526
- />
527
- ))
528
-
529
- expect(wrapper.find('.container input').exists()).toBe(true)
530
- expect(wrapper.find('.container').text()).toContain('extra')
531
- })
532
-
533
- test('decoratorProps / componentProps 被清空后仍可渲染', async () => {
534
- const form = createForm()
535
- const wrapper = mountWithForm(form, () => (
536
- <SchemaField
537
- components={components}
538
- schema={{
539
- type: 'object',
540
- properties: {
541
- a: {
542
- type: 'string',
543
- decorator: 'Decorator',
544
- decoratorProps: { label: 'L' },
545
- component: 'Input',
546
- componentProps: { placeholder: 'P' },
547
- },
548
- },
549
- }}
550
- />
551
- ))
552
-
553
- expect(wrapper.find('input').attributes('placeholder')).toBe('P')
554
-
555
- const field = form.fields['a'] as any
556
- field.decoratorProps = undefined
557
- field.componentProps = undefined
558
- await nextTick()
559
-
560
- expect(wrapper.find('.decorator').exists()).toBe(true)
561
- expect(wrapper.find('input').exists()).toBe(true)
562
- expect(wrapper.find('input').attributes('placeholder')).toBeUndefined()
563
- })
564
-
565
- test('void 字段的 component 事件不写入字段状态', async () => {
566
- const onChange = vi.fn()
567
- const onFocus = vi.fn()
568
- const onBlur = vi.fn()
569
- const form = createForm()
570
- const wrapper = mountWithForm(form, () => (
571
- <SchemaField
572
- components={components}
573
- schema={{
574
- type: 'object',
575
- properties: {
576
- v: {
577
- type: 'void',
578
- component: 'Input',
579
- componentProps: { onChange, onFocus, onBlur },
580
- },
581
- },
582
- }}
583
- />
584
- ))
585
-
586
- const input = wrapper.find('input')
587
- await input.trigger('input')
588
- await input.trigger('focus')
589
- await input.trigger('blur')
590
-
591
- expect(onChange).toHaveBeenCalled()
592
- expect(onFocus).toHaveBeenCalled()
593
- expect(onBlur).toHaveBeenCalled()
594
-
595
- const field = form.fields['v'] as any
596
- expect(field.displayName).toBe('VoidField')
597
- expect(field.value).toBeUndefined()
598
- })
599
-
600
- test('同名字段 schema 更新时触发字段重建逻辑', async () => {
601
- const form = createForm()
602
- let schema: any = {
603
- type: 'object',
604
- properties: {
605
- a: { type: 'string', component: 'Input' },
606
- },
607
- }
608
- const wrapper = mountWithForm(form, () => <SchemaField components={components} schema={schema} />)
609
- const first = form.fields['a']
610
-
611
- schema = {
612
- type: 'object',
613
- properties: {
614
- a: { type: 'string', component: 'Input', title: 'updated' },
615
- },
616
- }
617
- wrapper.vm.$forceUpdate()
618
- await nextTick()
619
- await nextTick()
620
-
621
- expect(form.fields['a']).toBe(first)
622
- expect(wrapper.find('input').exists()).toBe(true)
623
- })
624
-
625
- test('ReactiveField 无表单上下文或字段缺失时回退渲染插槽', () => {
626
- const bare = mount(ReactiveField)
627
- expect(bare.findAll('.fallback')).toHaveLength(0)
628
-
629
- const fallback = mount(ReactiveField, {
630
- slots: { default: () => <span class="fallback">fallback</span> },
631
- })
632
- expect(fallback.find('.fallback').exists()).toBe(true)
633
-
634
- const form = createForm()
635
- const Direct = defineComponent({
636
- setup() {
637
- return () =>
638
- h(FormProvider, { form }, () =>
639
- h(ReactiveField as any, { fieldType: null, fieldProps: null } as any, { default: () => undefined })
640
- )
641
- },
642
- })
643
- mount(Direct)
644
- expect(Object.keys(form.fields)).toHaveLength(0)
645
- })
646
-
647
- test('connect / mapProps 支持函数映射、非字符串映射与匿名组件', () => {
648
- const Anon = defineComponent({
649
- props: ['title', 'placeholder'],
650
- setup(props) {
651
- return () => <span class="anon">{`${props.title ?? ''}|${props.placeholder ?? ''}`}</span>
652
- },
653
- })
654
- const FnMapped = connect(
655
- Anon,
656
- mapProps((props: any) => ({ ...props, title: 'fn-title' }))
657
- )
658
- const SkipMapped = connect(Anon, mapProps({ placeholder: 'placeholder' }))
659
- const BoolMapped = connect(Anon, mapProps({ title: false } as any))
660
- // 匿名组件的 mapReadPretty 使用降级组件名
661
- connect(
662
- Anon,
663
- mapReadPretty(() => <span class="read-anon">read</span>)
664
- )
665
-
666
- const form = createForm()
667
- const wrapper = mountWithForm(form, () => (
668
- <SchemaField
669
- components={{ FnMapped, SkipMapped, BoolMapped }}
670
- schema={{
671
- type: 'object',
672
- properties: {
673
- fn: { type: 'string', component: 'FnMapped' },
674
- skip: {
675
- type: 'string',
676
- component: 'SkipMapped',
677
- componentProps: { placeholder: 'from-schema' },
678
- },
679
- bool: { type: 'string', title: 'title-value', component: 'BoolMapped' },
680
- },
681
- }}
682
- />
683
- ))
684
-
685
- const anons = wrapper.findAll('.anon')
686
- expect(anons[0].text()).toBe('fn-title|')
687
- expect(anons[1].text()).toBe('|from-schema')
688
- expect(anons[2].text()).toBe('title-value|')
689
-
690
- const DirectConnected = connect(Anon, mapProps({ title: 'title' }))
691
- const direct = mount(DirectConnected, { props: { title: 'direct-title' } })
692
- expect(direct.find('.anon').text()).toBe('direct-title|')
693
- })
694
- })