Created
May 28, 2017 17:30
-
-
Save biern/610de894eb5dd66eeec9d2b421f060b6 to your computer and use it in GitHub Desktop.
Render Route with enzyme shallow() proof of concept
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 React from 'react'; | |
import { Route } from 'react-router-dom'; | |
export default class RouteTest extends React.PureComponent { | |
render() { | |
return ( | |
<div> | |
<Route path="/hello" render={() => (<div>Hello world!</div>)} /> | |
</div> | |
); | |
} | |
} |
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 React from 'react'; | |
import { Router, Route } from 'react-router-dom'; | |
import createHistory from 'history/createBrowserHistory' | |
import RouteTest from './RouteTest'; | |
function shallowWithRoutes(component, path) { | |
const history = createHistory(); | |
const routeContext = { | |
router: { | |
history: history, | |
route: { | |
location: { pathname: path, } | |
}, | |
}, | |
} | |
return shallow(component).find(Route).dive({ | |
context: routeContext, | |
}); | |
}; | |
describe('(Component) RouteTest', () => { | |
it('should render hello', function () { | |
const wrapper = shallowWithRoutes( | |
<RouteTest />, '/hello' | |
); | |
expect(wrapper).toMatchSnapshot(); | |
}); | |
it('should not render hello', function () { | |
const wrapper = shallowWithRoutes( | |
<RouteTest />, '/goodbye' | |
); | |
expect(wrapper).toMatchSnapshot(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment