Show a friendly error when using TestUtils.Simulate with shallow rendering

This commit is contained in:
conorhastings
2015-11-01 20:01:58 -05:00
parent b9371bc216
commit fbb8d2fe2d
2 changed files with 26 additions and 0 deletions
+5
View File
@@ -470,6 +470,11 @@ ReactShallowRenderer.prototype._render = function(element, transaction, context)
function makeSimulator(eventType) {
return function(domComponentOrNode, eventData) {
var node;
invariant(
!React.isValidElement(domComponentOrNode),
'TestUtils.Simulate expects a component instance and not a ReactElement.' +
'TestUtils.Simulate will not work if you are using shallow rendering.'
);
if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
node = findDOMNode(domComponentOrNode);
} else if (domComponentOrNode.tagName) {
+21
View File
@@ -421,6 +421,27 @@ describe('ReactTestUtils', function() {
expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
});
it('should throw when attempting to use ReactTestUtils.Simulate with shallow rendering', function() {
var SomeComponent = React.createClass({
render: function() {
return (
<div onClick={this.props.handleClick}>
hello, world.
</div>
);
},
});
var handler = jasmine.createSpy('spy');
var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SomeComponent handleClick={handler} />);
var result = shallowRenderer.getRenderOutput();
expect(() => ReactTestUtils.Simulate.click(result)).toThrow(
'TestUtils.Simulate expects a component instance and not a ReactElement.' +
'TestUtils.Simulate will not work if you are using shallow rendering.'
);
expect(handler).not.toHaveBeenCalled();
});
it('can scry with stateless components involved', function() {
var Stateless = () => <div><hr /></div>;
var SomeComponent = React.createClass({