|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { Input } from '../input'; |
| 4 | + |
| 5 | +describe('Input Component', () => { |
| 6 | + it('renders input with default props', () => { |
| 7 | + render(<Input />); |
| 8 | + |
| 9 | + const input = screen.getByRole('textbox'); |
| 10 | + expect(input).toBeInTheDocument(); |
| 11 | + expect(input).toHaveAttribute('data-slot', 'input'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('renders input with placeholder', () => { |
| 15 | + render(<Input placeholder="Enter text" />); |
| 16 | + |
| 17 | + const input = screen.getByPlaceholderText('Enter text'); |
| 18 | + expect(input).toBeInTheDocument(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('renders input with value', () => { |
| 22 | + render(<Input value="test value" readOnly />); |
| 23 | + |
| 24 | + const input = screen.getByDisplayValue('test value'); |
| 25 | + expect(input).toBeInTheDocument(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('handles input changes', async () => { |
| 29 | + const handleChange = jest.fn(); |
| 30 | + const user = userEvent.setup(); |
| 31 | + |
| 32 | + render(<Input onChange={handleChange} />); |
| 33 | + |
| 34 | + const input = screen.getByRole('textbox'); |
| 35 | + await user.type(input, 'test'); |
| 36 | + |
| 37 | + expect(handleChange).toHaveBeenCalled(); |
| 38 | + expect(input).toHaveValue('test'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('renders different input types', () => { |
| 42 | + const { rerender } = render(<Input type="email" data-testid="input" />); |
| 43 | + |
| 44 | + let input = screen.getByTestId('input'); |
| 45 | + expect(input).toHaveAttribute('type', 'email'); |
| 46 | + |
| 47 | + rerender(<Input type="password" data-testid="input" />); |
| 48 | + input = screen.getByTestId('input'); |
| 49 | + expect(input).toHaveAttribute('type', 'password'); |
| 50 | + |
| 51 | + rerender(<Input type="number" data-testid="input" />); |
| 52 | + input = screen.getByTestId('input'); |
| 53 | + expect(input).toHaveAttribute('type', 'number'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('is disabled when disabled prop is true', () => { |
| 57 | + render(<Input disabled />); |
| 58 | + |
| 59 | + const input = screen.getByRole('textbox'); |
| 60 | + expect(input).toBeDisabled(); |
| 61 | + expect(input).toHaveClass('disabled:opacity-50'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('applies custom className', () => { |
| 65 | + render(<Input className="custom-input-class" data-testid="input" />); |
| 66 | + |
| 67 | + const input = screen.getByTestId('input'); |
| 68 | + expect(input).toHaveClass('custom-input-class'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('applies default input classes', () => { |
| 72 | + render(<Input data-testid="input" />); |
| 73 | + |
| 74 | + const input = screen.getByTestId('input'); |
| 75 | + expect(input).toHaveClass('rounded-md', 'border', 'h-9'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('forwards additional props', () => { |
| 79 | + render(<Input data-testid="input" aria-label="Test input" name="test-input" id="test-id" />); |
| 80 | + |
| 81 | + const input = screen.getByTestId('input'); |
| 82 | + expect(input).toHaveAttribute('aria-label', 'Test input'); |
| 83 | + expect(input).toHaveAttribute('name', 'test-input'); |
| 84 | + expect(input).toHaveAttribute('id', 'test-id'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('handles aria-invalid attribute', () => { |
| 88 | + render(<Input aria-invalid="true" data-testid="input" />); |
| 89 | + |
| 90 | + const input = screen.getByTestId('input'); |
| 91 | + expect(input).toHaveAttribute('aria-invalid', 'true'); |
| 92 | + expect(input).toHaveClass('aria-invalid:border-destructive'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments