2
0

input.test.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { render, fireEvent } from '@testing-library/react'
  2. import { Input } from '@/components/ui/input'
  3. describe('Input', () => {
  4. it('应该渲染输入框', () => {
  5. const { container } = render(<Input />)
  6. const input = container.querySelector('input')
  7. expect(input).toBeTruthy()
  8. })
  9. it('应该显示占位符文本', () => {
  10. const { getByPlaceholderText } = render(
  11. <Input placeholder="请输入内容" />
  12. )
  13. expect(getByPlaceholderText('请输入内容')).toBeTruthy()
  14. })
  15. it('应该处理输入事件', () => {
  16. const handleChange = jest.fn()
  17. const { container } = render(
  18. <Input onChange={handleChange} />
  19. )
  20. const input = container.querySelector('input') as HTMLInputElement
  21. fireEvent.input(input, { target: { value: '测试输入' } })
  22. expect(handleChange).toHaveBeenCalledWith('测试输入', expect.any(Object))
  23. })
  24. it('应该显示错误状态', () => {
  25. const { container, getByText } = render(
  26. <Input error={true} errorMessage="输入错误" />
  27. )
  28. const input = container.querySelector('input')
  29. expect(input?.className).toContain('border-red-500')
  30. expect(getByText('输入错误')).toBeTruthy()
  31. })
  32. it('应该显示左侧图标', () => {
  33. const { container } = render(
  34. <Input leftIcon="search" />
  35. )
  36. const leftIcon = container.querySelector('.absolute.left-3')
  37. expect(leftIcon).toBeTruthy()
  38. })
  39. it('应该显示右侧图标', () => {
  40. const { container } = render(
  41. <Input rightIcon="close" />
  42. )
  43. const rightIcon = container.querySelector('.absolute.right-3')
  44. expect(rightIcon).toBeTruthy()
  45. })
  46. it('应该处理左侧图标点击事件', () => {
  47. const handleLeftIconClick = jest.fn()
  48. const { container } = render(
  49. <Input leftIcon="search" onLeftIconClick={handleLeftIconClick} />
  50. )
  51. const leftIcon = container.querySelector('.absolute.left-3')
  52. fireEvent.click(leftIcon!)
  53. expect(handleLeftIconClick).toHaveBeenCalled()
  54. })
  55. it('应该处理右侧图标点击事件', () => {
  56. const handleRightIconClick = jest.fn()
  57. const { container } = render(
  58. <Input rightIcon="close" onRightIconClick={handleRightIconClick} />
  59. )
  60. const rightIcon = container.querySelector('.absolute.right-3')
  61. fireEvent.click(rightIcon!)
  62. expect(handleRightIconClick).toHaveBeenCalled()
  63. })
  64. it('应该应用不同的变体样式', () => {
  65. const { container: defaultContainer } = render(<Input variant="default" />)
  66. const { container: outlineContainer } = render(<Input variant="outline" />)
  67. const { container: filledContainer } = render(<Input variant="filled" />)
  68. const defaultInput = defaultContainer.querySelector('input')
  69. const outlineInput = outlineContainer.querySelector('input')
  70. const filledInput = filledContainer.querySelector('input')
  71. expect(defaultInput?.className).toContain('border-gray-300')
  72. expect(outlineInput?.className).toContain('border-input')
  73. expect(filledInput?.className).toContain('border-none')
  74. })
  75. it('应该应用不同的大小样式', () => {
  76. const { container: defaultContainer } = render(<Input size="default" />)
  77. const { container: smContainer } = render(<Input size="sm" />)
  78. const { container: lgContainer } = render(<Input size="lg" />)
  79. const defaultInput = defaultContainer.querySelector('input')
  80. const smInput = smContainer.querySelector('input')
  81. const lgInput = lgContainer.querySelector('input')
  82. expect(defaultInput?.className).toContain('h-10')
  83. expect(smInput?.className).toContain('h-9')
  84. expect(lgInput?.className).toContain('h-11')
  85. })
  86. it('应该禁用输入框', () => {
  87. const { container } = render(<Input disabled />)
  88. const input = container.querySelector('input')
  89. expect(input?.disabled).toBe(true)
  90. })
  91. it('应该设置输入框类型', () => {
  92. const { container } = render(<Input type="password" />)
  93. const input = container.querySelector('input')
  94. expect(input?.type).toBe('password')
  95. })
  96. it('应该设置输入框值', () => {
  97. const { container } = render(<Input value="预设值" />)
  98. const input = container.querySelector('input') as HTMLInputElement
  99. expect(input.value).toBe('预设值')
  100. })
  101. })