Skip to content

Commit d2dbc5e

Browse files
authored
[v2] Migrate more unit tests (#2441)
* chore: add createErrorMap to submit context * chore: migrate unit tests for lifecycle * chore: migrate field-state.spec.ts * chore: migrate array-methods unit tests * fix: satisfy knip and fix tsconfig * fix: fix tsconfig for real this time * chore: migrate unit tests for field array methods * chore: migrate unit tests for field handleChange * chore: migrate unit tests for default value * chore: migrate unit tests for form default value * chore: migrate unit tests for utils.spec.ts * chore: migrate field traversal * chore: migrate unit test for listeners.spec.ts * chore: migrate field listener spec * fix: correct faulty unit test * chore: refactor tests for node 18 compat
1 parent f0997b4 commit d2dbc5e

17 files changed

Lines changed: 2103 additions & 2254 deletions

‎packages/form-core/tests/FieldApi/FieldApi.lib.spec.ts‎

Lines changed: 1389 additions & 56 deletions
Large diffs are not rendered by default.

‎packages/form-core/tests/FieldApi/array-methods.spec.ts‎

Lines changed: 0 additions & 555 deletions
This file was deleted.

‎packages/form-core/tests/FieldApi/default-value.spec.ts‎

Lines changed: 0 additions & 123 deletions
This file was deleted.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { InternalFormApi } from '../../src/FormApi/FormApi.lib'
3+
import {
4+
collectFieldSubtree,
5+
visitAllFormFields,
6+
visitFieldAndAncestors,
7+
visitFieldSubtree,
8+
} from '../../src/FieldApi/fieldTraversal.lib'
9+
10+
function createFieldTree() {
11+
const form = new InternalFormApi({
12+
defaultValues: {
13+
section: { first: { deep: '' }, second: '' },
14+
other: '',
15+
},
16+
})
17+
const section = form._getOrCreateFieldApi({ name: 'section' })
18+
form._getOrCreateFieldApi({ name: 'section.first' })
19+
const deep = form._getOrCreateFieldApi({ name: 'section.first.deep' })
20+
form._getOrCreateFieldApi({ name: 'section.second' })
21+
form._getOrCreateFieldApi({ name: 'other' })
22+
23+
return { form, section, deep }
24+
}
25+
26+
describe('visitFieldAndAncestors', () => {
27+
it('visits a field and its ancestors without visiting the synthetic root', () => {
28+
const { deep } = createFieldTree()
29+
const names: Array<string> = []
30+
31+
visitFieldAndAncestors(deep, (field) => {
32+
names.push(field.name)
33+
})
34+
35+
expect(names).toEqual(['section.first.deep', 'section.first', 'section'])
36+
})
37+
38+
it('stops before visiting further ancestors when the visitor returns stop', () => {
39+
const { deep } = createFieldTree()
40+
const names: Array<string> = []
41+
42+
visitFieldAndAncestors(deep, (field, stop) => {
43+
names.push(field.name)
44+
if (field.name === 'section.first') return stop
45+
return undefined
46+
})
47+
48+
expect(names).toEqual(['section.first.deep', 'section.first'])
49+
})
50+
})
51+
52+
describe('visitFieldSubtree', () => {
53+
it('visits the field and its descendants, parents before children', () => {
54+
const { section } = createFieldTree()
55+
const names: Array<string> = []
56+
57+
visitFieldSubtree(section, (field) => {
58+
names.push(field.name)
59+
})
60+
61+
const firstIndex = names.indexOf('section.first')
62+
const deepIndex = names.indexOf('section.first.deep')
63+
64+
expect([...names].sort()).toEqual([
65+
'section',
66+
'section.first',
67+
'section.first.deep',
68+
'section.second',
69+
])
70+
expect(names[0]).toBe('section')
71+
expect(firstIndex).toBeLessThan(deepIndex)
72+
})
73+
74+
it('stops before visiting any more fields when the visitor returns stop', () => {
75+
const { section } = createFieldTree()
76+
const names: Array<string> = []
77+
78+
visitFieldSubtree(section, (field, stop) => {
79+
names.push(field.name)
80+
if (field.name !== 'section') return stop
81+
return undefined
82+
})
83+
84+
expect(names).toEqual([
85+
'section',
86+
expect.stringMatching(/^section\.(first|second)$/),
87+
])
88+
})
89+
})
90+
91+
describe('visitAllFormFields', () => {
92+
it('visits every field, parents before children, without visiting the synthetic root', () => {
93+
const { form } = createFieldTree()
94+
const names: Array<string> = []
95+
96+
visitAllFormFields(form._fieldRootNode, (field) => {
97+
names.push(field.name)
98+
})
99+
100+
const sectionIndex = names.indexOf('section')
101+
const firstIndex = names.indexOf('section.first')
102+
const deepIndex = names.indexOf('section.first.deep')
103+
const secondIndex = names.indexOf('section.second')
104+
105+
expect([...names].sort()).toEqual([
106+
'other',
107+
'section',
108+
'section.first',
109+
'section.first.deep',
110+
'section.second',
111+
])
112+
expect(sectionIndex).toBeLessThan(firstIndex)
113+
expect(sectionIndex).toBeLessThan(secondIndex)
114+
expect(firstIndex).toBeLessThan(deepIndex)
115+
})
116+
})
117+
118+
describe('collectFieldSubtree', () => {
119+
it('collects the field and its descendants, parents before children', () => {
120+
const { section } = createFieldTree()
121+
122+
const fields = collectFieldSubtree(section)
123+
const names = fields.map((field) => field.name)
124+
const firstIndex = names.indexOf('section.first')
125+
const deepIndex = names.indexOf('section.first.deep')
126+
127+
expect([...names].sort()).toEqual([
128+
'section',
129+
'section.first',
130+
'section.first.deep',
131+
'section.second',
132+
])
133+
expect(names[0]).toBe('section')
134+
expect(firstIndex).toBeLessThan(deepIndex)
135+
})
136+
})

‎packages/form-core/tests/FieldApi/fieldTraversal.spec.ts‎

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)