mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Merge pull request #4918 from glenjamin/shallow-getinstance
Expose component instance in shallow rendering
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user