我一直在玩reactjs
TestUtils
。这非常令人沮丧。我不确定,但经过几个小时的试验。在我看来,浅显渲染一个组件后,我不能使用findRenderedComponentWithType
或scryRenderedComponentsWithType
。实际上,我不知道如何从ReactComponent tree
中提取出它的类型。我不知道为什么,因为我对reactjs
非常陌生,我不确定我能做什么或不能做什么。浅层渲染组件和scryRenderedComponentsWithType
例如:
var Layout = React.createClass({
render: function(){
console.log(this.props);
return (
<div id="data-trader">
<header className="header">
<LogoElement />
<UserMenu user={this.props.user} />
</header>
<div className="container max">
<div className="main">
<RouteHandler path={ this.props.path } query={ this.props.query } params={ this.props.params } user={this.props.user} />
</div>
<Footer />
</div>
</div>
);
}
});
测试:
describe('Shallow Layout', function(){
beforeEach(function(){
fakeDOM = TestUtils.createRenderer();
});
// PASS
it('should exist as a component', function(done){
expect(<Layout/>).to.exist;
done();
});
// PASS
it('should have id=data-trader', function(done){
fakeDOM.render(<Layout />);
component = fakeDOM.getRenderOutput();
expect(component.props.id).to.eql('data-trader');
done();
});
it('get children', function(done) {
var StubbedComponent = TestHelpers.stubRouterContext(component);
var k = TestUtils.findRenderedComponentWithType(StubbedComponent, UserMenu);
console.log(k.length);
done();
})
});
我得到这个错误与findRenderedComponentWithType
Error: Did not find exactly one match for componentType:function UserMenu()