From fbb8d2fe2df0a4f89b769c5f2640fe51b05adaaa Mon Sep 17 00:00:00 2001 From: conorhastings Date: Sun, 1 Nov 2015 20:01:58 -0500 Subject: [PATCH] Show a friendly error when using TestUtils.Simulate with shallow rendering --- src/test/ReactTestUtils.js | 5 +++++ src/test/__tests__/ReactTestUtils-test.js | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/test/ReactTestUtils.js b/src/test/ReactTestUtils.js index 64b39fe5e3403..f400becc5f4ad 100644 --- a/src/test/ReactTestUtils.js +++ b/src/test/ReactTestUtils.js @@ -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) { diff --git a/src/test/__tests__/ReactTestUtils-test.js b/src/test/__tests__/ReactTestUtils-test.js index 00a6b5c0ce172..57f20971707a9 100644 --- a/src/test/__tests__/ReactTestUtils-test.js +++ b/src/test/__tests__/ReactTestUtils-test.js @@ -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 ( +
+ hello, world. +
+ ); + }, + }); + var handler = jasmine.createSpy('spy'); + var shallowRenderer = ReactTestUtils.createRenderer(); + shallowRenderer.render(); + 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 = () =>

; var SomeComponent = React.createClass({