Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 InternalFieldGroupApi,
4 defineFieldGroupFieldsRuntime,
5 fieldGroupHelperRuntime,
6} from '../../src/FieldGroup/FieldGroupApi.lib'
7import { InternalFormApi } from '../../src/FormApi/FormApi.lib'
8β
9function mergeWithDescriptors<TProps extends object>(
10 props: TProps,
11 overrides: Partial<TProps>,
12): TProps {
13 return Object.defineProperties(
14 Object.defineProperties({}, Object.getOwnPropertyDescriptors(props)),
15 Object.getOwnPropertyDescriptors(overrides),
16 ) as TProps
17}
18β
19describe('InternalFieldGroupApi', () => {
20 it('defaults omitted bindings to same-named form fields', () => {
21 const form = new InternalFormApi({ defaultValues: { name: 'Tony' } })
22 const group = new InternalFieldGroupApi({
23 form,
24 fieldNames: ['name'],
25 getBindings: () => undefined,
26 })
27β
28 expect(group.getFieldValue('name')).toBe('Tony')
29 expect(group.atom.get()).toEqual({ name: 'Tony' })
30β
31 group.setFieldValue('name', 'Rodney')
32 expect(form.getFieldValue('name')).toBe('Rodney')
33 })
34β
35 it('resolves logical names, follows binding changes, and exposes values', () => {
36 const form = new InternalFormApi({
37 defaultValues: {
38 first: { name: 'Tony' },
39 second: { name: 'Rodney' },
40 },
41 })
42 let bindings = { profile: 'first' }
43 const group = new InternalFieldGroupApi({
44 form,
45 fieldNames: ['profile.name'],
46 getBindings: () => bindings,
47 })
48β
49 expect(group.getFieldValue('profile.name')).toBe('Tony')
50 expect(group.atom.get()).toEqual({ 'profile.name': 'Tony' })
51β
52 group.setFieldValue('profile.name', 'Updated')
53 expect(form.getFieldValue('first.name')).toBe('Updated')
54 expect(group.atom.get()).toEqual({ 'profile.name': 'Updated' })
55β
56 group.resetField('profile.name')
57 expect(group.getFieldValue('profile.name')).toBe('Tony')
58β
59 bindings = { profile: 'second' }
60 expect(group.getFieldValue('profile.name')).toBe('Rodney')
61 })
62β
63 it('throws when a logical field has no binding', () => {
64 const form = new InternalFormApi({ defaultValues: { name: '' } })
65 const group = new InternalFieldGroupApi({
66 form,
67 fieldNames: ['missing'],
68 getBindings: () => ({}),
69 })
70β
71 expect(() => group.getFieldValue('missing')).toThrow(
72 'TanStack Form: Missing field group binding for "missing".',
73 )
74 })
75β
76 it('keeps transformed field option overrides lazy', () => {
77 const form = new InternalFormApi({
78 defaultValues: {
79 account: {
80 confirmation: '',
81 backup: '',
82 password: '',
83 email: '',
84 },
85 },
86 })
87 const bindings = {
88 confirmation: 'account.confirmation',
89 backup: 'account.backup',
90 password: 'account.password',
91 email: 'account.email',
92 }
93 const group = new InternalFieldGroupApi({
94 form,
95 fieldNames: ['confirmation'],
96 getBindings: () => bindings,
97 })
98 let fieldName = 'confirmation'
99 let watchedFieldName = 'password'
100 const props = {
101 get name() {
102 return fieldName
103 },
104 get validators() {
105 return [{ watchFields: [watchedFieldName] }]
106 },
107 get listeners() {
108 return [{ watchFields: [watchedFieldName] }]
109 },
110 }
111β
112 const options = group._getFormFieldOptions(
113 props as never,
114 mergeWithDescriptors,
115 )
116β
117 expect(options.name).toBe('account.confirmation')
118 expect(options.validators?.[0]?.watchFields).toEqual(['account.password'])
119 expect(options.listeners?.[0]?.watchFields).toEqual(['account.password'])
120β
121 fieldName = 'backup'
122 watchedFieldName = 'email'
123β
124 expect(options.name).toBe('account.backup')
125 expect(options.validators?.[0]?.watchFields).toEqual(['account.email'])
126 expect(options.listeners?.[0]?.watchFields).toEqual(['account.email'])
127 })
128β
129 it('forwards every array method through the current binding', () => {
130 const form = new InternalFormApi({
131 defaultValues: { lists: { primary: ['a', 'b', 'c'] } },
132 })
133 const group = new InternalFieldGroupApi({
134 form,
135 fieldNames: ['items'],
136 getBindings: () => ({ items: 'lists.primary' }),
137 })
138β
139 group.swapFieldValues('items', 0, 2)
140 expect(group.getFieldValue('items')).toEqual(['c', 'b', 'a'])
141β
142 group.moveFieldValue('items', 2, 0)
143 expect(group.getFieldValue('items')).toEqual(['a', 'c', 'b'])
144β
145 group.pushFieldValue('items', 'd')
146 group.insertFieldValue('items', 1, 'x')
147 group.removeFieldValue('items', 2)
148 group.filterFieldValues('items', (value) => value !== 'x')
149 expect(group.getFieldValue('items')).toEqual(['a', 'b', 'd'])
150β
151 group.clearFieldValues('items')
152 expect(group.getFieldValue('items')).toEqual([])
153 })
154β
155 it('shares the runtime implementations behind typed field definitions', () => {
156 const fields = { name: fieldGroupHelperRuntime.strict() }
157β
158 expect(fieldGroupHelperRuntime.loose()).toBeNull()
159 expect(defineFieldGroupFieldsRuntime(fields)).toBe(fields)
160 })
161})