Expose component instance in shallow rendering
Fixes #4056
Glen Mailer committed
Sep 20, 2015 at 15:16 UTC
241533782c7a2f5459a0a0e384b238122a67753f
2 files changed
+19
src/test/ReactTestUtils.js
+4
@@ -356,6 +356,10 @@ ReactShallowRenderer.prototype.getRenderOutput = function() {
356
);
357
};
358
359
+ReactShallowRenderer.prototype.getMountedInstance = function() {
360
+ return this._instance ? this._instance._instance : null;
361
+};
362
+
363
var NoopInternalComponent = function(element) {
364
this._renderedOutput = element;
365
this._currentElement = element;
src/test/__tests__/ReactTestUtils-test.js
+15
@@ -160,6 +160,21 @@ describe('ReactTestUtils', function() {
160
expect(updatedResultCausedByClick.props.className).toBe('was-clicked');
161
});
162
163
+ it('can access the mounted component instance', function() {
164
+ var SimpleComponent = React.createClass({
165
+ someMethod: function() {
166
+ return this.props.n;
167
+ },
168
+ render: function() {
169
+ return <div>{this.props.n}</div>;
170
+ },
171
+ });
172
+
173
+ var shallowRenderer = ReactTestUtils.createRenderer();
174
+ shallowRenderer.render(<SimpleComponent n={5} />);
175
+ expect(shallowRenderer.getMountedInstance().someMethod()).toEqual(5);
176
+ });
177
+
178
it('can shallowly render components with contextTypes', function() {
179
var SimpleComponent = React.createClass({
180
contextTypes: {