57ea4d9858
- **docs**: 完善组件注释、README 目录结构及 AGENTS.md 文档。 - **refactor**: - 提取通用 `PageHeader`、`Button`、`DashboardCard` 及 `ErrorBoundary` 组件。 - 重构消息通信机制,采用 `@webext-core/messaging` 实现类型安全。 - 将全局通知系统重构为 `SnackbarProvider` (后合并至 `GlobalSnackbar`)。 - 迁移样式系统至 MUI 主题,移除冗余 CSS。 - 优化路由配置,支持独立标签页模式及页面懒加载。 - 移除未使用文件、URL 工具及表单映射相关功能。 - **feat**: - 新增配置导出功能(JSON)及状态提示。 - 新增侧边栏状态变化通知机制。 - 新增文本统计及 JWT 解析工具。 - 优化二维码生成与解析逻辑,换用更轻量的 `qrious` 和 `qr-scanner`。 - 增强高亮器功能,支持闪烁效果及 Shadow DOM 穿透。 - **style**: 优化仪表盘响应式网格布局及 UI 细节。 - **fix**: 修复 `useStorageState` 依赖缺失及路由初始化性能问题。 - **test**: 更新单元测试以覆盖新增的工具函数及功能特性。
77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { render, screen, fireEvent } from '@testing-library/react';
|
|
import TopBar from '../TopBar';
|
|
import { RouterProvider } from '@/providers/RouterProvider';
|
|
import type { PageType } from '@/types/storage';
|
|
import React from 'react';
|
|
|
|
const mockRouterValue = {
|
|
currentPage: 'dashboard' as PageType,
|
|
visiblePages: ['dashboard', 'timestamp'] as PageType[],
|
|
pageOrder: ['timestamp'] as PageType[],
|
|
isLoaded: true,
|
|
navigateTo: vi.fn(),
|
|
navigateLocal: vi.fn(),
|
|
syncNavigation: vi.fn(),
|
|
goBack: vi.fn(),
|
|
setVisiblePages: vi.fn(),
|
|
setPageOrder: vi.fn(),
|
|
};
|
|
|
|
vi.mock('@/providers/RouterProvider', () => ({
|
|
useRouter: () => mockRouterValue,
|
|
RouterProvider: ({ children }: { children: React.ReactNode }) => children,
|
|
}));
|
|
|
|
describe('TopBar 组件', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
const renderWithProvider = (ui: React.ReactElement) => {
|
|
return render(<RouterProvider>{ui}</RouterProvider>);
|
|
};
|
|
|
|
describe('渲染测试', () => {
|
|
it('应使用默认标题渲染', () => {
|
|
renderWithProvider(<TopBar onOpenOptions={vi.fn()} />);
|
|
expect(screen.getByText('Testing Tools')).toBeInTheDocument();
|
|
});
|
|
|
|
it('不在 dashboard 时应渲染返回按钮', () => {
|
|
mockRouterValue.currentPage = 'timestamp';
|
|
renderWithProvider(<TopBar onOpenOptions={vi.fn()} />);
|
|
expect(screen.getByTestId('ArrowBackIosNewIcon')).toBeInTheDocument();
|
|
});
|
|
|
|
it('在 dashboard 上不应渲染返回按钮', () => {
|
|
mockRouterValue.currentPage = 'dashboard';
|
|
renderWithProvider(<TopBar onOpenOptions={vi.fn()} />);
|
|
expect(screen.queryByTestId('ArrowBackIosNewIcon')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('应渲染设置按钮', () => {
|
|
renderWithProvider(<TopBar onOpenOptions={vi.fn()} />);
|
|
expect(screen.getByTestId('SettingsIcon')).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe('交互测试', () => {
|
|
it('点击设置按钮时应调用 onOpenOptions', () => {
|
|
const handleOpenOptions = vi.fn();
|
|
renderWithProvider(<TopBar onOpenOptions={handleOpenOptions} />);
|
|
|
|
fireEvent.click(screen.getByTestId('SettingsIcon'));
|
|
expect(handleOpenOptions).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('点击返回按钮时应调用 goBack', () => {
|
|
mockRouterValue.currentPage = 'timestamp';
|
|
renderWithProvider(<TopBar onOpenOptions={vi.fn()} />);
|
|
|
|
fireEvent.click(screen.getByTestId('ArrowBackIosNewIcon'));
|
|
expect(mockRouterValue.goBack).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|