在调整浏览器窗口大小时,如何让React重新渲染视图?
我有一些块,我想在页面上单独布局,但我也希望它们在浏览器窗口更改时更新.最终结果将是Ben Holland的 Pinterest布局,但使用React编写的不仅仅是jQuery.我还有一段路要走.
这是我的应用程序:
var MyApp = React.createClass({
//does the http get from the server
loadBlocksFromServer: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
mimeType: 'textPlain',
success: function(data) {
this.setState({data: data.events});
}.bind(this)
});
},
getInitialState: function() {
return {data: []};
},
componentWillMount: function() {
this.loadBlocksFromServer();
},
render: function() {
return (
<div>
<Blocks data={this.state.data}/>
</div>
);
}
});
React.renderComponent(
<MyApp url="url_here"/>,
document.getElementById('view')
)
Run Code Online (Sandbox Code Playgroud)
然后我有了Block组件(相当于Pin上面Pinterest示例中的一个):
var Block = React.createClass({
render: function() {
return ( …Run Code Online (Sandbox Code Playgroud) 所以基本上当组件安装时,我有一个事件监听器监听resize事件.它切换isMobileView状态,然后将其作为道具传递给子项.因此,这是必要的,这是有效的,并经过测试.我是一个相当新的测试人员,我正试图找出一种方法,我可以编写一个测试来调整窗口大小并使所有逻辑发生并测试它是如何执行的.
这是代码 -
componentDidMount() {
this.setMobileViewState()
window.addEventListener('resize', this.setMobileViewState.bind(this));
}
setMobileViewState() {
if(document.documentElement.clientWidth <= this.props.mobileMenuShowWidth) {
this.setState({ isMobileView: true })
} else {
this.setState({ isMobileView: false })
}
}
Run Code Online (Sandbox Code Playgroud)
我知道代码有效,但我想为它编写一个测试.基本上只是确保状态正确变化的东西.
应该是一个简单的问题.如何在jsDom对象中设置宽度?
jsdom.env({
url:'http://testdatalocation',
scripts: ['http://code.jquery.com/jquery.js'],
done: function(errors, tstWindow) {
console.log(tstWindow.innerWidth);
};
}
});
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何让"innerWidth"成为1024以外的任何东西