Open History to see the last commit
1import { InternalFormApi } from '@tanstack/form-core/internals'
2import { describe, expect, it, vi } from 'vitest'
3import {
4 formDevtoolsBridgeRuntimeKey,
5 getOrCreateFormDevtoolsBridgeRuntime,
6} from '../src/bridge/runtime'
7import { formDevtoolsEventClient } from '../src/eventClient.lib'
8import { connectTestEventBus } from './testEventBus'
9import type {
10 DevtoolsFieldDetail,
11 FormDevtoolsBridgeStatusResponse,
12 FormDevtoolsEventMap,
13} from '../src/eventClientTypes'
14β
15const flushFieldPatches = () => Promise.resolve()
16β
17describe('form devtools bridge runtime', () => {
18 it('creates only one runtime for each host', () => {
19 const host = {}
20 const firstRuntime = { dispose: vi.fn() }
21 const createFirstRuntime = vi.fn(() => firstRuntime)
22 const createReplacementRuntime = vi.fn(() => ({ dispose: vi.fn() }))
23β
24 expect(
25 getOrCreateFormDevtoolsBridgeRuntime({
26 host,
27 createRuntime: createFirstRuntime,
28 }),
29 ).toBe(firstRuntime)
30 expect(
31 getOrCreateFormDevtoolsBridgeRuntime({
32 host,
33 createRuntime: createReplacementRuntime,
34 }),
35 ).toBe(firstRuntime)
36 expect(createFirstRuntime).toHaveBeenCalledOnce()
37 expect(createReplacementRuntime).not.toHaveBeenCalled()
38 expect(firstRuntime.dispose).not.toHaveBeenCalled()
39 })
40β
41 it('keeps mounted forms and field subscriptions across re-evaluation', async () => {
42 const disconnectEventBus = connectTestEventBus()
43 const host = {}
44 const runtime = getOrCreateFormDevtoolsBridgeRuntime({ host })
45 const form = new InternalFormApi({ defaultValues: { name: '' } })
46 const field = form._getOrCreateFieldApi({ name: 'name' })
47 const unregisterField = field._register()
48 const mountedForms: Array<FormDevtoolsEventMap['mounted-forms-changed']> =
49 []
50 const snapshots: Array<FormDevtoolsEventMap['field-list-snapshot']> = []
51 const patches: Array<FormDevtoolsEventMap['field-list-patch']> = []
52 const details: Array<DevtoolsFieldDetail> = []
53 const statuses: Array<FormDevtoolsBridgeStatusResponse> = []
54 const cleanupMountedForms = formDevtoolsEventClient.on(
55 'mounted-forms-changed',
56 (event) => mountedForms.push(event.payload),
57 )
58 const cleanupSnapshots = formDevtoolsEventClient.on(
59 'field-list-snapshot',
60 (event) => snapshots.push(event.payload),
61 )
62 const cleanupPatches = formDevtoolsEventClient.on(
63 'field-list-patch',
64 (event) => patches.push(event.payload),
65 )
66 const cleanupDetails = formDevtoolsEventClient.on(
67 'field-detail-changed',
68 (event) => details.push(event.payload),
69 )
70 const cleanupStatuses = formDevtoolsEventClient.on(
71 'bridge-status-response',
72 (event) => statuses.push(event.payload),
73 )
74 const unmountForm = form.mount()
75β
76 try {
77 const formInstanceId = mountedForms.at(-1)!.forms[0]!.instanceId
78 formDevtoolsEventClient.emit('field-list-subscribe', {
79 formInstanceId,
80 })
81 const fieldId = snapshots.at(-1)!.fields[0]!.fieldId
82 formDevtoolsEventClient.emit('field-detail-subscribe', {
83 formInstanceId,
84 fieldId,
85 settings: {
86 includeValues: true,
87 errorPayloadMode: 'full',
88 debounceMs: 0,
89 },
90 })
91 formDevtoolsEventClient.emit('bridge-status-request', {
92 requestId: 'before-re-evaluation',
93 })
94β
95 expect(statuses.at(-1)).toMatchObject({
96 requestId: 'before-re-evaluation',
97 mountedFormCount: 1,
98 })
99 const bridgeInstanceId = statuses.at(-1)!.bridgeInstanceId
100β
101 field.handleChange('Ada')
102 await flushFieldPatches()
103 expect(patches).toHaveLength(1)
104 expect(details.at(-1)?.state.value).toBe('Ada')
105β
106 const createReplacementRuntime = vi.fn(() => ({ dispose: vi.fn() }))
107 expect(
108 getOrCreateFormDevtoolsBridgeRuntime({
109 host,
110 createRuntime: createReplacementRuntime,
111 }),
112 ).toBe(runtime)
113 expect(createReplacementRuntime).not.toHaveBeenCalled()
114β
115 field._setMeta((meta) => ({ ...meta, isBlurred: true }))
116 await flushFieldPatches()
117 expect(patches).toHaveLength(2)
118 expect(patches.at(-1)?.upsert?.[0]?.setSummary).toEqual({
119 isBlurred: true,
120 })
121β
122 field.handleChange('Grace')
123 expect(details.at(-1)?.state.value).toBe('Grace')
124β
125 formDevtoolsEventClient.emit('bridge-status-request', {
126 requestId: 'after-re-evaluation',
127 })
128 expect(statuses.at(-1)).toEqual({
129 requestId: 'after-re-evaluation',
130 bridgeInstanceId,
131 mountedFormCount: 1,
132 })
133 } finally {
134 cleanupStatuses()
135 cleanupDetails()
136 cleanupPatches()
137 cleanupSnapshots()
138 cleanupMountedForms()
139 unmountForm()
140 unregisterField()
141 runtime.dispose()
142 disconnectEventBus()
143 }
144β
145 expect(
146 (host as { [key: symbol]: unknown })[formDevtoolsBridgeRuntimeKey],
147 ).toBeUndefined()
148 })
149})