Skip to content

Commit

Permalink
Merge pull request #4918 from glenjamin/shallow-getinstance
Browse files Browse the repository at this point in the history
Expose component instance in shallow rendering
  • Loading branch information
sophiebits committed Oct 7, 2015
2 parents 501a276 + 2415337 commit 8ebbb78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,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;
Expand Down
15 changes: 15 additions & 0 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>{this.props.n}</div>;
},
});

var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SimpleComponent n={5} />);
expect(shallowRenderer.getMountedInstance().someMethod()).toEqual(5);
});

it('can shallowly render components with contextTypes', function() {
var SimpleComponent = React.createClass({
contextTypes: {
Expand Down

0 comments on commit 8ebbb78

Please sign in to comment.