您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.spec.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React from 'react'
  2. import { fireEvent, render, screen } from '@testing-library/react'
  3. import '@testing-library/jest-dom'
  4. import Input, { inputVariants } from './index'
  5. // Mock the i18n hook
  6. jest.mock('react-i18next', () => ({
  7. useTranslation: () => ({
  8. t: (key: string) => {
  9. const translations: Record<string, string> = {
  10. 'common.operation.search': 'Search',
  11. 'common.placeholder.input': 'Please input',
  12. }
  13. return translations[key] || ''
  14. },
  15. }),
  16. }))
  17. describe('Input component', () => {
  18. describe('Variants', () => {
  19. it('should return correct classes for regular size', () => {
  20. const result = inputVariants({ size: 'regular' })
  21. expect(result).toContain('px-3')
  22. expect(result).toContain('radius-md')
  23. expect(result).toContain('system-sm-regular')
  24. })
  25. it('should return correct classes for large size', () => {
  26. const result = inputVariants({ size: 'large' })
  27. expect(result).toContain('px-4')
  28. expect(result).toContain('radius-lg')
  29. expect(result).toContain('system-md-regular')
  30. })
  31. it('should use regular size as default', () => {
  32. const result = inputVariants({})
  33. expect(result).toContain('px-3')
  34. expect(result).toContain('radius-md')
  35. expect(result).toContain('system-sm-regular')
  36. })
  37. })
  38. it('renders correctly with default props', () => {
  39. render(<Input />)
  40. const input = screen.getByPlaceholderText('Please input')
  41. expect(input).toBeInTheDocument()
  42. expect(input).not.toBeDisabled()
  43. expect(input).not.toHaveClass('cursor-not-allowed')
  44. })
  45. it('shows left icon when showLeftIcon is true', () => {
  46. render(<Input showLeftIcon />)
  47. const searchIcon = document.querySelector('svg')
  48. expect(searchIcon).toBeInTheDocument()
  49. const input = screen.getByPlaceholderText('Search')
  50. expect(input).toHaveClass('pl-[26px]')
  51. })
  52. it('shows clear icon when showClearIcon is true and has value', () => {
  53. render(<Input showClearIcon value="test" />)
  54. const clearIcon = document.querySelector('.group svg')
  55. expect(clearIcon).toBeInTheDocument()
  56. const input = screen.getByDisplayValue('test')
  57. expect(input).toHaveClass('pr-[26px]')
  58. })
  59. it('does not show clear icon when disabled, even with value', () => {
  60. render(<Input showClearIcon value="test" disabled />)
  61. const clearIcon = document.querySelector('.group svg')
  62. expect(clearIcon).not.toBeInTheDocument()
  63. })
  64. it('calls onClear when clear icon is clicked', () => {
  65. const onClear = jest.fn()
  66. render(<Input showClearIcon value="test" onClear={onClear} />)
  67. const clearIconContainer = document.querySelector('.group')
  68. fireEvent.click(clearIconContainer!)
  69. expect(onClear).toHaveBeenCalledTimes(1)
  70. })
  71. it('shows warning icon when destructive is true', () => {
  72. render(<Input destructive />)
  73. const warningIcon = document.querySelector('svg')
  74. expect(warningIcon).toBeInTheDocument()
  75. const input = screen.getByPlaceholderText('Please input')
  76. expect(input).toHaveClass('border-components-input-border-destructive')
  77. })
  78. it('applies disabled styles when disabled', () => {
  79. render(<Input disabled />)
  80. const input = screen.getByPlaceholderText('Please input')
  81. expect(input).toBeDisabled()
  82. expect(input).toHaveClass('cursor-not-allowed')
  83. expect(input).toHaveClass('bg-components-input-bg-disabled')
  84. })
  85. it('displays custom unit when provided', () => {
  86. render(<Input unit="km" />)
  87. const unitElement = screen.getByText('km')
  88. expect(unitElement).toBeInTheDocument()
  89. })
  90. it('applies custom className and style', () => {
  91. const customClass = 'test-class'
  92. const customStyle = { color: 'red' }
  93. render(<Input className={customClass} styleCss={customStyle} />)
  94. const input = screen.getByPlaceholderText('Please input')
  95. expect(input).toHaveClass(customClass)
  96. expect(input).toHaveStyle('color: red')
  97. })
  98. it('applies large size variant correctly', () => {
  99. render(<Input size={'large' as any} />)
  100. const input = screen.getByPlaceholderText('Please input')
  101. expect(input.className).toContain(inputVariants({ size: 'large' }))
  102. })
  103. it('uses custom placeholder when provided', () => {
  104. const placeholder = 'Custom placeholder'
  105. render(<Input placeholder={placeholder} />)
  106. const input = screen.getByPlaceholderText(placeholder)
  107. expect(input).toBeInTheDocument()
  108. })
  109. })