|
1 | 1 | import React from 'react'; |
2 | | -import { render, screen, waitFor } from '@testing-library/react'; |
3 | | -import { vi, expect, describe, it } from 'vitest'; |
4 | | -import '@testing-library/jest-dom/vitest'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import ChartContainer from '../ChartContainer'; |
5 | 5 |
|
6 | | -// Mock react-chartjs-2 Line component |
7 | | -vi.mock('react-chartjs-2', () => ({ |
8 | | - Line: () => <div data-testid="chart" /> |
9 | | -})); |
10 | | - |
11 | | -// Mock chart.js to avoid heavy setup |
| 6 | +// Mock chart.js and react-chartjs-2 to avoid canvas requirements |
12 | 7 | vi.mock('chart.js', () => { |
13 | | - const Chart = { register: vi.fn(), defaults: { plugins: { legend: { labels: { generateLabels: () => [] } } } } }; |
| 8 | + const Chart = { |
| 9 | + register: vi.fn(), |
| 10 | + defaults: { plugins: { legend: { labels: { generateLabels: vi.fn(() => []) } } } } |
| 11 | + }; |
14 | 12 | return { |
15 | | - ChartJS: Chart, |
16 | 13 | Chart, |
| 14 | + ChartJS: Chart, |
17 | 15 | CategoryScale: {}, |
18 | 16 | LinearScale: {}, |
19 | 17 | PointElement: {}, |
20 | 18 | LineElement: {}, |
21 | 19 | Title: {}, |
22 | 20 | Tooltip: {}, |
23 | | - Legend: {} |
| 21 | + Legend: {}, |
24 | 22 | }; |
25 | 23 | }); |
26 | 24 |
|
| 25 | +vi.mock('react-chartjs-2', async () => { |
| 26 | + const React = await import('react'); |
| 27 | + const charts = []; |
| 28 | + const lineProps = []; |
| 29 | + return { |
| 30 | + Line: React.forwardRef((props, ref) => { |
| 31 | + lineProps.push(props); |
| 32 | + const chart = { |
| 33 | + data: props.data, |
| 34 | + setActiveElements: vi.fn(), |
| 35 | + tooltip: { setActiveElements: vi.fn() }, |
| 36 | + update: vi.fn(), |
| 37 | + }; |
| 38 | + charts.push(chart); |
| 39 | + if (typeof ref === 'function') ref(chart); |
| 40 | + return <div data-testid="line-chart" />; |
| 41 | + }), |
| 42 | + __charts: charts, |
| 43 | + __lineProps: lineProps, |
| 44 | + }; |
| 45 | +}); |
| 46 | +import { __charts, __lineProps } from 'react-chartjs-2'; |
| 47 | + |
27 | 48 | vi.mock('chartjs-plugin-zoom', () => ({ default: {} })); |
28 | 49 |
|
29 | | -import ChartContainer from '../ChartContainer.jsx'; |
| 50 | +describe('ChartContainer', () => { |
| 51 | + it('prompts to upload files when none provided', () => { |
| 52 | + const onXRangeChange = vi.fn(); |
| 53 | + const onMaxStepChange = vi.fn(); |
| 54 | + render( |
| 55 | + <ChartContainer |
| 56 | + files={[]} |
| 57 | + metrics={[{ name: 'loss', keyword: 'loss', mode: 'keyword' }]} |
| 58 | + compareMode="normal" |
| 59 | + onXRangeChange={onXRangeChange} |
| 60 | + onMaxStepChange={onMaxStepChange} |
| 61 | + /> |
| 62 | + ); |
| 63 | + screen.getByText('📁 请上传日志文件开始分析'); |
| 64 | + expect(onMaxStepChange).toHaveBeenCalledWith(0); |
| 65 | + }); |
30 | 66 |
|
31 | | -const sampleFile = { |
32 | | - name: 'test.log', |
33 | | - id: '1', |
34 | | - content: 'loss: 1\nloss: 2', |
35 | | -}; |
| 67 | + it('prompts to select metrics when none provided', () => { |
| 68 | + const onXRangeChange = vi.fn(); |
| 69 | + const onMaxStepChange = vi.fn(); |
| 70 | + const files = [{ name: 'a.log', enabled: true, content: 'loss: 1' }]; |
| 71 | + render( |
| 72 | + <ChartContainer |
| 73 | + files={files} |
| 74 | + metrics={[]} |
| 75 | + compareMode="normal" |
| 76 | + onXRangeChange={onXRangeChange} |
| 77 | + onMaxStepChange={onMaxStepChange} |
| 78 | + /> |
| 79 | + ); |
| 80 | + screen.getByText('🎯 请选择要显示的图表'); |
| 81 | + }); |
36 | 82 |
|
37 | | -const metric = { name: 'loss', mode: 'keyword', keyword: 'loss:' }; |
| 83 | + it('renders charts and statistics', async () => { |
| 84 | + const onXRangeChange = vi.fn(); |
| 85 | + const onMaxStepChange = vi.fn(); |
| 86 | + const files = [ |
| 87 | + { name: 'a.log', enabled: true, content: 'loss: 1\nloss: 2' }, |
| 88 | + { name: 'b.log', enabled: true, content: 'loss: 1.5\nloss: 2.5' }, |
| 89 | + ]; |
| 90 | + render( |
| 91 | + <ChartContainer |
| 92 | + files={files} |
| 93 | + metrics={[{ name: 'loss', keyword: 'loss', mode: 'keyword' }]} |
| 94 | + compareMode="relative" |
| 95 | + onXRangeChange={onXRangeChange} |
| 96 | + onMaxStepChange={onMaxStepChange} |
| 97 | + /> |
| 98 | + ); |
38 | 99 |
|
39 | | -function renderComponent(props = {}) { |
40 | | - const onXRangeChange = vi.fn(); |
41 | | - const onMaxStepChange = vi.fn(); |
42 | | - const result = render( |
43 | | - <ChartContainer |
44 | | - files={[]} |
45 | | - metrics={[]} |
46 | | - compareMode="normal" |
47 | | - onXRangeChange={onXRangeChange} |
48 | | - onMaxStepChange={onMaxStepChange} |
49 | | - {...props} |
50 | | - /> |
51 | | - ); |
52 | | - return { ...result, onXRangeChange, onMaxStepChange }; |
53 | | -} |
| 100 | + screen.getByText('📊 loss'); |
| 101 | + screen.getByText(/差值统计/); |
| 102 | + expect(onMaxStepChange).toHaveBeenCalledWith(1); |
54 | 103 |
|
55 | | -describe('ChartContainer', () => { |
56 | | - it('shows empty message when no files', () => { |
57 | | - renderComponent(); |
58 | | - expect(screen.getByText('📊 暂无数据')).toBeInTheDocument(); |
| 104 | + // simulate hover to trigger sync |
| 105 | + const hover = __lineProps[0].options.onHover; |
| 106 | + hover({}, [{ index: 0 }]); |
| 107 | + expect(__charts[1].setActiveElements).toHaveBeenCalled(); |
59 | 108 | }); |
60 | 109 |
|
61 | | - it('shows metric selection message when no metrics', () => { |
62 | | - renderComponent({ files: [sampleFile] }); |
63 | | - expect(screen.getByText('🎯 请选择要显示的图表')).toBeInTheDocument(); |
64 | | - }); |
| 110 | + it('parses metrics, applies range and triggers callbacks', () => { |
| 111 | + const onXRangeChange = vi.fn(); |
| 112 | + const onMaxStepChange = vi.fn(); |
| 113 | + const files = [ |
| 114 | + { |
| 115 | + name: 'a.log', |
| 116 | + enabled: true, |
| 117 | + content: 'loss: 1\nloss: 2\nloss: 3\nacc: 4\nacc: 5', |
| 118 | + config: { dataRange: { start: 1, end: 3 } } |
| 119 | + }, |
| 120 | + { |
| 121 | + name: 'b.log', |
| 122 | + enabled: true, |
| 123 | + content: 'loss: 2\nloss: 4\nacc: 6\nacc: 8', |
| 124 | + config: { dataRange: { start: 1, end: 3 } } |
| 125 | + } |
| 126 | + ]; |
| 127 | + const metrics = [ |
| 128 | + { keyword: 'loss', mode: 'keyword' }, |
| 129 | + { regex: 'acc:(\\d+)', mode: 'regex' }, |
| 130 | + {} |
| 131 | + ]; |
| 132 | + |
| 133 | + render( |
| 134 | + <ChartContainer |
| 135 | + files={files} |
| 136 | + metrics={metrics} |
| 137 | + compareMode="relative" |
| 138 | + onXRangeChange={onXRangeChange} |
| 139 | + onMaxStepChange={onMaxStepChange} |
| 140 | + /> |
| 141 | + ); |
| 142 | + |
| 143 | + // metric titles |
| 144 | + expect(screen.getAllByText(/loss/)[0]).toBeTruthy(); |
| 145 | + screen.getByText(/metric2/); |
| 146 | + screen.getByText(/metric3/); |
| 147 | + |
| 148 | + // data range applied (start 1 end 3 => 2 points for loss) |
| 149 | + const currentProps = __lineProps.slice(-5); |
| 150 | + expect(currentProps[0].data.datasets[0].data).toHaveLength(2); |
| 151 | + |
| 152 | + // trigger container mouse leave |
| 153 | + const container = screen.getAllByTestId('line-chart')[0].parentElement; |
| 154 | + fireEvent.mouseLeave(container); |
| 155 | + |
| 156 | + // invoke legend and tooltip callbacks |
| 157 | + const opts = currentProps[0].options; |
| 158 | + opts.plugins.legend.labels.generateLabels({ data: { datasets: [{}, { borderDash: [5,5] }] } }); |
| 159 | + const tt = opts.plugins.tooltip.callbacks; |
| 160 | + tt.title([{ parsed: { x: 1 } }]); |
| 161 | + tt.label({ parsed: { y: 1.2345 } }); |
| 162 | + tt.labelColor({ dataset: { borderColor: '#fff' } }); |
65 | 163 |
|
66 | | - it('renders charts and triggers callbacks', async () => { |
67 | | - const { onXRangeChange, onMaxStepChange } = renderComponent({ files: [sampleFile], metrics: [metric] }); |
68 | | - expect(await screen.findByText('📊 loss')).toBeInTheDocument(); |
69 | | - await waitFor(() => { |
70 | | - expect(onMaxStepChange).toHaveBeenCalledWith(1); |
71 | | - expect(onXRangeChange).toHaveBeenCalled(); |
72 | | - }); |
73 | | - const cb = onXRangeChange.mock.calls[0][0]; |
74 | | - expect(cb({})).toEqual({ min: 0, max: 1 }); |
| 164 | + // invoke zoom callbacks |
| 165 | + opts.plugins.zoom.pan.onPanComplete({ chart: { scales: { x: { min: 0, max: 10 } } } }); |
| 166 | + opts.plugins.zoom.zoom.onZoomComplete({ chart: { scales: { x: { min: 2, max: 4 } } } }); |
75 | 167 | }); |
76 | 168 | }); |
0 commit comments