Тестирование компонента React Modal
Извините, но у меня были самые трудные времена, когда я пытался проверить закрытие моего 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;
});
});
Неважно, что я пытаюсь, я не могу найти кнопку закрытия.