import React, { PropsWithChildren } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { MemoryRouter } from 'react-router-dom';

export function createTestQueryClient() {
  return new QueryClient({
    defaultOptions: {
      queries: { retry: false },
    },
  });
}

export function renderWithProviders(ui: React.ReactElement, { route = '/', client }: { route?: string; client?: QueryClient } = {}) {
  const queryClient = client ?? createTestQueryClient();

  function Wrapper({ children }: PropsWithChildren<{}>) {
    return (
      <MemoryRouter initialEntries={[route]}>
        <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
      </MemoryRouter>
    );
  }

  // defer actual render to the test to avoid coupling to a specific testing lib here
  return { Wrapper, queryClient };
}
