+10









-LAN-
GitHub
twwu
crazywoola
jyong
Wu Tianwei
QuantumGhost
lyzno1
quicksand
Jyong
lyzno1
zxhlyh
Yongtao Huang
autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Joel
Copilot
nite-knite
Hanqing Zhao
gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Harry
85cda47c70
Signed-off-by: -LAN- <[email protected]> Co-authored-by: twwu <[email protected]> Co-authored-by: crazywoola <[email protected]> Co-authored-by: jyong <[email protected]> Co-authored-by: Wu Tianwei <[email protected]> Co-authored-by: QuantumGhost <[email protected]> Co-authored-by: lyzno1 <[email protected]> Co-authored-by: quicksand <[email protected]> Co-authored-by: Jyong <[email protected]> Co-authored-by: lyzno1 <[email protected]> Co-authored-by: zxhlyh <[email protected]> Co-authored-by: Yongtao Huang <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Joel <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: nite-knite <[email protected]> Co-authored-by: Hanqing Zhao <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Harry <[email protected]>
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()
|
|
})
|
|
})
|