Skip to content

Instantly share code, notes, and snippets.

@olejech
Created March 9, 2022 18:48
Show Gist options
  • Save olejech/80c1455d313fd779f8ba01b02f0759ea to your computer and use it in GitHub Desktop.
Save olejech/80c1455d313fd779f8ba01b02f0759ea to your computer and use it in GitHub Desktop.
Testing Library renderWithRouter helper function Typescript
import { render, RenderResult } from '@testing-library/react';
import { createMemoryHistory, MemoryHistory } from 'history';
import { ReactElement } from 'react';
import { Router } from 'react-router-dom';
type RenderWithRouterProps = {
route?: string;
history?: MemoryHistory;
};
export const renderWithRouter = (
component: ReactElement,
{ route = '/', history = createMemoryHistory({ initialEntries: [route] }) }: RenderWithRouterProps = {}
): RenderResult & { history: MemoryHistory } => {
const Wrapper: React.FC = ({ children }) => <Router history={history}>{children}</Router>;
const renderResult = render(component, { wrapper: Wrapper });
return { ...renderResult, history };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment