custom-render.tsx81 lines · main
| 1 | import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 2 | import { render, renderHook, type RenderOptions } from '@testing-library/react' |
| 3 | import { NuqsTestingAdapter } from 'nuqs/adapters/testing' |
| 4 | import { TooltipProvider } from 'ui' |
| 5 | import { CommandProvider } from 'ui-patterns/CommandMenu' |
| 6 | |
| 7 | // End of third-party imports |
| 8 | |
| 9 | import { ProfileContext, type ProfileContextType } from '@/lib/profile' |
| 10 | |
| 11 | type AdapterProps = Partial<Parameters<typeof NuqsTestingAdapter>[0]> |
| 12 | |
| 13 | const CustomWrapper = ({ |
| 14 | children, |
| 15 | queryClient, |
| 16 | nuqs, |
| 17 | profileContext, |
| 18 | }: { |
| 19 | children: React.ReactNode |
| 20 | queryClient?: QueryClient |
| 21 | nuqs?: AdapterProps |
| 22 | profileContext?: ProfileContextType |
| 23 | }) => { |
| 24 | const _queryClient = |
| 25 | queryClient ?? |
| 26 | new QueryClient({ |
| 27 | defaultOptions: { |
| 28 | queries: { |
| 29 | retry: false, |
| 30 | }, |
| 31 | }, |
| 32 | }) |
| 33 | |
| 34 | const content = ( |
| 35 | <QueryClientProvider client={_queryClient}> |
| 36 | <NuqsTestingAdapter {...nuqs}> |
| 37 | <TooltipProvider> |
| 38 | <CommandProvider openKey="">{children}</CommandProvider> |
| 39 | </TooltipProvider> |
| 40 | </NuqsTestingAdapter> |
| 41 | </QueryClientProvider> |
| 42 | ) |
| 43 | |
| 44 | return profileContext ? ( |
| 45 | <ProfileContext.Provider value={profileContext}>{content}</ProfileContext.Provider> |
| 46 | ) : ( |
| 47 | content |
| 48 | ) |
| 49 | } |
| 50 | |
| 51 | type CustomRenderOpts = RenderOptions & { |
| 52 | queryClient?: QueryClient |
| 53 | nuqs?: AdapterProps |
| 54 | profileContext?: ProfileContextType |
| 55 | } |
| 56 | |
| 57 | export const customRender = (component: React.ReactElement, renderOptions?: CustomRenderOpts) => { |
| 58 | return render(component, { |
| 59 | wrapper: ({ children }) => |
| 60 | CustomWrapper({ |
| 61 | queryClient: renderOptions?.queryClient, |
| 62 | nuqs: renderOptions?.nuqs, |
| 63 | profileContext: renderOptions?.profileContext, |
| 64 | children, |
| 65 | }), |
| 66 | ...renderOptions, |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | export const customRenderHook = (hook: () => any, renderOptions?: CustomRenderOpts) => { |
| 71 | return renderHook(hook, { |
| 72 | wrapper: ({ children }) => |
| 73 | CustomWrapper({ |
| 74 | children, |
| 75 | queryClient: renderOptions?.queryClient, |
| 76 | nuqs: renderOptions?.nuqs, |
| 77 | profileContext: renderOptions?.profileContext, |
| 78 | }), |
| 79 | ...renderOptions, |
| 80 | }) |
| 81 | } |