我正在尝试在 React 类上测试公共方法。正如您在第一个规范中看到的那样,这很容易,但是如果我需要提供上下文,则该组件需要包装在一个复合组件中,并且我看不到获取子公共方法的方法(第二个规范) .
it('consider this', function(){
var Stub = React.createClass({
doSomething: function() {
console.log('whoohoo!');
},
render: function(){
return null
}
});
var StubElement = TestUtils.renderIntoDocument(React.createElement(Stub, {}));
StubElement.doSomething(); //should log
});
it.only('now consider this with a context', function(){
var Stub = React.createClass({
contextTypes: {
location: React.PropTypes.object
},
doSomething: function() {
console.log('whoohoo!', this.context.location.pathname);
},
render: function(){
return null
}
});
var ContextWrapper = React.createClass({
childContextTypes: {
location: React.PropTypes.object
},
getChildContext: function() {
return {
location: window.location
}
},
render: function() {
return React.createElement(Stub, {})
}
});
var StubElement = TestUtils.renderIntoDocument(React.createElement(ContextWrapper, {}));
console.log(StubElement);
// how do I get public access to doSomething() ?
});
关于如何使用该方法的任何想法?谢谢!