You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.spec.tsx 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { fireEvent, render, screen } from '@testing-library/react'
  2. import '@testing-library/jest-dom'
  3. import SegmentedControl from './index'
  4. describe('SegmentedControl', () => {
  5. const options = [
  6. { value: 'option1', text: 'Option 1' },
  7. { value: 'option2', text: 'Option 2' },
  8. { value: 'option3', text: 'Option 3' },
  9. ]
  10. const optionsWithDisabled = [
  11. { value: 'option1', text: 'Option 1' },
  12. { value: 'option2', text: 'Option 2', disabled: true },
  13. { value: 'option3', text: 'Option 3' },
  14. ]
  15. const onSelectMock = jest.fn((value: string | number | symbol) => value)
  16. beforeEach(() => {
  17. onSelectMock.mockClear()
  18. })
  19. it('renders all options correctly', () => {
  20. render(<SegmentedControl options={options} value='option1' onChange={onSelectMock} />)
  21. options.forEach((option) => {
  22. expect(screen.getByText(option.text)).toBeInTheDocument()
  23. })
  24. const divider = screen.getByTestId('segmented-control-divider-1')
  25. expect(divider).toBeInTheDocument()
  26. })
  27. it('renders with custom activeClassName when provided', () => {
  28. render(
  29. <SegmentedControl
  30. options={options}
  31. value='option1'
  32. onChange={onSelectMock}
  33. activeClassName='custom-active-class'
  34. />,
  35. )
  36. const selectedOption = screen.getByText('Option 1').closest('button')
  37. expect(selectedOption).toHaveClass('custom-active-class')
  38. })
  39. it('highlights the selected option', () => {
  40. render(<SegmentedControl options={options} value='option2' onChange={onSelectMock} />)
  41. const selectedOption = screen.getByText('Option 2').closest('button')
  42. expect(selectedOption).toHaveClass('active')
  43. })
  44. it('calls onChange when an option is clicked', () => {
  45. render(<SegmentedControl options={options} value='option1' onChange={onSelectMock} />)
  46. fireEvent.click(screen.getByText('Option 3'))
  47. expect(onSelectMock).toHaveBeenCalledWith('option3')
  48. })
  49. it('does not call onChange when clicking the already selected option', () => {
  50. render(<SegmentedControl options={options} value='option1' onChange={onSelectMock} />)
  51. fireEvent.click(screen.getByText('Option 1'))
  52. expect(onSelectMock).not.toHaveBeenCalled()
  53. })
  54. it('handles disabled state correctly', () => {
  55. render(<SegmentedControl options={optionsWithDisabled} value='option1' onChange={onSelectMock} />)
  56. fireEvent.click(screen.getByText('Option 2'))
  57. expect(onSelectMock).not.toHaveBeenCalled()
  58. const optionElement = screen.getByText('Option 2').closest('button')
  59. expect(optionElement).toHaveAttribute('disabled')
  60. expect(optionElement).toHaveClass('disabled')
  61. fireEvent.click(screen.getByText('Option 3'))
  62. expect(onSelectMock).toHaveBeenCalledWith('option3')
  63. })
  64. it('renders with custom className when provided', () => {
  65. const customClass = 'my-custom-class'
  66. render(
  67. <SegmentedControl
  68. options={options}
  69. value='option1'
  70. onChange={onSelectMock}
  71. className={customClass}
  72. />,
  73. )
  74. const selectedOption = screen.getByText('Option 1').closest('button')?.closest('div')
  75. expect(selectedOption).toHaveClass(customClass)
  76. })
  77. })