dify/web/app/components/base/checkbox/__tests__/index.spec.tsx
2026-03-12 15:09:10 +08:00

111 lines
4.0 KiB
TypeScript

import { fireEvent, render, screen } from '@testing-library/react'
import Checkbox from '../index'
describe('Checkbox Component', () => {
const mockProps = {
id: 'test',
}
it('renders unchecked checkbox by default', () => {
render(<Checkbox {...mockProps} />)
const checkbox = screen.getByTestId('checkbox-test')
expect(checkbox).toBeInTheDocument()
expect(checkbox).not.toHaveClass('bg-components-checkbox-bg')
})
it('renders checked checkbox when checked prop is true', () => {
render(<Checkbox {...mockProps} checked />)
const checkbox = screen.getByTestId('checkbox-test')
expect(checkbox).toHaveClass('bg-components-checkbox-bg')
expect(screen.getByTestId('check-icon-test')).toBeInTheDocument()
})
it('renders indeterminate state correctly', () => {
render(<Checkbox {...mockProps} indeterminate />)
expect(screen.getByTestId('indeterminate-icon')).toBeInTheDocument()
})
it('handles click events when not disabled', () => {
const onCheck = vi.fn()
render(<Checkbox {...mockProps} onCheck={onCheck} />)
const checkbox = screen.getByTestId('checkbox-test')
fireEvent.click(checkbox)
expect(onCheck).toHaveBeenCalledTimes(1)
})
it('does not handle click events when disabled', () => {
const onCheck = vi.fn()
render(<Checkbox {...mockProps} disabled onCheck={onCheck} />)
const checkbox = screen.getByTestId('checkbox-test')
fireEvent.click(checkbox)
expect(onCheck).not.toHaveBeenCalled()
expect(checkbox).toHaveClass('cursor-not-allowed')
})
it('applies custom className when provided', () => {
const customClass = 'custom-class'
render(<Checkbox {...mockProps} className={customClass} />)
const checkbox = screen.getByTestId('checkbox-test')
expect(checkbox).toHaveClass(customClass)
})
it('applies correct styles for disabled checked state', () => {
render(<Checkbox {...mockProps} checked disabled />)
const checkbox = screen.getByTestId('checkbox-test')
expect(checkbox).toHaveClass('bg-components-checkbox-bg-disabled-checked')
expect(checkbox).toHaveClass('cursor-not-allowed')
})
it('applies correct styles for disabled unchecked state', () => {
render(<Checkbox {...mockProps} disabled />)
const checkbox = screen.getByTestId('checkbox-test')
expect(checkbox).toHaveClass('bg-components-checkbox-bg-disabled')
expect(checkbox).toHaveClass('cursor-not-allowed')
})
it('handles keyboard events (Space and Enter) when not disabled', () => {
const onCheck = vi.fn()
render(<Checkbox {...mockProps} onCheck={onCheck} />)
const checkbox = screen.getByTestId('checkbox-test')
fireEvent.keyDown(checkbox, { key: ' ' })
expect(onCheck).toHaveBeenCalledTimes(1)
fireEvent.keyDown(checkbox, { key: 'Enter' })
expect(onCheck).toHaveBeenCalledTimes(2)
})
it('does not handle keyboard events when disabled', () => {
const onCheck = vi.fn()
render(<Checkbox {...mockProps} disabled onCheck={onCheck} />)
const checkbox = screen.getByTestId('checkbox-test')
fireEvent.keyDown(checkbox, { key: ' ' })
expect(onCheck).not.toHaveBeenCalled()
fireEvent.keyDown(checkbox, { key: 'Enter' })
expect(onCheck).not.toHaveBeenCalled()
})
it('exposes aria-disabled attribute', () => {
const { rerender } = render(<Checkbox {...mockProps} />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-disabled', 'false')
rerender(<Checkbox {...mockProps} disabled />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-disabled', 'true')
})
it('normalizes aria-checked attribute', () => {
const { rerender } = render(<Checkbox {...mockProps} />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-checked', 'false')
rerender(<Checkbox {...mockProps} checked />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-checked', 'true')
rerender(<Checkbox {...mockProps} indeterminate />)
expect(screen.getByTestId('checkbox-test')).toHaveAttribute('aria-checked', 'mixed')
})
})