Merge pull request #4918 from glenjamin/shallow-getinstance

Expose component instance in shallow rendering
This commit is contained in:
Ben Alpert
2015-10-07 12:52:09 -07:00
2 changed files with 19 additions and 0 deletions
+4
View File
@@ -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;
+15
View File
@@ -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: {