mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Show a friendly error when using TestUtils.Simulate with shallow rendering
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user