| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import { ReactNode } from 'react';
- import { MemoryRouter, Routes, Route, useLocation } from 'react-router-dom';
- import { vi } from 'vitest';
- /**
- * 测试路由器的包装组件
- */
- export function TestRouter({
- children,
- initialPath = '/',
- routes = []
- }: {
- children: ReactNode;
- initialPath?: string;
- routes?: Array<{ path: string; element: ReactNode }>;
- }) {
- return (
- <MemoryRouter initialEntries={[initialPath]}>
- <Routes>
- {routes.map((route, index) => (
- <Route key={index} path={route.path} element={route.element} />
- ))}
- <Route path="*" element={children} />
- </Routes>
- </MemoryRouter>
- );
- }
- /**
- * 获取当前路由位置的Hook
- */
- export function useTestLocation() {
- const location = useLocation();
- return location;
- }
- /**
- * 创建测试导航函数
- */
- export function createTestNavigation() {
- return {
- navigate: vi.fn(),
- goBack: vi.fn(),
- goForward: vi.fn(),
- replace: vi.fn()
- };
- }
|