From 241533782c7a2f5459a0a0e384b238122a67753f Mon Sep 17 00:00:00 2001 From: Glen Mailer Date: Sun, 20 Sep 2015 15:16:51 +0100 Subject: [PATCH] Expose component instance in shallow rendering Fixes #4056 --- src/test/ReactTestUtils.js | 4 ++++ src/test/__tests__/ReactTestUtils-test.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/test/ReactTestUtils.js b/src/test/ReactTestUtils.js index 7bc8cd04557aa..e2cfc270f5da8 100644 --- a/src/test/ReactTestUtils.js +++ b/src/test/ReactTestUtils.js @@ -356,6 +356,10 @@ ReactShallowRenderer.prototype.getRenderOutput = function() { ); }; +ReactShallowRenderer.prototype.getMountedInstance = function() { + return this._instance ? this._instance._instance : null; +}; + var NoopInternalComponent = function(element) { this._renderedOutput = element; this._currentElement = element; diff --git a/src/test/__tests__/ReactTestUtils-test.js b/src/test/__tests__/ReactTestUtils-test.js index a9b3d0042a12c..6ec356353ef8a 100644 --- a/src/test/__tests__/ReactTestUtils-test.js +++ b/src/test/__tests__/ReactTestUtils-test.js @@ -160,6 +160,21 @@ describe('ReactTestUtils', function() { expect(updatedResultCausedByClick.props.className).toBe('was-clicked'); }); + it('can access the mounted component instance', function() { + var SimpleComponent = React.createClass({ + someMethod: function() { + return this.props.n; + }, + render: function() { + return
{this.props.n}
; + }, + }); + + var shallowRenderer = ReactTestUtils.createRenderer(); + shallowRenderer.render(); + expect(shallowRenderer.getMountedInstance().someMethod()).toEqual(5); + }); + it('can shallowly render components with contextTypes', function() { var SimpleComponent = React.createClass({ contextTypes: {