|
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 } 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 = { register: vi.fn() }; |
14 | 9 | return { |
15 | | - ChartJS: Chart, |
16 | 10 | Chart, |
| 11 | + ChartJS: Chart, |
17 | 12 | CategoryScale: {}, |
18 | 13 | LinearScale: {}, |
19 | 14 | PointElement: {}, |
20 | 15 | LineElement: {}, |
21 | 16 | Title: {}, |
22 | 17 | Tooltip: {}, |
23 | | - Legend: {} |
| 18 | + Legend: {}, |
24 | 19 | }; |
25 | 20 | }); |
26 | 21 |
|
27 | | -vi.mock('chartjs-plugin-zoom', () => ({ default: {} })); |
28 | | - |
29 | | -import ChartContainer from '../ChartContainer.jsx'; |
30 | | - |
31 | | -const sampleFile = { |
32 | | - name: 'test.log', |
33 | | - id: '1', |
34 | | - content: 'loss: 1\nloss: 2', |
35 | | -}; |
36 | | - |
37 | | -const metric = { name: 'loss', mode: 'keyword', keyword: 'loss:' }; |
| 22 | +vi.mock('react-chartjs-2', async () => { |
| 23 | + const React = await import('react'); |
| 24 | + const charts = []; |
| 25 | + const lineProps = []; |
| 26 | + return { |
| 27 | + Line: React.forwardRef((props, ref) => { |
| 28 | + lineProps.push(props); |
| 29 | + const chart = { |
| 30 | + data: props.data, |
| 31 | + setActiveElements: vi.fn(), |
| 32 | + tooltip: { setActiveElements: vi.fn() }, |
| 33 | + update: vi.fn(), |
| 34 | + }; |
| 35 | + charts.push(chart); |
| 36 | + if (typeof ref === 'function') ref(chart); |
| 37 | + return <div data-testid="line-chart" />; |
| 38 | + }), |
| 39 | + __charts: charts, |
| 40 | + __lineProps: lineProps, |
| 41 | + }; |
| 42 | +}); |
| 43 | +import { __charts, __lineProps } from 'react-chartjs-2'; |
38 | 44 |
|
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 | | -} |
| 45 | +vi.mock('chartjs-plugin-zoom', () => ({ default: {} })); |
54 | 46 |
|
55 | 47 | describe('ChartContainer', () => { |
56 | | - it('shows empty message when no files', () => { |
57 | | - renderComponent(); |
58 | | - expect(screen.getByText('📊 暂无数据')).toBeInTheDocument(); |
| 48 | + it('prompts to upload files when none provided', () => { |
| 49 | + const onXRangeChange = vi.fn(); |
| 50 | + const onMaxStepChange = vi.fn(); |
| 51 | + render( |
| 52 | + <ChartContainer |
| 53 | + files={[]} |
| 54 | + metrics={[{ name: 'loss', keyword: 'loss', mode: 'keyword' }]} |
| 55 | + compareMode="normal" |
| 56 | + onXRangeChange={onXRangeChange} |
| 57 | + onMaxStepChange={onMaxStepChange} |
| 58 | + /> |
| 59 | + ); |
| 60 | + screen.getByText('📁 请上传日志文件开始分析'); |
| 61 | + expect(onMaxStepChange).toHaveBeenCalledWith(0); |
59 | 62 | }); |
60 | 63 |
|
61 | | - it('shows metric selection message when no metrics', () => { |
62 | | - renderComponent({ files: [sampleFile] }); |
63 | | - expect(screen.getByText('🎯 请选择要显示的图表')).toBeInTheDocument(); |
| 64 | + it('prompts to select metrics when none provided', () => { |
| 65 | + const onXRangeChange = vi.fn(); |
| 66 | + const onMaxStepChange = vi.fn(); |
| 67 | + const files = [{ name: 'a.log', enabled: true, content: 'loss: 1' }]; |
| 68 | + render( |
| 69 | + <ChartContainer |
| 70 | + files={files} |
| 71 | + metrics={[]} |
| 72 | + compareMode="normal" |
| 73 | + onXRangeChange={onXRangeChange} |
| 74 | + onMaxStepChange={onMaxStepChange} |
| 75 | + /> |
| 76 | + ); |
| 77 | + screen.getByText('🎯 请选择要显示的图表'); |
64 | 78 | }); |
65 | 79 |
|
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 }); |
| 80 | + it('renders charts and statistics', async () => { |
| 81 | + const onXRangeChange = vi.fn(); |
| 82 | + const onMaxStepChange = vi.fn(); |
| 83 | + const files = [ |
| 84 | + { name: 'a.log', enabled: true, content: 'loss: 1\nloss: 2' }, |
| 85 | + { name: 'b.log', enabled: true, content: 'loss: 1.5\nloss: 2.5' }, |
| 86 | + ]; |
| 87 | + render( |
| 88 | + <ChartContainer |
| 89 | + files={files} |
| 90 | + metrics={[{ name: 'loss', keyword: 'loss', mode: 'keyword' }]} |
| 91 | + compareMode="relative" |
| 92 | + onXRangeChange={onXRangeChange} |
| 93 | + onMaxStepChange={onMaxStepChange} |
| 94 | + /> |
| 95 | + ); |
| 96 | + |
| 97 | + screen.getByText('📊 loss'); |
| 98 | + screen.getByText(/差值统计/); |
| 99 | + expect(onMaxStepChange).toHaveBeenCalledWith(1); |
| 100 | + |
| 101 | + // simulate hover to trigger sync |
| 102 | + const hover = __lineProps[0].options.onHover; |
| 103 | + hover({}, [{ index: 0 }]); |
| 104 | + expect(__charts[1].setActiveElements).toHaveBeenCalled(); |
75 | 105 | }); |
76 | 106 | }); |
0 commit comments