Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { InternalFormApi } from '../src/FormApi/FormApi.lib'
3import { installDevtoolsBridge } from '../src/devtoolsBridge.lib'
4β
5describe('installDevtoolsBridge', () => {
6 it('reports a field mount once and unmounts it after its last consumer unregisters', async ({
7 onTestFinished,
8 }) => {
9 vi.useFakeTimers()
10 const form = new InternalFormApi({ defaultValues: { name: '' } })
11 const field = form._getOrCreateFieldApi({ name: 'name' })
12 const mountField = vi.fn()
13 const unmountField = vi.fn()
14 const uninstall = installDevtoolsBridge({ mountField, unmountField })
15 onTestFinished(uninstall)
16β
17 const unregisterFirst = field._register()
18 const unregisterSecond = field._register()
19β
20 expect(mountField).toHaveBeenCalledOnce()
21 expect(mountField).toHaveBeenCalledWith(field)
22β
23 unregisterFirst()
24 expect(unmountField).not.toHaveBeenCalled()
25β
26 unregisterSecond()
27 await vi.runOnlyPendingTimersAsync()
28β
29 expect(unmountField).toHaveBeenCalledOnce()
30 expect(unmountField).toHaveBeenCalledWith(field, 'name')
31 })
32β
33 it('reports moved fields with their previous paths after an array swap', ({
34 onTestFinished,
35 }) => {
36 const form = new InternalFormApi({
37 defaultValues: { items: ['a', 'b'] },
38 })
39 const first = form._getOrCreateFieldApi({ name: 'items[0]' })
40 const second = form._getOrCreateFieldApi({ name: 'items[1]' })
41 const moveField = vi.fn()
42 const uninstall = installDevtoolsBridge({ moveField })
43 onTestFinished(uninstall)
44β
45 form.swapFieldValues('items', 0, 1)
46β
47 expect(moveField).toHaveBeenCalledWith(first, 'items[0]')
48 expect(moveField).toHaveBeenCalledWith(second, 'items[1]')
49 expect(first.name).toBe('items[1]')
50 expect(second.name).toBe('items[0]')
51 expect(form.state.values.items).toEqual(['b', 'a'])
52 })
53β
54 it('reports updates for a changed field and its parent even when both are already dirty', ({
55 onTestFinished,
56 }) => {
57 const form = new InternalFormApi({
58 defaultValues: { user: { name: '' } },
59 })
60 const parent = form._getOrCreateFieldApi({ name: 'user' })
61 const field = form._getOrCreateFieldApi({ name: 'user.name' })
62 const updateField = vi.fn()
63 const uninstall = installDevtoolsBridge({ updateField })
64 onTestFinished(uninstall)
65β
66 field.handleChange('first')
67β
68 expect(field.meta.isDirty).toBe(true)
69 expect(parent.meta.isDirty).toBe(true)
70 expect(updateField).toHaveBeenCalledWith(field)
71 expect(updateField).toHaveBeenCalledWith(parent)
72 updateField.mockClear()
73β
74 field.handleChange('second')
75β
76 expect(field.meta.isDirty).toBe(true)
77 expect(parent.meta.isDirty).toBe(true)
78 expect(field.value).toBe('second')
79 expect(parent.value).toEqual({ name: 'second' })
80 expect(updateField).toHaveBeenCalledWith(field)
81 expect(updateField).toHaveBeenCalledWith(parent)
82 })
83β
84 it('reports field resets and updated defaults that bypass change events', ({
85 onTestFinished,
86 }) => {
87 const form = new InternalFormApi({ defaultValues: { name: '' } })
88 const field = form._getOrCreateFieldApi({ name: 'name' })
89 const updateField = vi.fn()
90 const uninstall = installDevtoolsBridge({ updateField })
91 onTestFinished(uninstall)
92β
93 field.handleChange('changed')
94 updateField.mockClear()
95β
96 form.resetField('name')
97β
98 expect(field.value).toBe('')
99 expect(updateField).toHaveBeenCalledWith(field)
100 updateField.mockClear()
101β
102 form._update({ defaultValues: { name: 'new default' } })
103β
104 expect(field.value).toBe('new default')
105 expect(updateField).toHaveBeenCalledWith(field)
106 })
107β
108 it('reports every removed field in a subtree with its previous path', ({
109 onTestFinished,
110 }) => {
111 const form = new InternalFormApi({
112 defaultValues: { user: { name: '' } },
113 })
114 const parent = form._getOrCreateFieldApi({ name: 'user' })
115 const child = form._getOrCreateFieldApi({ name: 'user.name' })
116 const removeFieldSubtree = vi.fn()
117 const uninstall = installDevtoolsBridge({ removeFieldSubtree })
118 onTestFinished(uninstall)
119β
120 form.deleteField('user')
121β
122 expect(removeFieldSubtree).toHaveBeenCalledOnce()
123 expect(removeFieldSubtree).toHaveBeenCalledWith(form, [
124 { field: parent, previousPath: 'user' },
125 { field: child, previousPath: 'user.name' },
126 ])
127 })
128β
129 it('reports fields added beneath an existing parent', ({
130 onTestFinished,
131 }) => {
132 const form = new InternalFormApi({
133 defaultValues: { user: { name: 'Alice' } },
134 })
135 form._getOrCreateFieldApi({ name: 'user' })
136 const fieldAdded = vi.fn()
137 const uninstall = installDevtoolsBridge({ fieldAdded })
138 onTestFinished(uninstall)
139β
140 const child = form._getOrCreateFieldApi({ name: 'user.name' })
141β
142 expect(fieldAdded).toHaveBeenCalledWith(child)
143 expect(child.name).toBe('user.name')
144 expect(child.value).toBe('Alice')
145 })
146β
147 it('reports listener dependency endpoints when attached and when the watching field is deleted', ({
148 onTestFinished,
149 }) => {
150 const form = new InternalFormApi({
151 defaultValues: { source: '', target: 'target' },
152 })
153 const source = form._getOrCreateFieldApi({ name: 'source' })
154 const target = form._getOrCreateFieldApi({ name: 'target' })
155 source._register()
156 target._register()
157 const listener = vi.fn()
158 const fieldDependenciesChanged = vi.fn()
159 const uninstall = installDevtoolsBridge({ fieldDependenciesChanged })
160 onTestFinished(uninstall)
161β
162 target._update({
163 listeners: [
164 { triggers: ['change'], watchFields: ['source'], run: listener },
165 ],
166 })
167β
168 expect(fieldDependenciesChanged).toHaveBeenCalledWith([
169 expect.objectContaining({
170 kind: 'listener',
171 sourceField: source,
172 watchingField: target,
173 listenerInstance: expect.anything(),
174 }),
175 ])
176β
177 source.handleChange('first')
178 expect(listener).toHaveBeenCalledWith(
179 expect.objectContaining({ value: 'target', fieldApi: target }),
180 )
181 listener.mockClear()
182 fieldDependenciesChanged.mockClear()
183β
184 form.deleteField('target')
185β
186 expect(fieldDependenciesChanged).toHaveBeenCalledWith([
187 expect.objectContaining({
188 kind: 'listener',
189 sourceField: source,
190 watchingField: target,
191 listenerInstance: expect.anything(),
192 }),
193 ])
194β
195 source.handleChange('after deletion')
196 expect(listener).not.toHaveBeenCalled()
197 })
198β
199 it('receives form mount, update, and unmount notifications', ({
200 onTestFinished,
201 }) => {
202 const form = new InternalFormApi({
203 formId: 'profile-form',
204 defaultValues: { name: '' },
205 })
206 const mountForm = vi.fn()
207 const unmountForm = vi.fn()
208 const updateForm = vi.fn()
209 const uninstall = installDevtoolsBridge({
210 mountForm,
211 unmountForm,
212 updateForm,
213 })
214 onTestFinished(uninstall)
215β
216 const unmount = form.mount()
217 onTestFinished(unmount)
218 expect(mountForm).toHaveBeenCalledWith(form)
219β
220 form._update({
221 formId: 'renamed-profile-form',
222 defaultValues: { name: '' },
223 })
224β
225 expect(updateForm).toHaveBeenCalledWith(form)
226β
227 unmount()
228β
229 expect(unmountForm).toHaveBeenCalledWith(form)
230 })
231β
232 it('stops lifecycle notifications after the bridge is uninstalled', ({
233 onTestFinished,
234 }) => {
235 const form = new InternalFormApi({
236 formId: 'profile-form',
237 defaultValues: { name: '' },
238 })
239 const mountForm = vi.fn()
240 const unmountForm = vi.fn()
241 const updateForm = vi.fn()
242 const uninstall = installDevtoolsBridge({
243 mountForm,
244 unmountForm,
245 updateForm,
246 })
247 onTestFinished(uninstall)
248β
249 const unmount = form.mount()
250 onTestFinished(unmount)
251 expect(mountForm).toHaveBeenCalledWith(form)
252 mountForm.mockClear()
253β
254 uninstall()
255β
256 const unmountAfterUninstall = form.mount()
257 onTestFinished(unmountAfterUninstall)
258 form._update({
259 formId: 'renamed-profile-form',
260 defaultValues: { name: '' },
261 })
262 unmount()
263 unmountAfterUninstall()
264β
265 expect(mountForm).not.toHaveBeenCalled()
266 expect(updateForm).not.toHaveBeenCalled()
267 expect(unmountForm).not.toHaveBeenCalled()
268 })
269})