Skip to content

Instantly share code, notes, and snippets.

@rande
Created February 4, 2015 21:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rande/cbdd61017f4e2a83b2eb to your computer and use it in GitHub Desktop.
Save rande/cbdd61017f4e2a83b2eb to your computer and use it in GitHub Desktop.
Stub For React Router
/**
* From https://github.com/rackt/react-router/blob/master/docs/guides/testing.md
*
* var stubRouterContext = require('./stubRouterContext');
* var IndividualComponent = require('./IndividualComponent');
* var Subject = stubRouterContext(IndividualComponent, {someProp: 'foo'});
* React.render(<Subject/>, testElement);
*/
var React = require('react');
var _ = require('lodash');
var func = React.PropTypes.func;
var stubRouterContext = function(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: function() {
return _.merge({}, {
makePath: function() {},
makeHref: function() {},
transitionTo: function() {},
replaceWith: function() {},
goBack: function() {},
getCurrentPath: function() {},
getCurrentRoutes: function() {},
getCurrentPathname: function() {},
getCurrentParams: function() {},
getCurrentQuery: function() {},
isActive: function() {},
}, stubs);
},
render: function() {
return <Component {...props} />
}
});
};
module.exports = stubRouterContext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment