Show a friendly error when using TestUtils.Simulate with shallow rendering
conorhastings committed
Nov 1, 2015 at 20:01 UTC
fbb8d2fe2df0a4f89b769c5f2640fe51b05adaaa
2 files changed
+26
src/test/ReactTestUtils.js
+5
@@ -470,6 +470,11 @@ ReactShallowRenderer.prototype._render = function(element, transaction, context)
470
function makeSimulator(eventType) {
471
return function(domComponentOrNode, eventData) {
472
var node;
473
+ invariant(
474
+ !React.isValidElement(domComponentOrNode),
475
+ 'TestUtils.Simulate expects a component instance and not a ReactElement.' +
476
+ 'TestUtils.Simulate will not work if you are using shallow rendering.'
477
+ );
478
if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
479
node = findDOMNode(domComponentOrNode);
480
} else if (domComponentOrNode.tagName) {
src/test/__tests__/ReactTestUtils-test.js
+21
@@ -421,6 +421,27 @@ describe('ReactTestUtils', function() {
421
expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({target: node}));
422
});
423
424
+ it('should throw when attempting to use ReactTestUtils.Simulate with shallow rendering', 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 ReactElement.' +
440
+ 'TestUtils.Simulate will not work if you are using shallow rendering.'
441
+ );
442
+ expect(handler).not.toHaveBeenCalled();
443
+ });
444
+
445
it('can scry with stateless components involved', function() {
446
var Stateless = () => <div><hr /></div>;
447
var SomeComponent = React.createClass({