Skip to content

Commit

Permalink
Show a friendly error when using TestUtils.Simulate with shallow rend…
Browse files Browse the repository at this point in the history
…ering
  • Loading branch information
conorhastings committed Nov 3, 2015
1 parent b9371bc commit fbb8d2f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit fbb8d2f

Please sign in to comment.