Last active
October 20, 2015 04:41
-
-
Save ryanflorence/1f72da0cd9e507ebec29 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** @jsx React.DOM */ | |
function makeStubbedDescriptor(component, props, contextStubs) { | |
var TestWrapper = React.createClass({ | |
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 | |
}, | |
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: {} | |
}, contextStubs); | |
}, | |
render: function() { | |
this.props.ref = '__subject__'; | |
return component(this.props); | |
} | |
}); | |
return TestWrapper(props); | |
} | |
// usage | |
var component = React.renderComponent(makeStubbedDescriptor(App, {}), document.body); | |
// do stuff with component now and the router isn't in your way |
You can also require the merge
packaged with React: var merge = require('react/lib/merge')
I added this:
routeHandlers: React.PropTypes.array.isRequired,
getRouteAtDepth: React.PropTypes.func.isRequired,
getRouteComponents: React.PropTypes.func.isRequired,
getCurrentParams: React.PropTypes.func.isRequired
and this:
routeHandlers: [{}],
getRouteAtDepth: function() {},
getRouteComponents: function() { return {}; },
getCurrentParams: function() {}
to make it work with RouteHandler
and State
mixins.
Its works, but I receive warnings
Warning: Required context `getCurrentPath` was not specified in `Link`. Check the render method of `TargetScan`.
Warning: Required context `getCurrentRoutes` was not specified in `Link`. Check the render method of `TargetScan`.
Warning: Required context `getCurrentPathname` was not specified in `Link`. Check the render method of `TargetScan`.
Warning: Required context `getCurrentQuery` was not specified in `Link`. Check the render method of `TargetScan`.
Warning: Required context `getCurrentPath` was not specified in `Link`. Check the render method of `TargetScan`.
Warning: Required context `getCurrentRoutes` was not specified in `Link`. Check the render method of `TargetScan`.
Warning: Required context `getCurrentPathname` was not specified in `Link`. Check the render method of `TargetScan`.
Warning: Required context `getCurrentQuery` was not specified in `Link`. Check the render method of `TargetScan`.
Can you suggest what to do?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This requires a
merge
function which I put at the top of the inside of themakeStubbedDescriptor
function.