import { render, screen } from '@testing-library/react'; import React, { useEffect } from 'react'; import { ChartLegendWrapper, useChartId, useChartLegend, } from './ChartLegendWrapper'; import { ChartLegend } from './ChartLegend'; import userEvent from '@testing-library/user-event'; describe('ChartLegendWrapper', () => { beforeEach(() => { jest.clearAllMocks(); }); const TestChart = ({ seriesNames }: { seriesNames: string[] }) => { const chartId = useChartId(); const { register } = useChartLegend(); useEffect(() => { register(chartId, seriesNames); }, [chartId, register, seriesNames]); return
Test Chart
; }; const generateColors = (seriesNames: string[]) => { const colors: Record = {}; const colorPalette = ['red', 'blue', 'green', 'yellow', 'purple']; seriesNames.forEach((name, index) => { colors[name] = colorPalette[index % colorPalette.length]; }); return colors; }; describe('Dynamic Color Generation', () => { it('should generate colors dynamically based on registered series', () => { render( , ); expect(screen.getByText('CPU')).toBeInTheDocument(); expect(screen.getByText('Memory')).toBeInTheDocument(); expect(screen.getByLabelText('CPU selected')).toBeInTheDocument(); expect(screen.getByLabelText('Memory selected')).toBeInTheDocument(); }); it('should handle multiple charts with overlapping series', () => { const TestChart1 = () => { const chartId = useChartId(); const { register } = useChartLegend(); useEffect(() => { register(chartId, ['CPU', 'Memory']); }, [chartId, register]); return
Test Chart 1
; }; const TestChart2 = () => { const chartId = useChartId(); const { register } = useChartLegend(); useEffect(() => { register(chartId, ['CPU', 'Disk']); }, [chartId, register]); return
Test Chart 2
; }; render( , ); // Should show unique series from both charts expect(screen.getByText('CPU')).toBeInTheDocument(); expect(screen.getByText('Memory')).toBeInTheDocument(); expect(screen.getByText('Disk')).toBeInTheDocument(); // All should be selected by default expect(screen.getByLabelText('CPU selected')).toBeInTheDocument(); expect(screen.getByLabelText('Memory selected')).toBeInTheDocument(); expect(screen.getByLabelText('Disk selected')).toBeInTheDocument(); }); it('should handle empty series registration', () => { render( , ); // Should not crash and should render empty legend expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('should maintain selection state when new series are added', () => { const { rerender } = render( , ); // Initially only CPU expect(screen.getByText('CPU')).toBeInTheDocument(); expect(screen.getByLabelText('CPU selected')).toBeInTheDocument(); // Select only CPU userEvent.click(screen.getByText('CPU')); expect(screen.getByLabelText('CPU selected')).toBeInTheDocument(); // Add more series rerender( , ); // New series should be added and all should be selected (reset behavior) expect(screen.getByText('CPU')).toBeInTheDocument(); expect(screen.getByText('Memory')).toBeInTheDocument(); expect(screen.getByLabelText('CPU selected')).toBeInTheDocument(); expect(screen.getByLabelText('Memory selected')).toBeInTheDocument(); }); it('should work with different chart configurations', () => { render( , ); expect(screen.getByText('Series1')).toBeInTheDocument(); expect(screen.getByText('Series2')).toBeInTheDocument(); expect(screen.getByText('Series3')).toBeInTheDocument(); }); }); describe('Static Color Set', () => { const staticColorSet = { CPU: 'red', Memory: 'blue', Disk: 'green', }; it('should work with static color sets', () => { render( , ); expect(screen.getByText('CPU')).toBeInTheDocument(); expect(screen.getByText('Memory')).toBeInTheDocument(); expect(screen.getByText('Disk')).toBeInTheDocument(); }); it('should ignore registration when using static color sets', () => { render( , ); // Should only show static color set items, not registered series expect(screen.getByText('CPU')).toBeInTheDocument(); expect(screen.getByText('Memory')).toBeInTheDocument(); expect(screen.getByText('Disk')).toBeInTheDocument(); expect(screen.queryByText('DifferentSeries')).not.toBeInTheDocument(); }); }); describe('Error Handling', () => { it('should throw error when useChartLegend is used outside wrapper', () => { const TestComponent = () => { useChartLegend(); return
Test
; }; expect(() => render()).toThrow( 'useChartLegend must be used within a ChartLegendWrapper', ); }); }); });