import { describe, it, expect, vi, beforeEach } from 'vitest'; import { render, screen, fireEvent } from '@testing-library/react'; import { BrowserRouter } from 'react-router-dom'; // import { useTranslation } from '@/hooks/useTranslation' import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { FocusTrap } from '@/components/ui/focus-trap'; // Mock useTranslation vi.mock('@/hooks/useTranslation', () => ({ useTranslation: vi.fn(() => ({ t: (key: string) => key, i18n: { changeLanguage: vi.fn() }, language: 'en', changeLanguage: vi.fn(), isReady: true, })), })); // Mock i18n vi.mock('@/lib/i18n', () => ({ default: { changeLanguage: vi.fn(), isInitialized: true, }, })); const TestWrapper = ({ children }: { children: React.ReactNode }) => ( {children} ); describe('UI Components', () => { beforeEach(() => { vi.clearAllMocks(); }); describe('Button', () => { it('should render with default props', () => { render(); expect( screen.getByRole('button', { name: 'Test Button' }), ).toBeInTheDocument(); }); it('should render with different variants', () => { render(); const button = screen.getByRole('button', { name: 'Delete' }); expect(button).toBeInTheDocument(); // Destructive variant uses bg-destructive/10 with text-destructive expect(button).toHaveClass('text-destructive'); }); it('should handle click events', () => { const handleClick = vi.fn(); render(); fireEvent.click(screen.getByRole('button')); expect(handleClick).toHaveBeenCalledTimes(1); }); it('should be disabled when disabled prop is true', () => { render(); const button = screen.getByRole('button', { name: 'Disabled Button' }); expect(button).toBeDisabled(); }); it('should render as child when asChild is true', () => { render( , ); expect( screen.getByRole('link', { name: 'Link Button' }), ).toBeInTheDocument(); }); }); describe('Input', () => { it('should render with default props', () => { render(); expect(screen.getByPlaceholderText('Enter text')).toBeInTheDocument(); }); it('should handle value changes', () => { const handleChange = vi.fn(); render(); const input = screen.getByRole('textbox'); fireEvent.change(input, { target: { value: 'test input' } }); expect(handleChange).toHaveBeenCalled(); expect(input).toHaveValue('test input'); }); it('should support different input types', () => { render(); const input = screen.getByPlaceholderText('Enter email'); expect(input).toHaveAttribute('type', 'email'); }); it('should be disabled when disabled prop is true', () => { render(); const input = screen.getByPlaceholderText('Disabled input'); expect(input).toBeDisabled(); }); }); describe('FocusTrap', () => { it('should render children when active', () => { render(
Test content
, ); expect(screen.getByText('Test content')).toBeInTheDocument(); }); it('should not render children when inactive', () => { render(
Test content
, ); expect(screen.getByText('Test content')).toBeInTheDocument(); }); it('should call onEscape when Escape key is pressed', () => { const onEscape = vi.fn(); render(
Test content
, ); fireEvent.keyDown(document, { key: 'Escape' }); expect(onEscape).toHaveBeenCalledTimes(1); }); }); }); describe('Integration Tests', () => { it('should work with React Router', () => { render( , ); const link = screen.getByRole('link', { name: 'Go to Dashboard' }); expect(link).toBeInTheDocument(); expect(link).toHaveAttribute('href', '/dashboard'); }); it('should handle form interactions', async () => { const handleSubmit = vi.fn((e) => e.preventDefault()); render(
, ); const input = screen.getByPlaceholderText('Username'); const button = screen.getByRole('button', { name: 'Submit' }); fireEvent.change(input, { target: { value: 'testuser' } }); fireEvent.click(button); expect(handleSubmit).toHaveBeenCalledTimes(1); }); });