From 4a30d7406dba7fbe061496304abc0645abdbe304 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..8bc6e41a6aff7 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 React Element.' + + 'TestUtils.Simulate will not work if you are utilizing shallowRender.' + ); 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..c6782d95e042d 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 shallowRender', 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 React Element.' + + 'TestUtils.Simulate will not work if you are utilizing shallowRender.' + ); + expect(handler).not.toHaveBeenCalled(); + }); + it('can scry with stateless components involved', function() { var Stateless = () =>

; var SomeComponent = React.createClass({