diff --git a/src/design-system/composites/ThemedChart/ThemedChart.test.tsx b/src/design-system/composites/ThemedChart/ThemedChart.test.tsx new file mode 100644 index 0000000..cda2fad --- /dev/null +++ b/src/design-system/composites/ThemedChart/ThemedChart.test.tsx @@ -0,0 +1,46 @@ +import { describe, it, expect } from 'vitest' +import { render } from '@testing-library/react' +import { ThemedChart } from './ThemedChart' +import { Line } from 'recharts' + +// Recharts uses ResizeObserver internally +class ResizeObserverMock { + observe() {} + unobserve() {} + disconnect() {} +} +globalThis.ResizeObserver = ResizeObserverMock as any + +describe('ThemedChart', () => { + it('renders nothing when data is empty', () => { + const { container } = render( + + + , + ) + expect(container.innerHTML).toBe('') + }) + + it('renders a chart container when data is provided', () => { + const data = [ + { time: '10:00', value: 10 }, + { time: '10:01', value: 20 }, + ] + const { container } = render( + + + , + ) + expect(container.querySelector('.recharts-responsive-container')).toBeTruthy() + }) + + it('applies custom className', () => { + const data = [{ time: '10:00', value: 5 }] + const { container } = render( + + + , + ) + expect(container.querySelector('.my-chart')).toBeTruthy() + }) +}) diff --git a/src/design-system/composites/ThemedChart/ThemedChart.tsx b/src/design-system/composites/ThemedChart/ThemedChart.tsx new file mode 100644 index 0000000..888f880 --- /dev/null +++ b/src/design-system/composites/ThemedChart/ThemedChart.tsx @@ -0,0 +1,66 @@ +import { + ResponsiveContainer, + ComposedChart, + CartesianGrid, + XAxis, + YAxis, + Tooltip, +} from 'recharts' +import { rechartsTheme } from '../../utils/rechartsTheme' +import { ChartTooltip } from './ChartTooltip' + +interface ThemedChartProps { + data: Record[] + height?: number + xDataKey?: string + xType?: 'number' | 'category' + xTickFormatter?: (value: any) => string + yTickFormatter?: (value: any) => string + yLabel?: string + children: React.ReactNode + className?: string +} + +export function ThemedChart({ + data, + height = 200, + xDataKey = 'time', + xType = 'category', + xTickFormatter, + yTickFormatter, + yLabel, + children, + className, +}: ThemedChartProps) { + if (!data.length) { + return null + } + + return ( +
+ + + + + + } cursor={rechartsTheme.tooltip.cursor} /> + {children} + + +
+ ) +}