Reactjs 反应测试按钮属性禁用
我无法为按钮属性禁用编写正确的测试用例。我使用react插件TestUtils中的TestUtils 我有一个非常简单的组件:Reactjs 反应测试按钮属性禁用,reactjs,reactjs-testutils,react-test-renderer,Reactjs,Reactjs Testutils,React Test Renderer,我无法为按钮属性禁用编写正确的测试用例。我使用react插件TestUtils中的TestUtils 我有一个非常简单的组件: const propTypes = { disabled: PropTypes.func.isRequired }; class MyComponent extends Component { constructor(props) { super(props); } render() { return
const propTypes = {
disabled: PropTypes.func.isRequired
};
class MyComponent extends Component {
constructor(props) {
super(props);
}
render() {
return (
<span>
<button id="my-button" type="submit" disabled={this.props.disabled}>
MyButton
</button>
</span>
);
}
}
MyComponent.propTypes = propTypes;
export default MyComponent;
看起来您正在将函数分配给属性值,而不是函数的返回值,您可以使用
const props = {
disabled: function() {
return false;
}()
}
否则,您需要在测试时调用禁用的函数
expect( myButton.disabled() ).toEqual(false);
好的,谢谢。但是=>false和函数{return false;}之间有什么区别呢?使用=>false相当于函数{return false;},这很好,但是您指定的是disabled=function属性,而不是函数的值。您可以通过执行console.logtypeof props.disabled;来检查这一点;。。它将产生函数,而不是布尔值。通过添加,您可以调用函数,从而强制将立即返回值指定给disabled。
const props = {
disabled: function() {
return false;
}()
}
expect( myButton.disabled() ).toEqual(false);