dify/web/app/components/base/form/utils/secret-input/index.spec.ts
akashseth-ifp 4e142f72e8
test(base): add test coverage for more base/form components (#32437)
Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com>
2026-02-25 10:47:25 +08:00

55 lines
1.7 KiB
TypeScript

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',
})
})
})