Skip to content

Instantly share code, notes, and snippets.

@oieduardorabelo
Forked from stipsan/App-test.jsx
Last active June 16, 2017 09:44
Show Gist options
  • Save oieduardorabelo/c3a6a7f0bf3983231a55895a81af9c80 to your computer and use it in GitHub Desktop.
Save oieduardorabelo/c3a6a7f0bf3983231a55895a81af9c80 to your computer and use it in GitHub Desktop.
Testando com Jest: Dica #6
import renderer from 'react-test-renderer'
import App from '../App'
jest.mock('react-router-dom', () => ({
Link: 'Link',
Route: ({ children, ...props }) =>
typeof children === 'function'
? children({ match: path === '/somewhere' })
: createElement('Route', props)
}))
it('should render correctly', () => {
const component = renderer.create(<App />)
expect(component.toJSON()).toMatchSnapshot()
})
import { Route, Link } from 'react-router-dom'
import Somewhere from './Somewhere'
import SomewhereElse from './SomewhereElse'
const SidebarLink = ({ to, ...rest }) =>
<Route path={to}>
{({ match }) =>
<li className={match ? 'active' : ''}>
<Link to={to} {...rest} />
</li>}
</Route>
export default () =>
<div>
<ul>
<SidebarLink to="/somewhere" />
<SidebarLink to="/somewhere-else" />
</ul>
<Route path="/somewhere" component={Somewhere} />
<Route path="/somewhere-else" component={SomewhereElse} />
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment