Open History to see the last commit
1import { InternalFormApi } from '@tanstack/form-core/internals'
2import { describe, expect, it, vi } from 'vitest'
3import { createFormDevtoolsBridge } from '../src/bridge/createBridge'
4import type {
5 FieldsBridgeController,
6 MountedFormsBridgeController,
7} from '../src/bridge/createBridge'
8import type { FormId } from '../src/types/branded'
9β
10describe('form devtools bridge composition', () => {
11 it('routes form and field lifecycle events to their purpose controllers', () => {
12 const form = new InternalFormApi({ defaultValues: { name: '' } })
13 const field = form._getOrCreateFieldApi({
14 name: 'name',
15 listeners: [{ triggers: ['change'], run: () => {} }],
16 })
17 const formInstanceId = 'form-instance' as FormId
18 const mountedForms: MountedFormsBridgeController = {
19 mountForm: vi.fn(() => true),
20 unmountForm: vi.fn((_form, onFinalUnmount) => {
21 onFinalUnmount(formInstanceId)
22 return true
23 }),
24 updateForm: vi.fn(),
25 }
26 const fields: FieldsBridgeController = {
27 mountForm: vi.fn(),
28 unmountForm: vi.fn(),
29 mountField: vi.fn(),
30 updateField: vi.fn(),
31 fieldAdded: vi.fn(),
32 fieldDependenciesChanged: vi.fn(),
33 unmountField: vi.fn(),
34 moveField: vi.fn(),
35 removeFieldSubtree: vi.fn(),
36 }
37 const bridge = createFormDevtoolsBridge({ fields, mountedForms })
38 const removedFields = [{ field, previousPath: 'name' }]
39 const dependencyChanges = [
40 {
41 kind: 'listener' as const,
42 sourceField: field,
43 watchingField: field,
44 listenerInstance: field._listenerInstances![0]!,
45 },
46 ]
47β
48 bridge.mountForm?.(form)
49 bridge.updateForm?.(form)
50 bridge.mountField?.(field)
51 bridge.updateField?.(field)
52 bridge.fieldAdded?.(field)
53 bridge.fieldDependenciesChanged?.(dependencyChanges)
54 bridge.unmountField?.(field, 'name')
55 bridge.moveField?.(field, 'previousName')
56 bridge.removeFieldSubtree?.(form, removedFields)
57 bridge.unmountForm?.(form)
58β
59 expect(fields.mountForm).toHaveBeenCalledWith(form)
60 expect(fields.unmountForm).toHaveBeenCalledWith(formInstanceId)
61 expect(fields.mountField).toHaveBeenCalledWith(field)
62 expect(fields.updateField).toHaveBeenCalledWith(field)
63 expect(fields.fieldAdded).toHaveBeenCalledWith(field)
64 expect(fields.fieldDependenciesChanged).toHaveBeenCalledWith(
65 dependencyChanges,
66 )
67 expect(fields.unmountField).toHaveBeenCalledWith(field, 'name')
68 expect(fields.moveField).toHaveBeenCalledWith(field, 'previousName')
69 expect(fields.removeFieldSubtree).toHaveBeenCalledWith(form, removedFields)
70 expect(mountedForms.updateForm).toHaveBeenCalledWith(form)
71 })
72β
73 it('does not repeat field form-mount work for duplicate mounts', () => {
74 const form = new InternalFormApi({ defaultValues: {} })
75 const mountedForms: MountedFormsBridgeController = {
76 mountForm: vi.fn(() => false),
77 unmountForm: vi.fn(() => false),
78 updateForm: vi.fn(),
79 }
80 const fields: FieldsBridgeController = {
81 mountForm: vi.fn(),
82 unmountForm: vi.fn(),
83 mountField: vi.fn(),
84 updateField: vi.fn(),
85 fieldAdded: vi.fn(),
86 fieldDependenciesChanged: vi.fn(),
87 unmountField: vi.fn(),
88 moveField: vi.fn(),
89 removeFieldSubtree: vi.fn(),
90 }
91β
92 createFormDevtoolsBridge({ fields, mountedForms }).mountForm?.(form)
93β
94 expect(fields.mountForm).not.toHaveBeenCalled()
95 })
96})