我一直在玩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()