mirror of
https://github.com/langgenius/dify.git
synced 2025-11-22 07:56:37 +00:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: twwu <twwu@dify.ai> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: lyzno1 <yuanyouhuilyz@gmail.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: lyzno1 <92089059+lyzno1@users.noreply.github.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Hanqing Zhao <sherry9277@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Harry <xh001x@hotmail.com>
99 lines
3.1 KiB
TypeScript
99 lines
3.1 KiB
TypeScript
import { fireEvent, render, screen } from '@testing-library/react'
|
|
import '@testing-library/jest-dom'
|
|
import SegmentedControl from './index'
|
|
|
|
describe('SegmentedControl', () => {
|
|
const options = [
|
|
{ value: 'option1', text: 'Option 1' },
|
|
{ value: 'option2', text: 'Option 2' },
|
|
{ value: 'option3', text: 'Option 3' },
|
|
]
|
|
|
|
const optionsWithDisabled = [
|
|
{ value: 'option1', text: 'Option 1' },
|
|
{ value: 'option2', text: 'Option 2', disabled: true },
|
|
{ value: 'option3', text: 'Option 3' },
|
|
]
|
|
|
|
const onSelectMock = jest.fn((value: string | number | symbol) => value)
|
|
|
|
beforeEach(() => {
|
|
onSelectMock.mockClear()
|
|
})
|
|
|
|
it('renders all options correctly', () => {
|
|
render(<SegmentedControl options={options} value='option1' onChange={onSelectMock} />)
|
|
|
|
options.forEach((option) => {
|
|
expect(screen.getByText(option.text)).toBeInTheDocument()
|
|
})
|
|
|
|
const divider = screen.getByTestId('segmented-control-divider-1')
|
|
expect(divider).toBeInTheDocument()
|
|
})
|
|
|
|
it('renders with custom activeClassName when provided', () => {
|
|
render(
|
|
<SegmentedControl
|
|
options={options}
|
|
value='option1'
|
|
onChange={onSelectMock}
|
|
activeClassName='custom-active-class'
|
|
/>,
|
|
)
|
|
|
|
const selectedOption = screen.getByText('Option 1').closest('button')
|
|
expect(selectedOption).toHaveClass('custom-active-class')
|
|
})
|
|
|
|
it('highlights the selected option', () => {
|
|
render(<SegmentedControl options={options} value='option2' onChange={onSelectMock} />)
|
|
|
|
const selectedOption = screen.getByText('Option 2').closest('button')
|
|
expect(selectedOption).toHaveClass('active')
|
|
})
|
|
|
|
it('calls onChange when an option is clicked', () => {
|
|
render(<SegmentedControl options={options} value='option1' onChange={onSelectMock} />)
|
|
|
|
fireEvent.click(screen.getByText('Option 3'))
|
|
expect(onSelectMock).toHaveBeenCalledWith('option3')
|
|
})
|
|
|
|
it('does not call onChange when clicking the already selected option', () => {
|
|
render(<SegmentedControl options={options} value='option1' onChange={onSelectMock} />)
|
|
|
|
fireEvent.click(screen.getByText('Option 1'))
|
|
expect(onSelectMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('handles disabled state correctly', () => {
|
|
render(<SegmentedControl options={optionsWithDisabled} value='option1' onChange={onSelectMock} />)
|
|
|
|
fireEvent.click(screen.getByText('Option 2'))
|
|
expect(onSelectMock).not.toHaveBeenCalled()
|
|
|
|
const optionElement = screen.getByText('Option 2').closest('button')
|
|
expect(optionElement).toHaveAttribute('disabled')
|
|
expect(optionElement).toHaveClass('disabled')
|
|
|
|
fireEvent.click(screen.getByText('Option 3'))
|
|
expect(onSelectMock).toHaveBeenCalledWith('option3')
|
|
})
|
|
|
|
it('renders with custom className when provided', () => {
|
|
const customClass = 'my-custom-class'
|
|
render(
|
|
<SegmentedControl
|
|
options={options}
|
|
value='option1'
|
|
onChange={onSelectMock}
|
|
className={customClass}
|
|
/>,
|
|
)
|
|
|
|
const selectedOption = screen.getByText('Option 1').closest('button')?.closest('div')
|
|
expect(selectedOption).toHaveClass(customClass)
|
|
})
|
|
})
|