TestWrapper.tsx 855 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * 测试包装组件
  3. * 提供测试环境所需的Provider包装
  4. */
  5. import React from 'react';
  6. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  7. import { BrowserRouter } from 'react-router';
  8. interface TestWrapperProps {
  9. children: React.ReactNode;
  10. queryClient?: QueryClient;
  11. withRouter?: boolean;
  12. }
  13. const TestWrapper: React.FC<TestWrapperProps> = ({
  14. children,
  15. queryClient,
  16. withRouter = true,
  17. }) => {
  18. const testQueryClient = queryClient || new QueryClient({
  19. defaultOptions: {
  20. queries: {
  21. retry: false,
  22. gcTime: 0,
  23. },
  24. },
  25. });
  26. const content = (
  27. <QueryClientProvider client={testQueryClient}>
  28. {children}
  29. </QueryClientProvider>
  30. );
  31. if (withRouter) {
  32. return <BrowserRouter>{content}</BrowserRouter>;
  33. }
  34. return content;
  35. };
  36. export default TestWrapper;