Files
novalon-website/src/app/admin/page.test.tsx
T
张翔 b207bfa7af feat: 增加测试覆盖率并优化代码质量
test: 添加单元测试和端到端测试
refactor: 重构登录页面和上传模块
ci: 更新测试覆盖率阈值至42%
build: 添加测试相关依赖
docs: 更新测试文档
style: 修复代码格式问题
2026-03-11 11:14:37 +08:00

109 lines
3.2 KiB
TypeScript

import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import AdminDashboard from './page';
jest.mock('@/lib/auth', () => ({
auth: jest.fn().mockResolvedValue({
user: { name: '测试用户' },
}),
}));
jest.mock('@/db', () => ({
db: {
select: jest.fn().mockReturnValue({
from: jest.fn().mockReturnValue({
where: jest.fn().mockReturnValue({
orderBy: jest.fn().mockReturnValue({
limit: jest.fn().mockResolvedValue([]),
}),
}),
orderBy: jest.fn().mockReturnValue({
limit: jest.fn().mockResolvedValue([]),
}),
}),
}),
},
}));
jest.mock('next/link', () => {
return ({ children, href }: { children: React.ReactNode; href: string }) => {
return <a href={href}>{children}</a>;
};
});
describe('AdminDashboard', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('Rendering', () => {
it('should render dashboard', async () => {
const dashboard = await AdminDashboard();
render(dashboard);
const heading = screen.getByRole('heading', { level: 1 });
expect(heading).toBeInTheDocument();
expect(heading).toHaveTextContent('仪表盘');
});
it('should render welcome message', async () => {
const dashboard = await AdminDashboard();
render(dashboard);
const welcome = screen.getByText(/欢迎回来/i);
expect(welcome).toBeInTheDocument();
});
it('should render stat cards', async () => {
const dashboard = await AdminDashboard();
render(dashboard);
const totalContent = screen.getByText('总内容数');
const published = screen.getByText('已发布');
const draft = screen.getByText('草稿');
const users = screen.getByText('用户数');
expect(totalContent).toBeInTheDocument();
expect(published).toBeInTheDocument();
expect(draft).toBeInTheDocument();
expect(users).toBeInTheDocument();
});
it('should render recent content section', async () => {
const dashboard = await AdminDashboard();
render(dashboard);
const recentContent = screen.getByText('最近内容');
expect(recentContent).toBeInTheDocument();
});
it('should render quick actions section', async () => {
const dashboard = await AdminDashboard();
render(dashboard);
const quickActions = screen.getByText('快捷操作');
expect(quickActions).toBeInTheDocument();
});
});
describe('Navigation', () => {
it('should have content management link', async () => {
const dashboard = await AdminDashboard();
render(dashboard);
const contentLink = screen.getByRole('link', { name: /总内容数/i });
expect(contentLink).toBeInTheDocument();
expect(contentLink).toHaveAttribute('href', '/admin/content');
});
it('should have users link', async () => {
const dashboard = await AdminDashboard();
render(dashboard);
const usersLink = screen.getByRole('link', { name: /用户数/i });
expect(usersLink).toBeInTheDocument();
expect(usersLink).toHaveAttribute('href', '/admin/users');
});
});
});