Created
March 9, 2022 18:48
-
-
Save olejech/80c1455d313fd779f8ba01b02f0759ea to your computer and use it in GitHub Desktop.
Testing Library renderWithRouter helper function Typescript
This file contains 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
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