Skip to content

Instantly share code, notes, and snippets.

@MicheleBertoli
Created July 17, 2015 15:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MicheleBertoli/5482811aa2942709c063 to your computer and use it in GitHub Desktop.
Save MicheleBertoli/5482811aa2942709c063 to your computer and use it in GitHub Desktop.
If you want to test a react 0.13.3 component and you are using the react-router 0.13.3, you need this:
import React, {Component} from 'react';
export default ReactRouterContext = (Element) => {
class RouterStub {}
RouterStub.makePath = () => {};
RouterStub.makeHref = () => {};
RouterStub.transitionTo = () => {};
RouterStub.replaceWith = () => {};
RouterStub.goBack = () => {};
RouterStub.getCurrentPath = () => {};
RouterStub.getCurrentRoutes = () => {};
RouterStub.getCurrentPathname = () => {};
RouterStub.getCurrentParams = () => {};
RouterStub.getCurrentQuery = () => {};
RouterStub.isActive = () => {};
RouterStub.getRouteAtDepth = () => {};
RouterStub.setRouteComponentAtDepth = () => {};
class StubbedComponent extends Component {
getChildContext() {
return {
router: RouterStub,
routeDepth: 0
};
}
render() {
return <Element {...this.props} />;
}
}
StubbedComponent.childContextTypes = {
router: React.PropTypes.func,
routeDepth: React.PropTypes.number
};
return StubbedComponent;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment