/** * Tests for ChartCard Component */ import { render, screen } from '@testing-library/react'; import { ChartCard } from '@/components/charts/ChartCard'; describe('ChartCard', () => { const mockChildren =
Chart Content
; it('renders with title and children', () => { render({mockChildren}); expect(screen.getByText('Test Chart')).toBeInTheDocument(); expect(screen.getByText('Chart Content')).toBeInTheDocument(); }); it('renders with title and description', () => { render( {mockChildren} ); expect(screen.getByText('Test Chart')).toBeInTheDocument(); expect(screen.getByText('Test description')).toBeInTheDocument(); }); it('shows loading skeleton when loading is true', () => { render( {mockChildren} ); expect(screen.getByText('Test Chart')).toBeInTheDocument(); expect(screen.queryByText('Chart Content')).not.toBeInTheDocument(); // Skeleton should be visible const skeleton = document.querySelector('.h-\\[300px\\]'); expect(skeleton).toBeInTheDocument(); }); it('shows error alert when error is provided', () => { render( {mockChildren} ); expect(screen.getByText('Test Chart')).toBeInTheDocument(); expect(screen.getByText('Failed to load data')).toBeInTheDocument(); expect(screen.queryByText('Chart Content')).not.toBeInTheDocument(); }); it('applies custom className', () => { const { container } = render( {mockChildren} ); const card = container.firstChild; expect(card).toHaveClass('custom-class'); }); it('renders without description when not provided', () => { render({mockChildren}); expect(screen.getByText('Test Chart')).toBeInTheDocument(); expect(screen.getByText('Chart Content')).toBeInTheDocument(); // Description should not be present const cardDescription = document.querySelector('[class*="CardDescription"]'); expect(cardDescription).not.toBeInTheDocument(); }); it('prioritizes error over loading state', () => { render( {mockChildren} ); // Error should be shown expect(screen.getByText('Error message')).toBeInTheDocument(); // Loading skeleton should not be shown expect(screen.queryByText('Chart Content')).not.toBeInTheDocument(); }); });