2026-01-07 09:31:02 +00:00
|
|
|
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 () => {
|
2026-01-13 18:47:57 +00:00
|
|
|
render(<OptimizedImage src="test.jpg" alt="Test image" />);
|
|
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
await waitFor(() => {
|
|
|
|
|
const image = screen.getByAltText('Test image');
|
|
|
|
|
expect(image).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('renders placeholder while loading', () => {
|
|
|
|
|
render(
|
|
|
|
|
<OptimizedImage
|
|
|
|
|
src="test.jpg"
|
|
|
|
|
alt="Test"
|
|
|
|
|
placeholder={<div>Loading...</div>}
|
2026-01-13 18:47:57 +00:00
|
|
|
/>,
|
2026-01-07 09:31:02 +00:00
|
|
|
);
|
2026-01-13 18:47:57 +00:00
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
// Placeholder might show briefly
|
|
|
|
|
expect(screen.getByAltText('Test')).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('renders fallback on error', async () => {
|
|
|
|
|
render(
|
|
|
|
|
<OptimizedImage
|
|
|
|
|
src="invalid.jpg"
|
|
|
|
|
alt="Test"
|
|
|
|
|
fallback={<div>Image not available</div>}
|
2026-01-13 18:47:57 +00:00
|
|
|
/>,
|
2026-01-07 09:31:02 +00:00
|
|
|
);
|
2026-01-13 18:47:57 +00:00
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
// Wait for error handling
|
2026-01-13 18:47:57 +00:00
|
|
|
await waitFor(
|
|
|
|
|
() => {
|
|
|
|
|
// The component should handle errors gracefully
|
|
|
|
|
const image = screen.getByAltText('Test');
|
|
|
|
|
expect(image).toBeInTheDocument();
|
|
|
|
|
},
|
|
|
|
|
{ timeout: 1000 },
|
|
|
|
|
);
|
2026-01-07 09:31:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('applies custom className', () => {
|
|
|
|
|
const { container } = render(
|
2026-01-13 18:47:57 +00:00
|
|
|
<OptimizedImage src="test.jpg" alt="Test" className="custom-class" />,
|
2026-01-07 09:31:02 +00:00
|
|
|
);
|
2026-01-13 18:47:57 +00:00
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
const picture = container.querySelector('picture');
|
|
|
|
|
expect(picture).toHaveClass('custom-class');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('renders with width and height', () => {
|
|
|
|
|
render(
|
2026-01-13 18:47:57 +00:00
|
|
|
<OptimizedImage src="test.jpg" alt="Test" width={200} height={200} />,
|
2026-01-07 09:31:02 +00:00
|
|
|
);
|
2026-01-13 18:47:57 +00:00
|
|
|
|
2026-01-07 09:31:02 +00:00
|
|
|
const image = screen.getByAltText('Test');
|
|
|
|
|
expect(image).toHaveAttribute('width', '200');
|
|
|
|
|
expect(image).toHaveAttribute('height', '200');
|
|
|
|
|
});
|
|
|
|
|
});
|