import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { createMemoryHistory, createRootRoute, createRouter, RouterContextProvider, type AnyRoute, type AnyRouter, } from '@tanstack/react-router' import { render, renderHook, type RenderOptions } from '@testing-library/react' import { AuthContext, AuthContext as AuthContextType, FrameworkProvider } from 'common' import { useRouter as useMockRouter } from 'next-router-mock' import { NuqsTestingAdapter } from 'nuqs/adapters/testing' import { useMemo } from 'react' import { TooltipProvider } from 'ui' import { CommandProvider } from 'ui-patterns/CommandMenu' // End of third-party imports import { ProfileContext, type ProfileContextType } from '@/lib/profile' import { parseSearch, stringifySearch } from '@/lib/router-search-params' type AdapterProps = Partial[0]> export const createTestRouter = ( initialUrl = '/', routeTree: AnyRoute = createRootRoute() ): AnyRouter => createRouter({ routeTree, history: createMemoryHistory({ initialEntries: [initialUrl] }), parseSearch, stringifySearch, }) const TestRouterProvider = ({ children, router, initialUrl = '/', }: { children: React.ReactNode router?: AnyRouter initialUrl?: string }) => { const activeRouter = useMemo(() => router ?? createTestRouter(initialUrl), [router, initialUrl]) return {children} } const TestFrameworkProvider = ({ children }: { children: React.ReactNode }) => { const router = useMockRouter() const value = useMemo( () => ({ pathname: router.pathname, asPath: router.asPath, isReady: router.isReady, params: router.query, push: (path: string) => router.push(path), }), [router] ) return {children} } export const CustomWrapper = ({ children, queryClient, nuqs, profileContext, authContext, router, initialUrl, }: { children: React.ReactNode queryClient?: QueryClient nuqs?: AdapterProps profileContext?: ProfileContextType authContext?: AuthContext router?: AnyRouter initialUrl?: string }) => { const _queryClient = queryClient ?? new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false }, }, }) let content = ( {children} ) if (profileContext) { content = {content} } if (authContext) { content = {content} } return content } type CustomRenderOpts = RenderOptions & { queryClient?: QueryClient nuqs?: AdapterProps profileContext?: ProfileContextType authContext?: AuthContextType router?: AnyRouter initialUrl?: string } export const customRender = (component: React.ReactElement, renderOptions?: CustomRenderOpts) => { const router = renderOptions?.router ?? createTestRouter(renderOptions?.initialUrl) const result = render(component, { wrapper: ({ children }) => CustomWrapper({ queryClient: renderOptions?.queryClient, nuqs: renderOptions?.nuqs, profileContext: renderOptions?.profileContext, authContext: renderOptions?.authContext, router, children, }), ...renderOptions, }) return { ...result, router } } export const customRenderHook = (hook: () => T, renderOptions?: CustomRenderOpts) => { const router = renderOptions?.router ?? createTestRouter(renderOptions?.initialUrl) const result = renderHook(hook, { wrapper: ({ children }) => CustomWrapper({ children, queryClient: renderOptions?.queryClient, nuqs: renderOptions?.nuqs, profileContext: renderOptions?.profileContext, authContext: renderOptions?.authContext, router, }), ...renderOptions, }) return { ...result, router } }