/** @vitest-environment jsdom */ import { render, screen } from '@testing-library/react'; import { describe, it, expect, vi } from 'vitest'; import { ChartContainer, ChartTooltipContent, ChartCard, DashboardBarChart, DashboardLineChart, HorizontalBarChart, ComboMetricChart, DonutBreakdownChart, } from './chart'; import React from 'react'; // Mock recharts vi.mock('recharts', () => ({ ResponsiveContainer: ({ children }: any) =>
{children}
, Legend: ({ children }: any) =>
{children}
, Tooltip: ({ content, payload, active }: any) => { if (typeof content === 'function') return content({ payload, active }); return React.cloneElement(content, { payload, active }); }, BarChart: ({ children }: any) =>
{children}
, LineChart: ({ children }: any) =>
{children}
, AreaChart: ({ children }: any) =>
{children}
, ComposedChart: ({ children }: any) =>
{children}
, PieChart: ({ children }: any) =>
{children}
, CartesianGrid: ({ children }: any) =>
{children}
, XAxis: ({ children }: any) =>
{children}
, YAxis: ({ children }: any) =>
{children}
, Bar: ({ children, dataKey, barSize }: any) => (
{children}
), Area: ({ children, dataKey }: any) =>
{children}
, Line: ({ children, dataKey }: any) =>
{children}
, Pie: ({ children }: any) =>
{children}
, Cell: ({ children }: any) =>
{children}
, Label: ({ children }: any) =>
{children}
, })); describe('Chart Components', () => { const config = { label1: { label: 'Label 1', color: '#000' }, }; it('renders ChartContainer correctly', () => { render(
Chart Content
); expect(screen.getByTestId('chart-child')).toBeInTheDocument(); }); it('renders ChartTooltipContent correctly', () => { const payload = [{ name: 'item1', value: 10, fill: '#000', dataKey: 'label1', graphicalItemId: 'item1' }]; // Test the content component directly since Tooltip is tricky render( ); expect(screen.getByText('Label 1')).toBeInTheDocument(); expect(screen.getByText('10')).toBeInTheDocument(); }); it('renders ChartCard structure', () => { render(
Chart body
); expect(screen.getByText('Revenue')).toBeInTheDocument(); expect(screen.getByText('Monthly view')).toBeInTheDocument(); expect(screen.getByText('Chart body')).toBeInTheDocument(); expect(screen.getByText('Updated now')).toBeInTheDocument(); expect(screen.getByText('Revenue').closest('[data-slot="card"]')).toHaveClass('w-full'); }); it('renders dashboard chart series from config', () => { render( ); expect(screen.getByTestId('series-revenue')).toBeInTheDocument(); expect(screen.getByTestId('series-expenses')).toBeInTheDocument(); expect(screen.getByTestId('series-revenue')).toHaveAttribute('data-bar-size', '22'); }); it('renders empty, loading, and connection error states', () => { render( <> ); expect(screen.getByText('No data available')).toBeInTheDocument(); expect(screen.getByLabelText('Loading chart data')).toBeInTheDocument(); expect(screen.getByText('Connection error')).toBeInTheDocument(); expect(screen.getByText('Unable to connect to the analytics service.')).toBeInTheDocument(); }); it('renders line and horizontal chart series', () => { const config = { revenue: { label: 'Revenue', color: 'var(--chart-1)' }, pipeline: { label: 'Pipeline', color: 'var(--chart-4)' }, }; render( <> ); expect(screen.getAllByTestId('series-revenue')[0]).toBeInTheDocument(); expect(screen.getByTestId('series-pipeline')).toBeInTheDocument(); }); it('renders combo chart and donut chart primitives', () => { const config = { revenue: { label: 'Revenue', color: 'var(--chart-1)' }, conversion: { label: 'Conversion', color: 'var(--chart-2)' }, }; render( <> ); expect(screen.getAllByTestId('series-revenue')[0]).toBeInTheDocument(); expect(screen.getByTestId('series-conversion')).toBeInTheDocument(); }); });