import { render, screen, waitFor } from '@testing-library/react'; import { describe, it, expect, vi, beforeEach } from 'vitest'; import { OptimizedImage } from './optimized-image'; // Mock useIntersectionObserver vi.mock('@/hooks/useIntersectionObserver', () => ({ useIntersectionObserver: () => ({ isIntersecting: true }), })); describe('OptimizedImage Component', () => { beforeEach(() => { vi.clearAllMocks(); }); it('renders image with src', async () => { render(); await waitFor(() => { const image = screen.getByAltText('Test image'); expect(image).toBeInTheDocument(); }); }); it('renders placeholder while loading', () => { render( Loading...} />, ); // Placeholder might show briefly expect(screen.getByAltText('Test')).toBeInTheDocument(); }); it('renders fallback on error', async () => { render( Image not available} />, ); // Wait for error handling await waitFor( () => { // The component should handle errors gracefully const image = screen.getByAltText('Test'); expect(image).toBeInTheDocument(); }, { timeout: 1000 }, ); }); it('applies custom className', () => { const { container } = render( , ); const picture = container.querySelector('picture'); expect(picture).toHaveClass('custom-class'); }); it('renders with width and height', () => { render( , ); const image = screen.getByAltText('Test'); expect(image).toHaveAttribute('width', '200'); expect(image).toHaveAttribute('height', '200'); }); });