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