refactor: 优化组件并添加测试覆盖
- 创建 CLAUDE.md 项目开发指导文档 - 提取公共常量到 components/constants.ts - 修复 TimestampToDatetime 重复插件扩展问题 - 修复 DatetimeToTimestamp 时区解析问题 - 优化 RoutePersistence 移除不必要依赖 - 添加 Vitest 测试框架配置 - 为所有组件编写单元测试 (16个测试用例) - 更新 .gitignore 忽略测试结果目录 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { render } from '@testing-library/react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import RoutePersistence from '../RoutePersistence';
|
||||
import { storageUtil } from '@/utils/chromeStorage';
|
||||
import { vi } from 'vitest';
|
||||
|
||||
vi.mock('@/utils/chromeStorage', () => ({
|
||||
storageUtil: {
|
||||
get: vi.fn(),
|
||||
set: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
const renderComponentOnly = (initialPath = '/') => {
|
||||
return render(
|
||||
<MemoryRouter initialEntries={[initialPath]}>
|
||||
<RoutePersistence />
|
||||
</MemoryRouter>,
|
||||
);
|
||||
};
|
||||
|
||||
describe('RoutePersistence', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('组件应该正常渲染且返回 null', () => {
|
||||
const { container } = renderComponentOnly();
|
||||
expect(container.firstChild).toBeNull();
|
||||
});
|
||||
|
||||
it('当在根路由时应该从存储中恢复路由', async () => {
|
||||
const mockGet = storageUtil.get as vi.Mock;
|
||||
mockGet.mockResolvedValue('/timestamp');
|
||||
|
||||
await renderComponentOnly('/');
|
||||
|
||||
expect(mockGet).toHaveBeenCalledWith('app/lastRoute');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user