import { fireEvent, render, screen } from '@testing-library/react' import * as React from 'react' import Input, { inputVariants } from './index' // Mock the i18n hook vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string, options?: { ns?: string }) => { const translations: Record = { 'operation.search': 'Search', 'placeholder.input': 'Please input', } if (translations[key]) return translations[key] const prefix = options?.ns ? `${options.ns}.` : '' return `${prefix}${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() 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() 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() 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() const clearIcon = document.querySelector('.group svg') expect(clearIcon).not.toBeInTheDocument() }) it('calls onClear when clear icon is clicked', () => { const onClear = vi.fn() render() const clearIconContainer = document.querySelector('.group') fireEvent.click(clearIconContainer!) expect(onClear).toHaveBeenCalledTimes(1) }) it('shows warning icon when destructive is true', () => { render() 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() 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() const unitElement = screen.getByText('km') expect(unitElement).toBeInTheDocument() }) it('applies custom className and style', () => { const customClass = 'test-class' const customStyle = { color: 'red' } render() const input = screen.getByPlaceholderText('Please input') expect(input).toHaveClass(customClass) expect(input).toHaveStyle({ color: 'rgb(255, 0, 0)' }) }) it('applies large size variant correctly', () => { render() const input = screen.getByPlaceholderText('Please input') expect(input.className).toContain(inputVariants({ size: 'large' })) }) it('uses custom placeholder when provided', () => { const placeholder = 'Custom placeholder' render() const input = screen.getByPlaceholderText(placeholder) expect(input).toBeInTheDocument() }) })