test(base): add test coverage for more base/form components (#32437)

Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com>
This commit is contained in:
akashseth-ifp
2026-02-25 08:17:25 +05:30
committed by GitHub
parent a6456da393
commit 4e142f72e8
22 changed files with 1393 additions and 0 deletions

View File

@ -0,0 +1,54 @@
import type { AnyFormApi } from '@tanstack/react-form'
import { FormTypeEnum } from '../../types'
import { getTransformedValuesWhenSecretInputPristine, transformFormSchemasSecretInput } from './index'
describe('secret input utilities', () => {
it('should mask only selected truthy values in transformFormSchemasSecretInput', () => {
expect(transformFormSchemasSecretInput(['apiKey'], {
apiKey: 'secret',
token: 'token-value',
emptyValue: '',
})).toEqual({
apiKey: '[__HIDDEN__]',
token: 'token-value',
emptyValue: '',
})
})
it('should mask pristine secret input fields from form state', () => {
const formSchemas = [
{ name: 'apiKey', type: FormTypeEnum.secretInput, label: 'API Key', required: true },
{ name: 'name', type: FormTypeEnum.textInput, label: 'Name', required: true },
]
const form = {
store: {
state: {
values: {
apiKey: 'secret',
name: 'Alice',
},
},
},
getFieldMeta: (name: string) => ({ isPristine: name === 'apiKey' }),
}
expect(getTransformedValuesWhenSecretInputPristine(formSchemas, form as unknown as AnyFormApi)).toEqual({
apiKey: '[__HIDDEN__]',
name: 'Alice',
})
})
it('should keep value unchanged when secret input is not pristine', () => {
const formSchemas = [
{ name: 'apiKey', type: FormTypeEnum.secretInput, label: 'API Key', required: true },
]
const form = {
store: { state: { values: { apiKey: 'secret' } } },
getFieldMeta: () => ({ isPristine: false }),
}
expect(getTransformedValuesWhenSecretInputPristine(formSchemas, form as unknown as AnyFormApi)).toEqual({
apiKey: 'secret',
})
})
})

View File

@ -0,0 +1,39 @@
import * as z from 'zod'
import { zodSubmitValidator } from './zod-submit-validator'
describe('zodSubmitValidator', () => {
it('should return undefined for valid values', () => {
const validator = zodSubmitValidator(z.object({
name: z.string().min(2),
}))
expect(validator({ value: { name: 'Alice' } })).toBeUndefined()
})
it('should return first error message per field for invalid values', () => {
const validator = zodSubmitValidator(z.object({
name: z.string().min(3, 'Name too short'),
age: z.number().min(18, 'Must be adult'),
}))
expect(validator({ value: { name: 'Al', age: 15 } })).toEqual({
fields: {
name: 'Name too short',
age: 'Must be adult',
},
})
})
it('should ignore root-level issues without a field path', () => {
const schema = z.object({ value: z.number() }).superRefine((_value, ctx) => {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Root error',
path: [],
})
})
const validator = zodSubmitValidator(schema)
expect(validator({ value: { value: 1 } })).toEqual({ fields: {} })
})
})