mirror of
https://github.com/langgenius/dify.git
synced 2025-11-06 22:04:31 +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>
98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
import { fireEvent, render, screen } from '@testing-library/react'
|
|
import { InputNumber } from './index'
|
|
|
|
jest.mock('react-i18next', () => ({
|
|
useTranslation: () => ({
|
|
t: (key: string) => key,
|
|
}),
|
|
}))
|
|
|
|
describe('InputNumber Component', () => {
|
|
const defaultProps = {
|
|
onChange: jest.fn(),
|
|
}
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks()
|
|
})
|
|
|
|
it('renders input with default values', () => {
|
|
render(<InputNumber {...defaultProps} />)
|
|
const input = screen.getByRole('spinbutton')
|
|
expect(input).toBeInTheDocument()
|
|
})
|
|
|
|
it('handles increment button click', () => {
|
|
render(<InputNumber {...defaultProps} value={5} />)
|
|
const incrementBtn = screen.getByRole('button', { name: /increment/i })
|
|
|
|
fireEvent.click(incrementBtn)
|
|
expect(defaultProps.onChange).toHaveBeenCalledWith(6)
|
|
})
|
|
|
|
it('handles decrement button click', () => {
|
|
render(<InputNumber {...defaultProps} value={5} />)
|
|
const decrementBtn = screen.getByRole('button', { name: /decrement/i })
|
|
|
|
fireEvent.click(decrementBtn)
|
|
expect(defaultProps.onChange).toHaveBeenCalledWith(4)
|
|
})
|
|
|
|
it('respects max value constraint', () => {
|
|
render(<InputNumber {...defaultProps} value={10} max={10} />)
|
|
const incrementBtn = screen.getByRole('button', { name: /increment/i })
|
|
|
|
fireEvent.click(incrementBtn)
|
|
expect(defaultProps.onChange).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('respects min value constraint', () => {
|
|
render(<InputNumber {...defaultProps} value={0} min={0} />)
|
|
const decrementBtn = screen.getByRole('button', { name: /decrement/i })
|
|
|
|
fireEvent.click(decrementBtn)
|
|
expect(defaultProps.onChange).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('handles direct input changes', () => {
|
|
render(<InputNumber {...defaultProps} />)
|
|
const input = screen.getByRole('spinbutton')
|
|
|
|
fireEvent.change(input, { target: { value: '42' } })
|
|
expect(defaultProps.onChange).toHaveBeenCalledWith(42)
|
|
})
|
|
|
|
it('handles empty input', () => {
|
|
render(<InputNumber {...defaultProps} value={1} />)
|
|
const input = screen.getByRole('spinbutton')
|
|
|
|
fireEvent.change(input, { target: { value: '' } })
|
|
expect(defaultProps.onChange).toHaveBeenCalledWith(0)
|
|
})
|
|
|
|
it('handles invalid input', () => {
|
|
render(<InputNumber {...defaultProps} />)
|
|
const input = screen.getByRole('spinbutton')
|
|
|
|
fireEvent.change(input, { target: { value: 'abc' } })
|
|
expect(defaultProps.onChange).toHaveBeenCalledWith(0)
|
|
})
|
|
|
|
it('displays unit when provided', () => {
|
|
const unit = 'px'
|
|
render(<InputNumber {...defaultProps} unit={unit} />)
|
|
expect(screen.getByText(unit)).toBeInTheDocument()
|
|
})
|
|
|
|
it('disables controls when disabled prop is true', () => {
|
|
render(<InputNumber {...defaultProps} disabled />)
|
|
const input = screen.getByRole('spinbutton')
|
|
const incrementBtn = screen.getByRole('button', { name: /increment/i })
|
|
const decrementBtn = screen.getByRole('button', { name: /decrement/i })
|
|
|
|
expect(input).toBeDisabled()
|
|
expect(incrementBtn).toBeDisabled()
|
|
expect(decrementBtn).toBeDisabled()
|
|
})
|
|
})
|