Skip to content

Instantly share code, notes, and snippets.

@jbasdf
Created June 4, 2015 00:23
Show Gist options
  • Save jbasdf/81e95f54f21389bfcdf2 to your computer and use it in GitHub Desktop.
Save jbasdf/81e95f54f21389bfcdf2 to your computer and use it in GitHub Desktop.
Stub context for React Router and Material UI >= 0.8.0
"use strict";
import React from "react";
import assign from "object-assign";
var { func } = React.PropTypes;
var mui = require('material-ui');
var ThemeManager = new mui.Styles.ThemeManager();
export default (Component, props, stubs) => {
function RouterStub(){ }
assign(RouterStub, {
makePath(){},
makeHref(){},
transitionTo(){},
replaceWith(){},
goBack(){},
getCurrentPath(){},
getCurrentRoutes(){},
getCurrentPathname(){},
getCurrentParams(){},
getCurrentQuery(){},
isActive(){},
getRouteAtDepth(){},
setRouteComponentAtDepth(){}
}, stubs);
class Stubber extends React.Component {
getChildContext(){
return {
router: RouterStub,
routeDepth: 0,
muiTheme: ThemeManager.getCurrentTheme()
};
}
// Use to get access to the component instance in case you need to spyOn a method of the component.
originalComponent(){
return this.refs.originalComponent;
}
render(){
return <Component ref="originalComponent" {...props} />;
}
}
Stubber.childContextTypes = {
router: React.PropTypes.func,
routeDepth: React.PropTypes.number,
muiTheme: React.PropTypes.object
};
return Stubber;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment