feat: 增加测试覆盖率并优化代码质量
test: 添加单元测试和端到端测试 refactor: 重构登录页面和上传模块 ci: 更新测试覆盖率阈值至42% build: 添加测试相关依赖 docs: 更新测试文档 style: 修复代码格式问题
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import { describe, it, expect, jest, beforeAll } from '@jest/globals';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import '@testing-library/jest-dom';
|
||||
|
||||
jest.mock('framer-motion', () => ({
|
||||
motion: {
|
||||
div: ({ children, className, ...props }: any) => (
|
||||
<div className={className} {...props}>
|
||||
{children}
|
||||
</div>
|
||||
),
|
||||
section: ({ children, className, ...props }: any) => (
|
||||
<section className={className} {...props}>
|
||||
{children}
|
||||
</section>
|
||||
),
|
||||
},
|
||||
AnimatePresence: ({ children }: any) => <>{children}</>,
|
||||
useInView: () => [null, true],
|
||||
}));
|
||||
|
||||
jest.mock('next/link', () => {
|
||||
return ({ children, href, ...props }: any) => (
|
||||
<a href={href} {...props}>
|
||||
{children}
|
||||
</a>
|
||||
);
|
||||
});
|
||||
|
||||
jest.mock('lucide-react', () => ({
|
||||
ArrowRight: () => <span data-testid="arrow-right-icon" />,
|
||||
ArrowLeft: () => <span data-testid="arrow-left-icon" />,
|
||||
Code: () => <span data-testid="code-icon" />,
|
||||
Cloud: () => <span data-testid="cloud-icon" />,
|
||||
BarChart3: () => <span data-testid="bar-chart-icon" />,
|
||||
Shield: () => <span data-testid="shield-icon" />,
|
||||
}));
|
||||
|
||||
jest.mock('@/components/ui/button', () => ({
|
||||
Button: ({ children, className, ...props }: any) => (
|
||||
<button className={className} {...props}>
|
||||
{children}
|
||||
</button>
|
||||
),
|
||||
}));
|
||||
|
||||
jest.mock('@/components/ui/badge', () => ({
|
||||
Badge: ({ children, className, ...props }: any) => (
|
||||
<span className={className} {...props}>
|
||||
{children}
|
||||
</span>
|
||||
),
|
||||
}));
|
||||
|
||||
jest.mock('@/components/ui/loading-skeleton', () => ({
|
||||
ServiceCardSkeleton: () => <div data-testid="service-card-skeleton">Loading...</div>,
|
||||
}));
|
||||
|
||||
jest.mock('@/components/ui/page-header', () => ({
|
||||
PageHeader: ({ title, description }: any) => (
|
||||
<header>
|
||||
<h1>{title}</h1>
|
||||
<p>{description}</p>
|
||||
</header>
|
||||
),
|
||||
}));
|
||||
|
||||
jest.mock('@/lib/constants', () => ({
|
||||
SERVICES: [
|
||||
{
|
||||
id: 'software-dev',
|
||||
title: '软件开发',
|
||||
icon: 'Code',
|
||||
description: '定制化软件开发服务',
|
||||
features: ['需求分析', '架构设计', '开发测试', '运维支持'],
|
||||
},
|
||||
{
|
||||
id: 'cloud-service',
|
||||
title: '云服务',
|
||||
icon: 'Cloud',
|
||||
description: '企业云服务解决方案',
|
||||
features: ['云迁移', '云原生', '云安全', '云运维'],
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
import ServicesPage from './page';
|
||||
|
||||
describe('ServicesPage', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('Rendering', () => {
|
||||
it('should render services page', () => {
|
||||
const { container } = render(<ServicesPage />);
|
||||
const pageContainer = container.querySelector('.min-h-screen');
|
||||
expect(pageContainer).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render page header', () => {
|
||||
render(<ServicesPage />);
|
||||
const title = screen.getByText(/核心业务/i);
|
||||
expect(title).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render back to home link', () => {
|
||||
render(<ServicesPage />);
|
||||
const backLink = screen.getByText(/返回首页/i);
|
||||
expect(backLink).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render loading skeletons initially', () => {
|
||||
render(<ServicesPage />);
|
||||
const skeletons = screen.getAllByTestId('service-card-skeleton');
|
||||
expect(skeletons.length).toBe(4);
|
||||
});
|
||||
|
||||
it('should render CTA section', () => {
|
||||
render(<ServicesPage />);
|
||||
const cta = screen.getByText(/准备开始您的数字化转型之旅/i);
|
||||
expect(cta).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Navigation', () => {
|
||||
it('should have contact link', () => {
|
||||
render(<ServicesPage />);
|
||||
const contactLink = screen.getByRole('link', { name: /立即咨询/i });
|
||||
expect(contactLink).toHaveAttribute('href', '/contact');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Accessibility', () => {
|
||||
it('should have proper heading hierarchy', () => {
|
||||
render(<ServicesPage />);
|
||||
const h1 = screen.getByRole('heading', { level: 1 });
|
||||
expect(h1).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user