Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

fieldListBridge.test.ts

817 lines26.2 KB

1import {
2 InternalFormApi,
3 InternalFormGroupApi,
4 installDevtoolsBridge,
5} from '@tanstack/form-core/internals'
6import { afterEach, describe, expect, it, vi } from 'vitest'
7import { createFieldsController } from '../src/bridge/fields'
8import { createMountedFormsController } from '../src/bridge/forms/mountedForms'
9import { createFormDevtoolsBridge } from '../src/bridge/createBridge'
10import { formDevtoolsEventClient } from '../src/eventClient.lib'
11import { connectTestEventBus } from './testEventBus'
12import { setFieldValidatorErrors } from './testUtils'
13import type { FormDevtoolsEventMap } from '../src/eventClientTypes'
14​
15type FieldListSnapshot = FormDevtoolsEventMap['field-list-snapshot']
16type FieldListPatch = FormDevtoolsEventMap['field-list-patch']
17​
18const flushPatches = () => Promise.resolve()
19​
20afterEach(() => {
21 vi.useRealTimers()
22})
23​
24describe('field list bridge', () => {
25 it('uses snapshots for recovery and sparse patches for live meta', async () => {
26 const disconnectEventBus = connectTestEventBus()
27 const mountedForms = createMountedFormsController()
28 const fields = createFieldsController(mountedForms)
29 const form = new InternalFormApi({ defaultValues: { name: '' } })
30 const field = form._getOrCreateFieldApi({ name: 'name' })
31 const unregisterField = field._register()
32 const snapshots: Array<FieldListSnapshot> = []
33 const patches: Array<FieldListPatch> = []
34 const cleanupSnapshotListener = formDevtoolsEventClient.on(
35 'field-list-snapshot',
36 (event) => snapshots.push(event.payload),
37 )
38 const cleanupPatchListener = formDevtoolsEventClient.on(
39 'field-list-patch',
40 (event) => patches.push(event.payload),
41 )
42​
43 try {
44 mountedForms.mountForm(form)
45 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
46​
47 formDevtoolsEventClient.emit('field-list-subscribe', {
48 formInstanceId: instanceId,
49 })
50 expect(snapshots.at(-1)?.fields).toEqual([
51 { path: 'name', fieldId: expect.any(String) },
52 ])
53 const fieldId = snapshots.at(-1)!.fields[0]!.fieldId
54​
55 field.handleChange('changed')
56 fields.updateField(field)
57 await flushPatches()
58​
59 expect(snapshots).toHaveLength(1)
60 expect(patches.at(-1)).toEqual({
61 formInstanceId: instanceId,
62 upsert: [
63 {
64 fieldId,
65 setSummary: {
66 isDirty: true,
67 isTouched: true,
68 isDefaultValue: false,
69 },
70 },
71 ],
72 })
73​
74 const patchCount = patches.length
75 field._setMeta((meta) => ({ ...meta, isBlurred: true }))
76 fields.updateField(field)
77 await flushPatches()
78 expect(patches).toHaveLength(patchCount + 1)
79 expect(patches.at(-1)).toEqual({
80 formInstanceId: instanceId,
81 upsert: [{ fieldId, setSummary: { isBlurred: true } }],
82 })
83​
84 field._setMeta((meta) => ({ ...meta, isBlurred: false }))
85 fields.updateField(field)
86 await flushPatches()
87 expect(patches.at(-1)).toEqual({
88 formInstanceId: instanceId,
89 upsert: [{ fieldId, clearSummary: ['isBlurred'] }],
90 })
91​
92 const patchCountAfterBlur = patches.length
93 field._setMeta((meta) => ({ ...meta, isValidating: true }))
94 fields.updateField(field)
95 await flushPatches()
96 expect(patches).toHaveLength(patchCountAfterBlur)
97​
98 field._setMeta((meta) => ({ ...meta, isValidating: false }))
99 fields.updateField(field)
100 await flushPatches()
101 expect(patches).toHaveLength(patchCountAfterBlur)
102​
103 setFieldValidatorErrors(field, [{ message: 'Invalid name' }])
104 fields.updateField(field)
105 await flushPatches()
106 expect(field.meta.isValid).toBe(false)
107 expect(patches.at(-1)).toEqual({
108 formInstanceId: instanceId,
109 upsert: [
110 {
111 fieldId,
112 setSummary: { hasSelfErrors: true, validity: 'invalid' },
113 },
114 ],
115 })
116​
117 setFieldValidatorErrors(field, [])
118 fields.updateField(field)
119 await flushPatches()
120 expect(field.meta.isValid).toBe(true)
121 expect(patches.at(-1)).toEqual({
122 formInstanceId: instanceId,
123 upsert: [{ fieldId, clearSummary: ['hasSelfErrors', 'validity'] }],
124 })
125​
126 field.handleChange('', {
127 markAsDirty: false,
128 markAsTouched: false,
129 causeValidation: false,
130 })
131 fields.updateField(field)
132 await flushPatches()
133 expect(field.meta.isDefaultValue).toBe(true)
134 expect(field.meta.isDirty).toBe(true)
135 expect(patches.at(-1)).toEqual({
136 formInstanceId: instanceId,
137 upsert: [{ fieldId, clearSummary: ['isDefaultValue'] }],
138 })
139​
140 field._setMeta((meta) => ({ ...meta, isDirty: false }))
141 fields.updateField(field)
142 await flushPatches()
143 expect(patches.at(-1)).toEqual({
144 formInstanceId: instanceId,
145 upsert: [{ fieldId, clearSummary: ['isDirty'] }],
146 })
147​
148 field._setMeta((meta) => ({ ...meta, isTouched: false }))
149 fields.updateField(field)
150 await flushPatches()
151 expect(patches.at(-1)).toEqual({
152 formInstanceId: instanceId,
153 upsert: [{ fieldId, clearSummary: ['isTouched'] }],
154 })
155​
156 formDevtoolsEventClient.emit('field-list-unsubscribe', {
157 formInstanceId: instanceId,
158 })
159 const unsubscribedPatchCount = patches.length
160 field.handleChange('changed again')
161 fields.updateField(field)
162 await flushPatches()
163 expect(patches).toHaveLength(unsubscribedPatchCount)
164​
165 formDevtoolsEventClient.emit('field-list-subscribe', {
166 formInstanceId: instanceId,
167 })
168 expect(snapshots.at(-1)?.fields[0]?.summary).toEqual({
169 isDirty: true,
170 isTouched: true,
171 isDefaultValue: false,
172 })
173​
174 const patchCountBeforeUnmount = patches.length
175 field._setMeta((meta) => ({ ...meta, isDirty: false }))
176 fields.updateField(field)
177 mountedForms.unmountForm(form, fields.unmountForm)
178 await flushPatches()
179 expect(snapshots.at(-1)?.fields).toEqual([])
180 expect(patches).toHaveLength(patchCountBeforeUnmount)
181 } finally {
182 cleanupPatchListener()
183 cleanupSnapshotListener()
184 unregisterField()
185 fields.dispose()
186 mountedForms.dispose()
187 disconnectEventBus()
188 }
189 })
190​
191 it('retains unmounted fields while their errors change', async () => {
192 const disconnectEventBus = connectTestEventBus()
193 const mountedForms = createMountedFormsController()
194 const fields = createFieldsController(mountedForms)
195 const form = new InternalFormApi({ defaultValues: { name: '' } })
196 const field = form._getOrCreateFieldApi({ name: 'name' })
197 const unregisterField = field._register()
198 const snapshots: Array<FieldListSnapshot> = []
199 const patches: Array<FieldListPatch> = []
200 const cleanupSnapshotListener = formDevtoolsEventClient.on(
201 'field-list-snapshot',
202 (event) => snapshots.push(event.payload),
203 )
204 const cleanupPatchListener = formDevtoolsEventClient.on(
205 'field-list-patch',
206 (event) => patches.push(event.payload),
207 )
208​
209 try {
210 mountedForms.mountForm(form)
211 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
212 formDevtoolsEventClient.emit('field-list-subscribe', {
213 formInstanceId: instanceId,
214 })
215 const fieldId = snapshots[0]!.fields[0]!.fieldId
216​
217 unregisterField()
218 fields.unmountField(field, 'name')
219 await flushPatches()
220 expect(patches.at(-1)).toEqual({
221 formInstanceId: instanceId,
222 upsert: [{ fieldId, path: 'name', isMounted: false }],
223 })
224​
225 setFieldValidatorErrors(field, [{ message: 'Invalid name' }])
226 fields.updateField(field)
227 await flushPatches()
228 expect(patches.at(-1)).toEqual({
229 formInstanceId: instanceId,
230 upsert: [
231 {
232 fieldId,
233 setSummary: { hasSelfErrors: true, validity: 'invalid' },
234 },
235 ],
236 })
237​
238 setFieldValidatorErrors(field, [])
239 fields.updateField(field)
240 await flushPatches()
241 expect(patches.at(-1)).toEqual({
242 formInstanceId: instanceId,
243 upsert: [{ fieldId, clearSummary: ['hasSelfErrors', 'validity'] }],
244 })
245 } finally {
246 cleanupPatchListener()
247 cleanupSnapshotListener()
248 fields.dispose()
249 mountedForms.dispose()
250 disconnectEventBus()
251 }
252 })
253​
254 it('removes and restores a field when FormGroup ownership changes', async () => {
255 const disconnectEventBus = connectTestEventBus()
256 const mountedForms = createMountedFormsController()
257 const fields = createFieldsController(mountedForms)
258 const bridge = createFormDevtoolsBridge({ fields, mountedForms })
259 const uninstallBridge = installDevtoolsBridge(bridge)
260 const form = new InternalFormApi({
261 defaultValues: { guestDetails: { name: '' } },
262 })
263 form._getOrCreateFieldApi({ name: 'guestDetails' })
264 const nameField = form._getOrCreateFieldApi({
265 name: 'guestDetails.name',
266 })
267 const unregisterName = nameField._register()
268 const snapshots: Array<FieldListSnapshot> = []
269 const patches: Array<FieldListPatch> = []
270 const cleanupSnapshotListener = formDevtoolsEventClient.on(
271 'field-list-snapshot',
272 (event) => snapshots.push(event.payload),
273 )
274 const cleanupPatchListener = formDevtoolsEventClient.on(
275 'field-list-patch',
276 (event) => patches.push(event.payload),
277 )
278 let group: InternalFormGroupApi<any, any, any, any, any> | undefined
279​
280 try {
281 mountedForms.mountForm(form)
282 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
283 formDevtoolsEventClient.emit('field-list-subscribe', {
284 formInstanceId: instanceId,
285 })
286 const groupFieldId = snapshots[0]!.fields.find(
287 (field) => field.path === 'guestDetails',
288 )!.fieldId
289​
290 group = new InternalFormGroupApi({ form, name: 'guestDetails' })
291 await flushPatches()
292 expect(patches.at(-1)).toEqual({
293 formInstanceId: instanceId,
294 remove: [groupFieldId],
295 })
296​
297 group._cleanup()
298 group = undefined
299 await flushPatches()
300 expect(patches.at(-1)).toEqual({
301 formInstanceId: instanceId,
302 upsert: [
303 {
304 fieldId: groupFieldId,
305 path: 'guestDetails',
306 isMounted: false,
307 },
308 ],
309 })
310 } finally {
311 group?._cleanup()
312 cleanupPatchListener()
313 cleanupSnapshotListener()
314 unregisterName()
315 uninstallBridge()
316 fields.dispose()
317 mountedForms.dispose()
318 disconnectEventBus()
319 }
320 })
321​
322 it('activates long validation after 300 ms and clears it immediately', async () => {
323 vi.useFakeTimers()
324 const disconnectEventBus = connectTestEventBus()
325 const mountedForms = createMountedFormsController()
326 const fields = createFieldsController(mountedForms)
327 const form = new InternalFormApi({ defaultValues: { name: '' } })
328 const field = form._getOrCreateFieldApi({ name: 'name' })
329 const unregisterField = field._register()
330 const snapshots: Array<FieldListSnapshot> = []
331 const patches: Array<FieldListPatch> = []
332 const cleanupSnapshotListener = formDevtoolsEventClient.on(
333 'field-list-snapshot',
334 (event) => snapshots.push(event.payload),
335 )
336 const cleanupPatchListener = formDevtoolsEventClient.on(
337 'field-list-patch',
338 (event) => patches.push(event.payload),
339 )
340​
341 try {
342 mountedForms.mountForm(form)
343 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
344 formDevtoolsEventClient.emit('field-list-subscribe', {
345 formInstanceId: instanceId,
346 })
347 const fieldId = snapshots[0]!.fields[0]!.fieldId
348​
349 field._setMeta((meta) => ({ ...meta, isValidating: true }))
350 fields.updateField(field)
351 await flushPatches()
352 expect(patches).toEqual([])
353​
354 await vi.advanceTimersByTimeAsync(150)
355 fields.updateField(field)
356 await flushPatches()
357 await vi.advanceTimersByTimeAsync(149)
358 expect(patches).toEqual([])
359​
360 await vi.advanceTimersByTimeAsync(1)
361 await flushPatches()
362 expect(patches).toEqual([
363 {
364 formInstanceId: instanceId,
365 upsert: [{ fieldId, setSummary: { isLongValidating: true } }],
366 },
367 ])
368​
369 field._setMeta((meta) => ({ ...meta, isValidating: false }))
370 fields.updateField(field)
371 await flushPatches()
372 expect(patches.at(-1)).toEqual({
373 formInstanceId: instanceId,
374 upsert: [{ fieldId, clearSummary: ['isLongValidating'] }],
375 })
376 } finally {
377 cleanupPatchListener()
378 cleanupSnapshotListener()
379 unregisterField()
380 fields.dispose()
381 mountedForms.dispose()
382 disconnectEventBus()
383 }
384 })
385​
386 it('coalesces long validation for a field and its parent', async () => {
387 vi.useFakeTimers()
388 const disconnectEventBus = connectTestEventBus()
389 const mountedForms = createMountedFormsController()
390 const fields = createFieldsController(mountedForms)
391 const bridge = createFormDevtoolsBridge({ fields, mountedForms })
392 const uninstallBridge = installDevtoolsBridge(bridge)
393 const form = new InternalFormApi({
394 defaultValues: { user: { name: '' } },
395 })
396 const parent = form._getOrCreateFieldApi({ name: 'user' })
397 const child = form._getOrCreateFieldApi({ name: 'user.name' })
398 const unmountForm = form.mount()
399 const unregisterParent = parent._register()
400 const unregisterChild = child._register()
401 const snapshots: Array<FieldListSnapshot> = []
402 const patches: Array<FieldListPatch> = []
403 const cleanupSnapshotListener = formDevtoolsEventClient.on(
404 'field-list-snapshot',
405 (event) => snapshots.push(event.payload),
406 )
407 const cleanupPatchListener = formDevtoolsEventClient.on(
408 'field-list-patch',
409 (event) => patches.push(event.payload),
410 )
411​
412 try {
413 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
414 formDevtoolsEventClient.emit('field-list-subscribe', {
415 formInstanceId: instanceId,
416 })
417 const fieldIds = new Map(
418 snapshots[0]!.fields.map((field) => [field.path, field.fieldId]),
419 )
420​
421 child._setValidationCount((count) => count + 1)
422 child._setValidationCount((count) => count + 1)
423 await vi.advanceTimersByTimeAsync(300)
424 await flushPatches()
425​
426 expect(patches).toHaveLength(1)
427 expect(patches[0]).toEqual({
428 formInstanceId: instanceId,
429 upsert: expect.arrayContaining([
430 {
431 fieldId: fieldIds.get('user'),
432 setSummary: { isLongValidating: true },
433 },
434 {
435 fieldId: fieldIds.get('user.name'),
436 setSummary: { isLongValidating: true },
437 },
438 ]),
439 })
440​
441 child._setValidationCount((count) => count - 1)
442 await flushPatches()
443 expect(patches).toHaveLength(1)
444​
445 child._setValidationCount((count) => count - 1)
446 await flushPatches()
447 expect(patches).toHaveLength(2)
448 expect(patches[1]).toEqual({
449 formInstanceId: instanceId,
450 upsert: expect.arrayContaining([
451 {
452 fieldId: fieldIds.get('user'),
453 clearSummary: ['isLongValidating'],
454 },
455 {
456 fieldId: fieldIds.get('user.name'),
457 clearSummary: ['isLongValidating'],
458 },
459 ]),
460 })
461 } finally {
462 cleanupPatchListener()
463 cleanupSnapshotListener()
464 unregisterChild()
465 unregisterParent()
466 unmountForm()
467 uninstallBridge()
468 fields.dispose()
469 mountedForms.dispose()
470 disconnectEventBus()
471 }
472 })
473​
474 it('restarts observation after unsubscribe and preserves active recovery snapshots', async () => {
475 vi.useFakeTimers()
476 const disconnectEventBus = connectTestEventBus()
477 const mountedForms = createMountedFormsController()
478 const fields = createFieldsController(mountedForms)
479 const form = new InternalFormApi({ defaultValues: { name: '' } })
480 const field = form._getOrCreateFieldApi({ name: 'name' })
481 const unregisterField = field._register()
482 const snapshots: Array<FieldListSnapshot> = []
483 const patches: Array<FieldListPatch> = []
484 const cleanupSnapshotListener = formDevtoolsEventClient.on(
485 'field-list-snapshot',
486 (event) => snapshots.push(event.payload),
487 )
488 const cleanupPatchListener = formDevtoolsEventClient.on(
489 'field-list-patch',
490 (event) => patches.push(event.payload),
491 )
492​
493 try {
494 mountedForms.mountForm(form)
495 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
496 formDevtoolsEventClient.emit('field-list-subscribe', {
497 formInstanceId: instanceId,
498 })
499 const fieldId = snapshots[0]!.fields[0]!.fieldId
500​
501 field._setMeta((meta) => ({ ...meta, isValidating: true }))
502 fields.updateField(field)
503 await vi.advanceTimersByTimeAsync(150)
504 formDevtoolsEventClient.emit('field-list-unsubscribe', {
505 formInstanceId: instanceId,
506 })
507 expect(vi.getTimerCount()).toBe(0)
508​
509 await vi.advanceTimersByTimeAsync(150)
510 expect(patches).toEqual([])
511​
512 formDevtoolsEventClient.emit('field-list-subscribe', {
513 formInstanceId: instanceId,
514 })
515 expect(snapshots.at(-1)?.fields).toEqual([{ path: 'name', fieldId }])
516​
517 await vi.advanceTimersByTimeAsync(300)
518 await flushPatches()
519 expect(patches.at(-1)).toEqual({
520 formInstanceId: instanceId,
521 upsert: [{ fieldId, setSummary: { isLongValidating: true } }],
522 })
523​
524 formDevtoolsEventClient.emit('field-list-subscribe', {
525 formInstanceId: instanceId,
526 })
527 expect(snapshots.at(-1)?.fields).toEqual([
528 {
529 path: 'name',
530 fieldId,
531 summary: { isLongValidating: true },
532 },
533 ])
534​
535 field._setMeta((meta) => ({ ...meta, isValidating: false }))
536 fields.updateField(field)
537 await flushPatches()
538 field._setMeta((meta) => ({ ...meta, isValidating: true }))
539 fields.updateField(field)
540 expect(vi.getTimerCount()).toBe(1)
541​
542 mountedForms.unmountForm(form, fields.unmountForm)
543 expect(vi.getTimerCount()).toBe(0)
544 const patchCount = patches.length
545 await vi.advanceTimersByTimeAsync(300)
546 expect(patches).toHaveLength(patchCount)
547 } finally {
548 cleanupPatchListener()
549 cleanupSnapshotListener()
550 unregisterField()
551 fields.dispose()
552 mountedForms.dispose()
553 disconnectEventBus()
554 }
555 })
556​
557 it('coalesces structural changes to each field final state', async () => {
558 const disconnectEventBus = connectTestEventBus()
559 const mountedForms = createMountedFormsController()
560 const fields = createFieldsController(mountedForms)
561 const form = new InternalFormApi({ defaultValues: { stable: '' } })
562 const stableField = form._getOrCreateFieldApi({ name: 'stable' })
563 const unregisterStable = stableField._register()
564 const patches: Array<FieldListPatch> = []
565 const snapshots: Array<FieldListSnapshot> = []
566 const cleanupPatchListener = formDevtoolsEventClient.on(
567 'field-list-patch',
568 (event) => patches.push(event.payload),
569 )
570 const cleanupSnapshotListener = formDevtoolsEventClient.on(
571 'field-list-snapshot',
572 (event) => snapshots.push(event.payload),
573 )
574​
575 try {
576 mountedForms.mountForm(form)
577 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
578 formDevtoolsEventClient.emit('field-list-subscribe', {
579 formInstanceId: instanceId,
580 })
581​
582 const transient = form._getOrCreateFieldApi({ name: 'transient' })
583 const unregisterTransient = transient._register()
584 fields.mountField(transient)
585 unregisterTransient()
586 fields.unmountField(transient, 'transient')
587 await flushPatches()
588​
589 expect(patches.at(-1)).toEqual({
590 formInstanceId: instanceId,
591 upsert: [
592 {
593 fieldId: expect.any(String),
594 path: 'transient',
595 isMounted: false,
596 },
597 ],
598 })
599​
600 patches.length = 0
601 unregisterStable()
602 fields.unmountField(stableField, 'stable')
603 const unregisterRemounted = stableField._register()
604 fields.mountField(stableField)
605 fields.updateField(stableField)
606 await flushPatches()
607​
608 expect(patches).toEqual([
609 {
610 formInstanceId: instanceId,
611 upsert: [
612 {
613 fieldId: snapshots[0]!.fields[0]!.fieldId,
614 path: 'stable',
615 },
616 ],
617 },
618 ])
619 unregisterRemounted()
620 } finally {
621 cleanupSnapshotListener()
622 cleanupPatchListener()
623 fields.dispose()
624 mountedForms.dispose()
625 disconnectEventBus()
626 }
627 })
628​
629 it('coalesces parent and child meta updates into one patch event', async () => {
630 const disconnectEventBus = connectTestEventBus()
631 const mountedForms = createMountedFormsController()
632 const fields = createFieldsController(mountedForms)
633 const bridge = createFormDevtoolsBridge({ fields, mountedForms })
634 const uninstallBridge = installDevtoolsBridge(bridge)
635 const form = new InternalFormApi({
636 defaultValues: { user: { name: '' } },
637 })
638 const parent = form._getOrCreateFieldApi({ name: 'user' })
639 const child = form._getOrCreateFieldApi({ name: 'user.name' })
640 const unmountForm = form.mount()
641 const unregisterParent = parent._register()
642 const unregisterChild = child._register()
643 const patches: Array<FieldListPatch> = []
644 const snapshots: Array<FieldListSnapshot> = []
645 const cleanupPatchListener = formDevtoolsEventClient.on(
646 'field-list-patch',
647 (event) => patches.push(event.payload),
648 )
649 const cleanupSnapshotListener = formDevtoolsEventClient.on(
650 'field-list-snapshot',
651 (event) => snapshots.push(event.payload),
652 )
653​
654 try {
655 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
656 formDevtoolsEventClient.emit('field-list-subscribe', {
657 formInstanceId: instanceId,
658 })
659 const fieldIds = new Map(
660 snapshots[0]!.fields.map((field) => [field.path, field.fieldId]),
661 )
662​
663 child.handleChange('changed')
664 await flushPatches()
665​
666 expect(patches).toHaveLength(1)
667 expect(patches[0]?.formInstanceId).toBe(instanceId)
668 expect(patches[0]?.upsert).toEqual(
669 expect.arrayContaining([
670 {
671 fieldId: fieldIds.get('user'),
672 setSummary: {
673 isDirty: true,
674 isTouched: true,
675 isDefaultValue: false,
676 },
677 },
678 {
679 fieldId: fieldIds.get('user.name'),
680 setSummary: {
681 isDirty: true,
682 isTouched: true,
683 isDefaultValue: false,
684 },
685 },
686 ]),
687 )
688​
689 patches.length = 0
690 child.handleChange('', { causeValidation: false })
691 expect(parent.meta.isDefaultValue).toBe(true)
692 expect(child.meta.isDefaultValue).toBe(true)
693 await flushPatches()
694​
695 expect(patches).toHaveLength(1)
696 expect(patches[0]?.upsert).toEqual(
697 expect.arrayContaining([
698 {
699 fieldId: fieldIds.get('user'),
700 clearSummary: ['isDefaultValue'],
701 },
702 {
703 fieldId: fieldIds.get('user.name'),
704 clearSummary: ['isDefaultValue'],
705 },
706 ]),
707 )
708​
709 patches.length = 0
710 form._update({
711 defaultValues: { user: { name: 'new default' } },
712 })
713 await flushPatches()
714​
715 expect(patches).toHaveLength(1)
716 expect(patches[0]?.upsert).toEqual(
717 expect.arrayContaining([
718 {
719 fieldId: fieldIds.get('user'),
720 setSummary: { isDefaultValue: false },
721 },
722 {
723 fieldId: fieldIds.get('user.name'),
724 setSummary: { isDefaultValue: false },
725 },
726 ]),
727 )
728​
729 patches.length = 0
730 parent._kill()
731 await flushPatches()
732​
733 expect(patches).toEqual([
734 {
735 formInstanceId: instanceId,
736 remove: expect.arrayContaining([
737 fieldIds.get('user'),
738 fieldIds.get('user.name'),
739 ]),
740 },
741 ])
742 } finally {
743 cleanupSnapshotListener()
744 cleanupPatchListener()
745 unregisterChild()
746 unregisterParent()
747 unmountForm()
748 uninstallBridge()
749 fields.dispose()
750 mountedForms.dispose()
751 disconnectEventBus()
752 }
753 })
754​
755 it('emits only final paths after repeated moves in one microtask', async () => {
756 const disconnectEventBus = connectTestEventBus()
757 const mountedForms = createMountedFormsController()
758 const fields = createFieldsController(mountedForms)
759 const bridge = createFormDevtoolsBridge({ fields, mountedForms })
760 const uninstallBridge = installDevtoolsBridge(bridge)
761 const form = new InternalFormApi({ defaultValues: { items: ['a', 'b'] } })
762 const field0 = form._getOrCreateFieldApi({ name: 'items[0]' })
763 const field1 = form._getOrCreateFieldApi({ name: 'items[1]' })
764 const unmountForm = form.mount()
765 const unregister0 = field0._register()
766 const unregister1 = field1._register()
767 const patches: Array<FieldListPatch> = []
768 const snapshots: Array<FieldListSnapshot> = []
769 const cleanupPatchListener = formDevtoolsEventClient.on(
770 'field-list-patch',
771 (event) => patches.push(event.payload),
772 )
773 const cleanupSnapshotListener = formDevtoolsEventClient.on(
774 'field-list-snapshot',
775 (event) => snapshots.push(event.payload),
776 )
777​
778 try {
779 const { instanceId } = mountedForms.getMountedFormsSnapshot()[0]!
780 formDevtoolsEventClient.emit('field-list-subscribe', {
781 formInstanceId: instanceId,
782 })
783 const fieldIds = new Map(
784 snapshots[0]!.fields.map((field) => [field.path, field.fieldId]),
785 )
786​
787 form.swapFieldValues('items', 0, 1)
788 form.swapFieldValues('items', 0, 1)
789 await flushPatches()
790​
791 expect(patches).toHaveLength(1)
792 expect(patches[0]).toEqual({
793 formInstanceId: instanceId,
794 upsert: expect.arrayContaining([
795 {
796 fieldId: fieldIds.get('items[0]'),
797 path: 'items[0]',
798 },
799 {
800 fieldId: fieldIds.get('items[1]'),
801 path: 'items[1]',
802 },
803 ]),
804 })
805 } finally {
806 cleanupSnapshotListener()
807 cleanupPatchListener()
808 unregister1()
809 unregister0()
810 unmountForm()
811 uninstallBridge()
812 fields.dispose()
813 mountedForms.dispose()
814 disconnectEventBus()
815 }
816 })
817})