Skip to content

Instantly share code, notes, and snippets.

@jaketrent
Last active October 10, 2022 04:37
Show Gist options
  • Save jaketrent/f68028eedd4f1ddf7997 to your computer and use it in GitHub Desktop.
Save jaketrent/f68028eedd4f1ddf7997 to your computer and use it in GitHub Desktop.
React-router stub context (for testing)
// from https://github.com/rackt/react-router/blob/master/docs/guides/testing.md
var assign = require("react/lib/Object.assign");
var React = require("react");
var func = React.PropTypes.func;
module.exports = (Component, props, stubs) => {
return React.createClass({
childContextTypes: {
makePath: func,
makeHref: func,
transitionTo: func,
replaceWith: func,
goBack: func,
getCurrentPath: func,
getCurrentRoutes: func,
getCurrentPathname: func,
getCurrentParams: func,
getCurrentQuery: func,
isActive: func
},
getChildContext () {
return assign({
makePath () {},
makeHref () {},
transitionTo () {},
replaceWith () {},
goBack () {},
getCurrentPath () {},
getCurrentRoutes () {},
getCurrentPathname () {},
getCurrentParams () { return {};},
getCurrentQuery () { return {}; },
isActive () {}
}, stubs);
},
render () {
return (
<Component {...props} />
);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment