StatisticsDashboard.test.tsx
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import StatisticsDashboard from '../pages/Statistics/StatisticsDashboard';
// Mock statisticsService to avoid actual API calls
vi.mock('../services/statisticsService', () => ({
statisticsService: {
getSummary: vi.fn().mockResolvedValue({ total_calls: 1234, success_rate: 98.5, avg_duration_ms: 1200, total_tokens: 56789 }),
getTrends: vi.fn().mockResolvedValue([]),
getByModel: vi.fn().mockResolvedValue([]),
getByAccount: vi.fn().mockResolvedValue([]),
},
}));
// Mock recharts to avoid SVG rendering issues in jsdom
vi.mock('recharts', () => ({
ResponsiveContainer: ({ children }: any) => <div data-testid="responsive-container">{children}</div>,
AreaChart: ({ children }: any) => <div data-testid="area-chart">{children}</div>,
Area: () => <div data-testid="area" />,
BarChart: ({ children }: any) => <div data-testid="bar-chart">{children}</div>,
Bar: () => <div data-testid="bar" />,
XAxis: () => <div data-testid="xaxis" />,
YAxis: () => <div data-testid="yaxis" />,
CartesianGrid: () => <div data-testid="grid" />,
Tooltip: () => <div data-testid="tooltip" />,
Legend: () => <div data-testid="legend" />,
LineChart: ({ children }: any) => <div data-testid="line-chart">{children}</div>,
Line: () => <div data-testid="line" />,
}));
describe('StatisticsDashboard', () => {
it('渲染 page-header', () => {
render(<StatisticsDashboard />);
expect(screen.getByText('调用统计')).toBeTruthy();
expect(screen.getByText('查看模型调用的统计数据、趋势和分布情况')).toBeTruthy();
});
it('渲染时间范围选择器', () => {
render(<StatisticsDashboard />);
expect(screen.getByText('7天')).toBeTruthy();
expect(screen.getByText('30天')).toBeTruthy();
expect(screen.getByText('90天')).toBeTruthy();
expect(screen.getByText('自定义')).toBeTruthy();
});
it('渲染概览统计卡片', () => {
render(<StatisticsDashboard />);
expect(screen.getByText('总调用次数')).toBeTruthy();
expect(screen.getByText('成功率')).toBeTruthy();
expect(screen.getByText('平均耗时')).toBeTruthy();
expect(screen.getByText('Token 消耗')).toBeTruthy();
});
it('渲染图表区域标题', () => {
render(<StatisticsDashboard />);
expect(screen.getByText('调用趋势')).toBeTruthy();
expect(screen.getByText(/模型调用分布/)).toBeTruthy();
expect(screen.getByText(/账号调用分布/)).toBeTruthy();
});
it('渲染粒度切换按钮', () => {
render(<StatisticsDashboard />);
expect(screen.getByText('按小时')).toBeTruthy();
expect(screen.getByText('按天')).toBeTruthy();
});
});