Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,445 changes: 1,389 additions & 56 deletions packages/form-core/tests/FieldApi/FieldApi.lib.spec.ts

Large diffs are not rendered by default.

555 changes: 0 additions & 555 deletions packages/form-core/tests/FieldApi/array-methods.spec.ts

This file was deleted.

123 changes: 0 additions & 123 deletions packages/form-core/tests/FieldApi/default-value.spec.ts

This file was deleted.

136 changes: 136 additions & 0 deletions packages/form-core/tests/FieldApi/fieldTraversal.lib.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { describe, expect, it } from 'vitest'
import { InternalFormApi } from '../../src/FormApi/FormApi.lib'
import {
collectFieldSubtree,
visitAllFormFields,
visitFieldAndAncestors,
visitFieldSubtree,
} from '../../src/FieldApi/fieldTraversal.lib'

function createFieldTree() {
const form = new InternalFormApi({
defaultValues: {
section: { first: { deep: '' }, second: '' },
other: '',
},
})
const section = form._getOrCreateFieldApi({ name: 'section' })
form._getOrCreateFieldApi({ name: 'section.first' })
const deep = form._getOrCreateFieldApi({ name: 'section.first.deep' })
form._getOrCreateFieldApi({ name: 'section.second' })
form._getOrCreateFieldApi({ name: 'other' })

return { form, section, deep }
}

describe('visitFieldAndAncestors', () => {
it('visits a field and its ancestors without visiting the synthetic root', () => {
const { deep } = createFieldTree()
const names: Array<string> = []

visitFieldAndAncestors(deep, (field) => {
names.push(field.name)
})

expect(names).toEqual(['section.first.deep', 'section.first', 'section'])
})

it('stops before visiting further ancestors when the visitor returns stop', () => {
const { deep } = createFieldTree()
const names: Array<string> = []

visitFieldAndAncestors(deep, (field, stop) => {
names.push(field.name)
if (field.name === 'section.first') return stop
return undefined
})

expect(names).toEqual(['section.first.deep', 'section.first'])
})
})

describe('visitFieldSubtree', () => {
it('visits the field and its descendants, parents before children', () => {
const { section } = createFieldTree()
const names: Array<string> = []

visitFieldSubtree(section, (field) => {
names.push(field.name)
})

const firstIndex = names.indexOf('section.first')
const deepIndex = names.indexOf('section.first.deep')

expect([...names].sort()).toEqual([
'section',
'section.first',
'section.first.deep',
'section.second',
])
expect(names[0]).toBe('section')
expect(firstIndex).toBeLessThan(deepIndex)
})

it('stops before visiting any more fields when the visitor returns stop', () => {
const { section } = createFieldTree()
const names: Array<string> = []

visitFieldSubtree(section, (field, stop) => {
names.push(field.name)
if (field.name !== 'section') return stop
return undefined
})

expect(names).toEqual([
'section',
expect.stringMatching(/^section\.(first|second)$/),
])
})
})

describe('visitAllFormFields', () => {
it('visits every field, parents before children, without visiting the synthetic root', () => {
const { form } = createFieldTree()
const names: Array<string> = []

visitAllFormFields(form._fieldRootNode, (field) => {
names.push(field.name)
})

const sectionIndex = names.indexOf('section')
const firstIndex = names.indexOf('section.first')
const deepIndex = names.indexOf('section.first.deep')
const secondIndex = names.indexOf('section.second')

expect([...names].sort()).toEqual([
'other',
'section',
'section.first',
'section.first.deep',
'section.second',
])
expect(sectionIndex).toBeLessThan(firstIndex)
expect(sectionIndex).toBeLessThan(secondIndex)
expect(firstIndex).toBeLessThan(deepIndex)
})
})

describe('collectFieldSubtree', () => {
it('collects the field and its descendants, parents before children', () => {
const { section } = createFieldTree()

const fields = collectFieldSubtree(section)
const names = fields.map((field) => field.name)
const firstIndex = names.indexOf('section.first')
const deepIndex = names.indexOf('section.first.deep')

expect([...names].sort()).toEqual([
'section',
'section.first',
'section.first.deep',
'section.second',
])
expect(names[0]).toBe('section')
expect(firstIndex).toBeLessThan(deepIndex)
})
})
98 changes: 0 additions & 98 deletions packages/form-core/tests/FieldApi/fieldTraversal.spec.ts

This file was deleted.

Loading
Loading