测试 React Modal 组件
Testing a React Modal component
抱歉,我一直在尝试通过单击按钮来测试关闭我的 React Modal,这是最艰难的时刻。 Modal尽量简单,能想到或找到的都试过了,还是查询不到它的children
模态组件:
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;
});
});
无论我怎么尝试,我似乎都找不到关闭按钮。
我写了一个 jsFiddle using the React Base Fiddle (JSX) 来了解你的测试中发生了什么(我创建了我自己的 'spy',它在调用时简单地记录到控制台)。
我发现您找不到按钮的原因是因为它不存在于您期望的位置。
Bootstrap Modal Component (<Modal/>
) is actually contained within a React-Overlays modal component (called BaseModal
in the code, which is from here). This in turn renders a component called Portal
whose render method干脆returnsnull
。这是您试图在其上查找渲染组件的 null
值。
由于模态框不是以传统的 React 方式呈现的,因此 React 无法看到模态框以便在 TestUtils
上使用。一个完全独立的 <div/>
子节点放置在 document
正文中,这个新的 <div/>
用于构建模态。
因此,为了允许您使用 React 的 TestUtils
模拟点击(按钮上的点击处理程序仍然绑定到按钮的点击事件),您可以使用标准的 JS 方法来搜索 DOM 代替。按如下方式设置您的测试:
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 will get the actual DOM node of the button
var closeButton = document.body.getElementsByClassName("my-modal-class")[0].getElementsByClassName("btn btn-default")[0];
// Will now get here
TestUtils.Simulate.click(CloseButton);
expect(spy.calledOnce).to.be.true;
});
});
函数 getElementsByClassName
returns 具有 class 的元素集合,因此您必须从每个集合中取出第一个(并且在您的测试用例中,您唯一的一个)。
你的测试现在应该通过了^_^
这是我最终采用的解决方案。它将 React 渲染模态框的方式存根为只渲染 <div>
。
test-utils.js
:
var sinon = require('sinon');
var React = require('react');
var Modal = require('react-bootstrap').Modal;
module.exports = {
stubModal: function() {
var createElement = React.createElement;
modalStub = sinon.stub(React, 'createElement', function(elem) {
return elem === Modal ?
React.DOM.div.apply(this, [].slice.call(arguments, 1)) :
createElement.apply(this, arguments);
});
return modalStub;
},
stubModalRestore: function() {
if (modalStub) {
modalStub.restore();
modalStub = undefined;
} else {
console.error('Cannot restore nonexistent modalStub');
}
}
};
modal-test.jsx
var testUtils = require('./test-utils');
describe('test a Modal!', function() {
before(testUtils.stubModal);
after(testUtils.stubModalRestore);
it('renders stuff', function() {
var MyModalComponent = TestUtils.renderIntoDocument(
<MyModal show/>
);
// Do more stuff you normally expect you can do
});
});
抱歉,我一直在尝试通过单击按钮来测试关闭我的 React Modal,这是最艰难的时刻。 Modal尽量简单,能想到或找到的都试过了,还是查询不到它的children
模态组件:
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;
});
});
无论我怎么尝试,我似乎都找不到关闭按钮。
我写了一个 jsFiddle using the React Base Fiddle (JSX) 来了解你的测试中发生了什么(我创建了我自己的 'spy',它在调用时简单地记录到控制台)。
我发现您找不到按钮的原因是因为它不存在于您期望的位置。
Bootstrap Modal Component (<Modal/>
) is actually contained within a React-Overlays modal component (called BaseModal
in the code, which is from here). This in turn renders a component called Portal
whose render method干脆returnsnull
。这是您试图在其上查找渲染组件的 null
值。
由于模态框不是以传统的 React 方式呈现的,因此 React 无法看到模态框以便在 TestUtils
上使用。一个完全独立的 <div/>
子节点放置在 document
正文中,这个新的 <div/>
用于构建模态。
因此,为了允许您使用 React 的 TestUtils
模拟点击(按钮上的点击处理程序仍然绑定到按钮的点击事件),您可以使用标准的 JS 方法来搜索 DOM 代替。按如下方式设置您的测试:
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 will get the actual DOM node of the button
var closeButton = document.body.getElementsByClassName("my-modal-class")[0].getElementsByClassName("btn btn-default")[0];
// Will now get here
TestUtils.Simulate.click(CloseButton);
expect(spy.calledOnce).to.be.true;
});
});
函数 getElementsByClassName
returns 具有 class 的元素集合,因此您必须从每个集合中取出第一个(并且在您的测试用例中,您唯一的一个)。
你的测试现在应该通过了^_^
这是我最终采用的解决方案。它将 React 渲染模态框的方式存根为只渲染 <div>
。
test-utils.js
:
var sinon = require('sinon');
var React = require('react');
var Modal = require('react-bootstrap').Modal;
module.exports = {
stubModal: function() {
var createElement = React.createElement;
modalStub = sinon.stub(React, 'createElement', function(elem) {
return elem === Modal ?
React.DOM.div.apply(this, [].slice.call(arguments, 1)) :
createElement.apply(this, arguments);
});
return modalStub;
},
stubModalRestore: function() {
if (modalStub) {
modalStub.restore();
modalStub = undefined;
} else {
console.error('Cannot restore nonexistent modalStub');
}
}
};
modal-test.jsx
var testUtils = require('./test-utils');
describe('test a Modal!', function() {
before(testUtils.stubModal);
after(testUtils.stubModalRestore);
it('renders stuff', function() {
var MyModalComponent = TestUtils.renderIntoDocument(
<MyModal show/>
);
// Do more stuff you normally expect you can do
});
});