import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import ToolCard from '../ToolCard';
import AccessTimeIcon from '@mui/icons-material/AccessTime';
describe('ToolCard 组件', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('渲染测试', () => {
it('应渲染标题和描述', () => {
render(
}
onClick={() => {}}
/>,
);
expect(screen.getByText('测试工具')).toBeInTheDocument();
expect(screen.getByText('这是一个测试工具')).toBeInTheDocument();
});
it('无描述时仅渲染标题', () => {
render(
}
onClick={() => {}}
/>,
);
expect(screen.getByText('仅标题')).toBeInTheDocument();
});
it('应渲染图标', () => {
render(
}
onClick={() => {}}
/>,
);
expect(screen.getByTestId('test-icon')).toBeInTheDocument();
});
it('提供快照内容时应渲染快照', () => {
render(
}
onClick={() => {}}
snapshot={
快照内容
}
/>,
);
expect(screen.getByTestId('snapshot')).toBeInTheDocument();
});
it('未提供快照时不渲染快照区域', () => {
const { container } = render(
}
onClick={() => {}}
/>,
);
expect(container.querySelector('[data-testid="snapshot"]')).not.toBeInTheDocument();
});
});
describe('AI 徽章测试', () => {
it('hasAI 为 true 时应渲染 AI 徽章', () => {
render(
}
onClick={() => {}}
/>,
);
const autoAwesomeIcon = screen.getByTestId('AutoAwesomeIcon');
expect(autoAwesomeIcon).toBeInTheDocument();
});
it('hasAI 为 false 时不应渲染 AI 徽章', () => {
render(
}
onClick={() => {}}
/>,
);
const autoAwesomeIcon = screen.queryByTestId('AutoAwesomeIcon');
expect(autoAwesomeIcon).not.toBeInTheDocument();
});
});
describe('交互测试', () => {
it('点击时应调用 onClick', () => {
const handleClick = vi.fn();
render(
}
onClick={handleClick}
/>,
);
const card = screen.getByText('可点击').closest('.MuiBox-root');
if (card) {
fireEvent.click(card);
}
expect(handleClick).toHaveBeenCalledTimes(1);
});
});
describe('样式测试', () => {
it('应应用自定义颜色代码', () => {
const customColor = '#ff5722';
const { container } = render(
}
onClick={() => {}}
/>,
);
const iconContainer = container.querySelector('.MuiBox-root > div');
expect(iconContainer).toBeInTheDocument();
});
});
});