Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { fireEvent, render, screen } from '@testing-library/react'
  2. import Checkbox from './index'
  3. describe('Checkbox Component', () => {
  4. const mockProps = {
  5. id: 'test',
  6. }
  7. it('renders unchecked checkbox by default', () => {
  8. render(<Checkbox {...mockProps} />)
  9. const checkbox = screen.getByTestId('checkbox-test')
  10. expect(checkbox).toBeInTheDocument()
  11. expect(checkbox).not.toHaveClass('bg-components-checkbox-bg')
  12. })
  13. it('renders checked checkbox when checked prop is true', () => {
  14. render(<Checkbox {...mockProps} checked />)
  15. const checkbox = screen.getByTestId('checkbox-test')
  16. expect(checkbox).toHaveClass('bg-components-checkbox-bg')
  17. expect(screen.getByTestId('check-icon-test')).toBeInTheDocument()
  18. })
  19. it('renders indeterminate state correctly', () => {
  20. render(<Checkbox {...mockProps} indeterminate />)
  21. expect(screen.getByTestId('indeterminate-icon')).toBeInTheDocument()
  22. })
  23. it('handles click events when not disabled', () => {
  24. const onCheck = jest.fn()
  25. render(<Checkbox {...mockProps} onCheck={onCheck} />)
  26. const checkbox = screen.getByTestId('checkbox-test')
  27. fireEvent.click(checkbox)
  28. expect(onCheck).toHaveBeenCalledTimes(1)
  29. })
  30. it('does not handle click events when disabled', () => {
  31. const onCheck = jest.fn()
  32. render(<Checkbox {...mockProps} disabled onCheck={onCheck} />)
  33. const checkbox = screen.getByTestId('checkbox-test')
  34. fireEvent.click(checkbox)
  35. expect(onCheck).not.toHaveBeenCalled()
  36. expect(checkbox).toHaveClass('cursor-not-allowed')
  37. })
  38. it('applies custom className when provided', () => {
  39. const customClass = 'custom-class'
  40. render(<Checkbox {...mockProps} className={customClass} />)
  41. const checkbox = screen.getByTestId('checkbox-test')
  42. expect(checkbox).toHaveClass(customClass)
  43. })
  44. it('applies correct styles for disabled checked state', () => {
  45. render(<Checkbox {...mockProps} checked disabled />)
  46. const checkbox = screen.getByTestId('checkbox-test')
  47. expect(checkbox).toHaveClass('bg-components-checkbox-bg-disabled-checked')
  48. expect(checkbox).toHaveClass('cursor-not-allowed')
  49. })
  50. it('applies correct styles for disabled unchecked state', () => {
  51. render(<Checkbox {...mockProps} disabled />)
  52. const checkbox = screen.getByTestId('checkbox-test')
  53. expect(checkbox).toHaveClass('bg-components-checkbox-bg-disabled')
  54. expect(checkbox).toHaveClass('cursor-not-allowed')
  55. })
  56. })