【发布时间】:2016-01-29 13:55:28
【问题描述】:
很抱歉,我一直在尝试通过单击按钮来关闭我的 React 模式。 Modal 尽可能简单,我已经尝试了所有我能想到或找到的方法,但我仍然无法查询它的子节点。
模态组件:
var React = require('react');
var Modal = require('react-bootstrap').Modal;
var Button = require('react-bootstrap').Button;
var MyModal = React.createClass({
...
render: function() {
return (
<Modal className="my-modal-class" show={this.props.show}>
<Modal.Header>
<Modal.Title>My Modal</Modal.Title>
</Modal.Header>
<Modal.Body>
Hello, World!
</Modal.Body>
<Modal.Footer>
<Button onClick={this.props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
);
}
});
我的目标是测试关闭按钮在被点击时是否会触发onHide() 函数。
我的测试文件:
describe('MyModal.jsx', function() {
it('tests the Close Button', function() {
var spy = sinon.spy();
var MyModalComponent = TestUtils.renderIntoDocument(
<MyModal show onHide={spy}/>
);
// This passes
TestUtils.findRenderedComponentWithType(MyModalComponent, MyModal);
// This fails
var CloseButton = TestUtils.findRenderedDOMComponentWithTag(MyModalComponent, 'button');
// Never gets here
TestUtils.Simulate.click(CloseButton);
expect(spy.calledOnce).to.be.true;
});
});
无论我怎么尝试,我似乎都找不到关闭按钮。
【问题讨论】:
-
你在开玩笑吗?我从未使用过 jest,但我认为它会自动模拟依赖组件。这可能是问题吗?
-
@AbhishekJain 我正在使用摩卡
-
是
<Button>一个用户定义的类还是你想要做<button>? -
@HenrikAndersson 我应该澄清这是 React-Bootstrap 的一部分
标签: javascript unit-testing reactjs bootstrap-modal react-jsx