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 2, 2015
1 parent 88bae3f commit 4a30d74
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 React Element.' +
'TestUtils.Simulate will not work if you are utilizing shallowRender.'
);
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 shallowRender', 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 React Element.' +
'TestUtils.Simulate will not work if you are utilizing shallowRender.'
);
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 4a30d74

Please sign in to comment.