dify/web/app/components/base/form/hooks/use-check-validated.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

106 lines
2.8 KiB
TypeScript

import type { AnyFormApi } from '@tanstack/react-form'
import { renderHook } from '@testing-library/react'
import { FormTypeEnum } from '../types'
import { useCheckValidated } from './use-check-validated'
const mockNotify = vi.fn()
vi.mock('@/app/components/base/toast', () => ({
useToastContext: () => ({
notify: mockNotify,
}),
}))
describe('useCheckValidated', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should return true when form has no errors', () => {
const form = {
getAllErrors: () => undefined,
state: { values: {} },
}
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, []))
expect(result.current.checkValidated()).toBe(true)
expect(mockNotify).not.toHaveBeenCalled()
})
it('should notify and return false when visible field has errors', () => {
const form = {
getAllErrors: () => ({
fields: {
name: { errors: ['Name is required'] },
},
}),
state: { values: {} },
}
const schemas = [{
name: 'name',
label: 'Name',
required: true,
type: FormTypeEnum.textInput,
show_on: [],
}]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(false)
expect(mockNotify).toHaveBeenCalledWith({
type: 'error',
message: 'Name is required',
})
})
it('should ignore hidden field errors and return true', () => {
const form = {
getAllErrors: () => ({
fields: {
secret: { errors: ['Secret is required'] },
},
}),
state: { values: { enabled: 'false' } },
}
const schemas = [{
name: 'secret',
label: 'Secret',
required: true,
type: FormTypeEnum.textInput,
show_on: [{ variable: 'enabled', value: 'true' }],
}]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(true)
expect(mockNotify).not.toHaveBeenCalled()
})
it('should notify when field is shown and has errors', () => {
const form = {
getAllErrors: () => ({
fields: {
secret: { errors: ['Secret is required'] },
},
}),
state: { values: { enabled: 'true' } },
}
const schemas = [{
name: 'secret',
label: 'Secret',
required: true,
type: FormTypeEnum.textInput,
show_on: [{ variable: 'enabled', value: 'true' }],
}]
const { result } = renderHook(() => useCheckValidated(form as unknown as AnyFormApi, schemas))
expect(result.current.checkValidated()).toBe(false)
expect(mockNotify).toHaveBeenCalledWith({
type: 'error',
message: 'Secret is required',
})
})
})