utils.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Test utilities and wrapper components.
  3. */
  4. import React from 'react';
  5. import { render } from '@testing-library/react';
  6. import type { RenderOptions } from '@testing-library/react';
  7. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  8. import { BrowserRouter } from 'react-router-dom';
  9. import { ThemeProvider } from '../contexts/ThemeContext';
  10. import { ToastProvider } from '../contexts/ToastContext';
  11. // Create a new QueryClient for each test
  12. function createTestQueryClient() {
  13. return new QueryClient({
  14. defaultOptions: {
  15. queries: {
  16. retry: false,
  17. gcTime: 0,
  18. },
  19. mutations: {
  20. retry: false,
  21. },
  22. },
  23. });
  24. }
  25. interface AllProvidersProps {
  26. children: React.ReactNode;
  27. }
  28. function AllProviders({ children }: AllProvidersProps) {
  29. const queryClient = createTestQueryClient();
  30. return (
  31. <QueryClientProvider client={queryClient}>
  32. <BrowserRouter>
  33. <ThemeProvider>
  34. <ToastProvider>{children}</ToastProvider>
  35. </ThemeProvider>
  36. </BrowserRouter>
  37. </QueryClientProvider>
  38. );
  39. }
  40. /**
  41. * Custom render function that wraps components with all providers.
  42. */
  43. function customRender(
  44. ui: React.ReactElement,
  45. options?: Omit<RenderOptions, 'wrapper'>
  46. ) {
  47. return render(ui, { wrapper: AllProviders, ...options });
  48. }
  49. // Re-export everything from testing-library
  50. export * from '@testing-library/react';
  51. // Override render with our custom render
  52. export { customRender as render };
  53. /**
  54. * Create a test QueryClient with custom configuration.
  55. */
  56. export { createTestQueryClient };
  57. /**
  58. * Helper to wait for async operations.
  59. */
  60. export const waitForAsync = () => new Promise((resolve) => setTimeout(resolve, 0));