Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5284 - Show a friendly error when using TestUtils.Simulate with shallow rendering #5358

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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