我正在尝试通过使用findRenderedComponentWithType
和findRenderedComponentWithType
查找组件来进行一些单元测试。但是,我遇到了一些麻烦。
我有一个浅渲染组件<Layout />
,我想在其中找到<UserMenu />
.
var Layout = React.createClass({
render: function(){
console.log(this.props);
return (
<div id="data-trader">
<header className="header">
<UserMenu user={this.props.user} />
</header>
<SideMenu user={this.props.user}/>
</div>
);
}
});
module.exports = Layout;
在我的测试文件中,我尝试了这个:
describe('Layout', function() {
beforeEach(function(){
fakeDOM = TestUtils.createRenderer();
it('should exist as a component', function(done) {
expect(<Layout/>).to.exist;
done();
});
fakeDOM.render(<Layout />);
renderedFakeDOMOutput = fakeDOM.getRenderOutput();
});
it('should have login button when props.user is undefined', function(done) {
renderedFakeDOMOutput.props.user = undefined;
let UserMenuComponent = TestUtils.scryRenderedComponentsWithType(renderedFakeDOMOutput, UserMenu);
done();
});
});
但是,scryRenderedComponentsWithType
找不到findRenderedComponentWithType
任何类型为 的组件UserMenu
。
我还尝试在组件所在exports
的位置创建另一个文件,UserMenu
但我得到相同的输出或找到 0(数组长度 0)或在未找到组件时出错(Error: Did not find exactly one match for componentType:function UserMenu()
)。