Skip to content

Commit 4a30d74

Browse files
author
conorhastings
committed
Show a friendly error when using TestUtils.Simulate with shallow rendering
1 parent 88bae3f commit 4a30d74

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/test/ReactTestUtils.js

+5
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ ReactShallowRenderer.prototype._render = function(element, transaction, context)
470470
function makeSimulator(eventType) {
471471
return function(domComponentOrNode, eventData) {
472472
var node;
473+
invariant(
474+
!React.isValidElement(domComponentOrNode),
475+
'TestUtils.Simulate expects a component instance and not a React Element.' +
476+
'TestUtils.Simulate will not work if you are utilizing shallowRender.'
477+
);
473478
if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
474479
node = findDOMNode(domComponentOrNode);
475480
} else if (domComponentOrNode.tagName) {

src/test/__tests__/ReactTestUtils-test.js

+21
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,27 @@ describe('ReactTestUtils', function() {
421421
expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
422422
});
423423

424+
it('should throw when attempting to use ReactTestUtils.Simulate with shallowRender', function() {
425+
var SomeComponent = React.createClass({
426+
render: function() {
427+
return (
428+
<div onClick={this.props.handleClick}>
429+
hello, world.
430+
</div>
431+
);
432+
},
433+
});
434+
var handler = jasmine.createSpy('spy');
435+
var shallowRenderer = ReactTestUtils.createRenderer();
436+
shallowRenderer.render(<SomeComponent handleClick={handler} />);
437+
var result = shallowRenderer.getRenderOutput();
438+
expect(() => ReactTestUtils.Simulate.click(result)).toThrow(
439+
'TestUtils.Simulate expects a component instance and not a React Element.' +
440+
'TestUtils.Simulate will not work if you are utilizing shallowRender.'
441+
);
442+
expect(handler).not.toHaveBeenCalled();
443+
});
444+
424445
it('can scry with stateless components involved', function() {
425446
var Stateless = () => <div><hr /></div>;
426447
var SomeComponent = React.createClass({

0 commit comments

Comments
 (0)