Skip to content

Instantly share code, notes, and snippets.

@aaronshaf
Created January 23, 2015 21:56
Show Gist options
  • Select an option

  • Save aaronshaf/141a3dd9bde44394c86d to your computer and use it in GitHub Desktop.

Select an option

Save aaronshaf/141a3dd9bde44394c86d to your computer and use it in GitHub Desktop.
// Modified from: https://gist.github.com/rpflorence/1f72da0cd9e507ebec29
var React = require('react/addons')
var merge = require('lodash-node/modern/objects/assign')
function makeStubbedElement(Component, props, contextStubs) {
var TestWrapper = React.createClass({
displayName: 'StubbedElement',
childContextTypes: {
currentPath: React.PropTypes.string,
makePath: React.PropTypes.func.isRequired,
makeHref: React.PropTypes.func.isRequired,
transitionTo: React.PropTypes.func.isRequired,
replaceWith: React.PropTypes.func.isRequired,
goBack: React.PropTypes.func.isRequired,
isActive: React.PropTypes.func.isRequired,
activeRoutes: React.PropTypes.array.isRequired,
activeParams: React.PropTypes.object.isRequired,
activeQuery: React.PropTypes.object.isRequired,
location: React.PropTypes.object,
routes: React.PropTypes.array.isRequired,
namedRoutes: React.PropTypes.object.isRequired,
scrollBehavior: React.PropTypes.object,
routeHandlers: React.PropTypes.array.isRequired,
getRouteAtDepth: React.PropTypes.func.isRequired,
getRouteComponents: React.PropTypes.func.isRequired,
getCurrentParams: React.PropTypes.func.isRequired,
getCurrentPath: React.PropTypes.func.isRequired,
getCurrentQuery: React.PropTypes.func.isRequired,
getCurrentRoutes: React.PropTypes.func.isRequired,
getCurrentPathname: React.PropTypes.func.isRequired
},
getChildContext: function() {
return merge({
currentPath: '__STUB__',
makePath: function() {},
makeHref: function() { return '__STUB__'; },
transitionTo: function() {},
replaceWith: function() {},
goBack: function() {},
isActive: function() {},
activeRoutes: [],
activeParams: {},
activeQuery: {},
location: {},
routes: [],
namedRoutes: {},
scrollBehavior: {},
routeHandlers: [{}],
getRouteAtDepth: function() {},
getRouteComponents: function() { return {}; },
getCurrentParams: function() {},
getCurrentQuery: function() { return {} },
getCurrentPath: function() {},
getCurrentRoutes: function() {},
getCurrentPathname: function() {}
}, contextStubs);
},
render: function() {
props.ref = '__subject__';
return <Component {...props} />;
}
});
return TestWrapper;
}
module.exports = makeStubbedElement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment