refactor: 重构 Dashboard 页面和 ToolCard 组件,全面迁移至 shadcn 样式并优化布局

This commit is contained in:
雨霖铃
2026-05-22 21:56:09 +08:00
parent 917fca5947
commit ae0b923e11
3 changed files with 106 additions and 77 deletions
+19 -10
View File
@@ -17,7 +17,7 @@ describe('ToolCard 组件', () => {
description="这是一个测试工具"
colorKey="primary"
icon={Clock}
onClick={() => {}}
onNavigate={() => {}}
/>,
);
@@ -26,14 +26,14 @@ describe('ToolCard 组件', () => {
});
it('无描述时仅渲染标题', () => {
render(<ToolCard title="仅标题" colorKey="primary" icon={Clock} onClick={() => {}} />);
render(<ToolCard title="仅标题" colorKey="primary" icon={Clock} onNavigate={() => {}} />);
expect(screen.getByText('仅标题')).toBeInTheDocument();
});
it('应渲染图标', () => {
const { container } = render(
<ToolCard title="带图标" colorKey="primary" icon={Clock} onClick={() => {}} />,
<ToolCard title="带图标" colorKey="primary" icon={Clock} onNavigate={() => {}} />,
);
const svgElement = container.querySelector('svg');
@@ -46,7 +46,7 @@ describe('ToolCard 组件', () => {
title="带快照"
colorKey="primary"
icon={Clock}
onClick={() => {}}
onNavigate={() => {}}
snapshot={<div data-testid="snapshot"></div>}
/>,
);
@@ -56,25 +56,32 @@ describe('ToolCard 组件', () => {
it('未提供快照时不渲染快照区域', () => {
const { container } = render(
<ToolCard title="无快照" colorKey="primary" icon={Clock} onClick={() => {}} />,
<ToolCard
title="无快照"
colorKey="primary"
icon={Clock}
onClick={() => {}}
onNavigate={function (): void {
throw new Error('Function not implemented.');
}}
/>,
);
expect(container.querySelector('[data-testid="snapshot"]')).not.toBeInTheDocument();
});
it('应使用 CardActionArea 渲染,支持键盘聚焦', () => {
render(<ToolCard title="可聚焦" colorKey="primary" icon={Clock} onClick={() => {}} />);
render(<ToolCard title="可聚焦" colorKey="primary" icon={Clock} onNavigate={() => {}} />);
const button = screen.getByRole('button', { name: /可聚焦/ });
expect(button).toBeInTheDocument();
expect(button).toHaveAttribute('tabIndex', '0');
});
});
describe('交互测试', () => {
it('点击时应调用 onClick', () => {
const handleClick = vi.fn();
render(<ToolCard title="可点击" colorKey="primary" icon={Clock} onClick={handleClick} />);
render(<ToolCard title="可点击" colorKey="primary" icon={Clock} onNavigate={handleClick} />);
const button = screen.getByRole('button', { name: /可点击/ });
fireEvent.click(button);
@@ -84,7 +91,9 @@ describe('ToolCard 组件', () => {
it('按 Enter 键时应调用 onClick', async () => {
const handleClick = vi.fn();
render(<ToolCard title="键盘可触发" colorKey="primary" icon={Clock} onClick={handleClick} />);
render(
<ToolCard title="键盘可触发" colorKey="primary" icon={Clock} onNavigate={handleClick} />,
);
const button = screen.getByRole('button', { name: /键盘可触发/ });
await act(async () => {
@@ -99,7 +108,7 @@ describe('ToolCard 组件', () => {
describe('样式测试', () => {
it('应应用自定义颜色代码', () => {
const { container } = render(
<ToolCard title="自定义颜色" colorKey="warning" icon={Clock} onClick={() => {}} />,
<ToolCard title="自定义颜色" colorKey="warning" icon={Clock} onNavigate={() => {}} />,
);
const svgElement = container.querySelector('svg');