Created
February 11, 2026 19:06
-
-
Save claudiainbytes/69182d16fff1dea8e4c1686b94ae234c to your computer and use it in GitHub Desktop.
Testing Custom Render con Múltiples Providers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // test-utils/custom-render.tsx | |
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | |
| import { AuthProvider } from '@/contexts/AuthContext' | |
| import { ThemeProvider } from '@/contexts/ThemeContext' | |
| interface RenderOptions { | |
| initialUser?: User | |
| theme?: 'light' | 'dark' | |
| queryClient?: QueryClient | |
| } | |
| export const renderWithProviders = ( | |
| ui: React.ReactElement, | |
| { | |
| initialUser = null, | |
| theme = 'light', | |
| queryClient = createTestQueryClient(), | |
| ...renderOptions | |
| }: RenderOptions = {} | |
| ) => { | |
| const Wrapper = ({ children }) => ( | |
| <QueryClientProvider client={queryClient}> | |
| <AuthProvider initialUser={initialUser}> | |
| <ThemeProvider initialTheme={theme}> | |
| {children} | |
| </ThemeProvider> | |
| </AuthProvider> | |
| </QueryClientProvider> | |
| ) | |
| return render(ui, { wrapper: Wrapper, ...renderOptions }) | |
| } | |
| // Uso | |
| it('muestra contenido para usuario autenticado', () => { | |
| renderWithProviders(<Dashboard />, { | |
| initialUser: { id: 1, name: 'John' }, | |
| theme: 'dark' | |
| }) | |
| expect(screen.getByText(/welcome, john/i)).toBeInTheDocument() | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment