Skip to content

Instantly share code, notes, and snippets.

@stipsan
Last active December 19, 2023 12:43
Show Gist options
  • Save stipsan/35d2baf7509c7863e7f8b2ade515d4cb to your computer and use it in GitHub Desktop.
Save stipsan/35d2baf7509c7863e7f8b2ade515d4cb to your computer and use it in GitHub Desktop.
Testing with Jest: Tip #5
import renderer from 'react-test-renderer'
import Sidebar from '../Sidebar'
jest.mock('react-router-dom', () => ({
Link: 'Link',
Route: ({ children, path }) => children({ match: path === '/somewhere' })
}))
it('should render correctly', () => {
const component = renderer.create(<Sidebar />)
expect(component.toJSON()).toMatchSnapshot()
})
import { Route, Link } from 'react-router-dom'
const SidebarLink = ({ to, ...rest }) =>
<Route path={to}>
{({ match }) =>
<li className={match ? 'active' : ''}>
<Link to={to} {...rest} />
</li>}
</Route>
export default () =>
<ul>
<SidebarLink to="/somewhere" />
<SidebarLink to="/somewhere-else" />
</ul>
@drodriques
Copy link

Found this very helpful, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment