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

adapter.test.ts

112 lines4.5 KB

1import { describe, expect, it } from 'vitest'
2import { render } from 'vitest-browser-svelte'
3import { userEvent } from 'vitest/browser'
4import BaseForm from './adapter/BaseForm.svelte'
5import AppForm from './adapter/AppForm.svelte'
6import FieldGroup from './adapter/FieldGroup.svelte'
7import IdentityFieldGroup from './adapter/IdentityFieldGroup.svelte'
8import ParityForm from './adapter/ParityForm.svelte'
9​
10describe('Svelte Form v2 adapter', () => {
11 it('renders reactive fields, arrays, subscriptions, groups, and resets', async () => {
12 const { getByLabelText, getByRole, getByTestId } = await render(BaseForm)
13​
14 const name = getByLabelText('Name')
15 await name.clear()
16 await userEvent.type(name, 'x')
17 expect(getByRole('alert')).toHaveTextContent('Name is too short')
18 await name.clear()
19 await userEvent.type(name, 'Updated')
20 expect(getByTestId('name-value')).toHaveTextContent('Updated')
21​
22 await getByRole('button', { name: 'Push' }).click()
23 expect(getByTestId('items')).toHaveTextContent('one,two')
24​
25 const guest = getByLabelText('Guest')
26 await guest.clear()
27 await userEvent.type(guest, 'Changed')
28 expect(getByTestId('group-value')).toHaveTextContent('Changed')
29​
30 await expect.element(getByTestId('visible')).not.toBeInTheDocument()
31 await getByRole('button', { name: 'Show' }).click()
32 expect(getByTestId('visible')).toBeInTheDocument()
33​
34 await getByRole('button', { name: 'Reset' }).click()
35 expect(name).toHaveValue('Rodney')
36 expect(getByTestId('items')).toHaveTextContent('reset')
37 expect(getByTestId('group-value')).toHaveTextContent('Reset Guest')
38 })
39​
40 it('provides typed app field and form contexts through form groups', async () => {
41 const { getByRole, getByTestId } = await render(AppForm)
42 expect(getByTestId('app-field')).toHaveTextContent('Name:guest.name:Tony')
43 expect(getByTestId('summary')).toHaveTextContent(
44 JSON.stringify({ guest: { name: 'Tony' } }),
45 )
46 await getByRole('button', { name: 'Update label' }).click()
47 expect(getByTestId('app-field')).toHaveTextContent(
48 'Guest name:guest.name:Tony',
49 )
50 })
51​
52 it('maps reusable logical fields and forwards methods and atoms', async () => {
53 const { getByRole, getByTestId } = await render(FieldGroup)
54 expect(getByTestId('logical-field')).toHaveTextContent(
55 'profile.name:Initial',
56 )
57 await getByRole('button', { name: 'Update logical' }).click()
58 expect(getByTestId('logical-value')).toHaveTextContent('Updated')
59 expect(getByTestId('logical-field')).toHaveTextContent(
60 'profile.name:Updated',
61 )
62 await getByRole('button', { name: 'Move logical item' }).click()
63 expect(getByTestId('logical-items')).toHaveTextContent('b,c,a')
64 })
65​
66 it('defaults omitted bindings to same-named form fields', async () => {
67 const { getByRole, getByTestId } = await render(IdentityFieldGroup)
68 expect(getByTestId('logical-field')).toHaveTextContent('name:Initial')
69 await getByRole('button', { name: 'Update logical' }).click()
70 expect(getByTestId('logical-value')).toHaveTextContent('Updated')
71 expect(getByTestId('logical-field')).toHaveTextContent('name:Updated')
72 await getByRole('button', { name: 'Move logical item' }).click()
73 expect(getByTestId('logical-items')).toHaveTextContent('b,c,a')
74 })
75​
76 it('prefixes watched fields, preserves field registration, and isolates array updates', async () => {
77 let watchedCalls = 0
78 let mountCalls = 0
79 let unmountCalls = 0
80 const { getByRole, getByTestId, unmount } = await render(ParityForm, {
81 watchedListener: () => watchedCalls++,
82 mountListener: () => mountCalls++,
83 unmountListener: () => unmountCalls++,
84 })
85​
86 await getByRole('button', { name: 'Change watched field' }).click()
87 expect(watchedCalls).toBe(1)
88​
89 const initialMounts = mountCalls
90 const initialUnmounts = unmountCalls
91 await getByRole('button', { name: 'Update options' }).click()
92 expect(mountCalls).toBe(initialMounts)
93 expect(unmountCalls).toBe(initialUnmounts)
94​
95 const initialArrayRuns = Number(
96 getByTestId('array-runs').element().textContent,
97 )
98 await getByRole('button', { name: 'Change array child' }).click()
99 expect(Number(getByTestId('array-runs').element().textContent)).toBe(
100 initialArrayRuns,
101 )
102​
103 await getByRole('button', { name: 'Push array item' }).click()
104 expect(Number(getByTestId('array-runs').element().textContent)).toBe(
105 initialArrayRuns + 1,
106 )
107 expect(getByTestId('array-length')).toHaveTextContent('2')
108​
109 await unmount()
110 expect(mountCalls - unmountCalls).toBe(0)
111 })
112})