Skip to content

Commit

Permalink
ReactShallowRenderer.render returns the rendered output
Browse files Browse the repository at this point in the history
  • Loading branch information
simonewebdesign committed Nov 8, 2015
1 parent 99fbde5 commit c8b8c81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
18 changes: 10 additions & 8 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,6 @@ var ReactShallowRenderer = function() {
this._instance = null;
};

ReactShallowRenderer.prototype.getRenderOutput = function() {
return (
(this._instance && this._instance._renderedComponent &&
this._instance._renderedComponent._renderedOutput)
|| null
);
};

ReactShallowRenderer.prototype.getMountedInstance = function() {
return this._instance ? this._instance._instance : null;
};
Expand Down Expand Up @@ -435,6 +427,16 @@ ReactShallowRenderer.prototype.render = function(element, context) {
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
this._render(element, transaction, context);
ReactUpdates.ReactReconcileTransaction.release(transaction);

return this.getRenderOutput();
};

ReactShallowRenderer.prototype.getRenderOutput = function() {
return (
(this._instance && this._instance._renderedComponent &&
this._instance._renderedComponent._renderedOutput)
|| null
);
};

ReactShallowRenderer.prototype.unmount = function() {
Expand Down
25 changes: 8 additions & 17 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ describe('ReactTestUtils', function() {
});

var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SomeComponent />);

var result = shallowRenderer.getRenderOutput();
var result = shallowRenderer.render(<SomeComponent />);

expect(result.type).toBe('div');
expect(result.props.children).toEqual([
Expand Down Expand Up @@ -95,9 +93,7 @@ describe('ReactTestUtils', function() {
});

var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SomeComponent />);

var result = shallowRenderer.getRenderOutput();
var result = shallowRenderer.render(<SomeComponent />);

expect(result).toBe(null);
});
Expand Down Expand Up @@ -148,16 +144,14 @@ describe('ReactTestUtils', function() {
});

var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SomeComponent />);
var result = shallowRenderer.getRenderOutput();
var result = shallowRenderer.render(<SomeComponent />);
expect(result.type).toBe('div');
expect(result.props.children).toEqual([
<span className="child1" />,
<span className="child2" />,
]);

shallowRenderer.render(<SomeComponent aNew="prop" />);
var updatedResult = shallowRenderer.getRenderOutput();
var updatedResult = shallowRenderer.render(<SomeComponent aNew="prop" />);
expect(updatedResult.type).toBe('a');

var mockEvent = {};
Expand Down Expand Up @@ -194,8 +188,7 @@ describe('ReactTestUtils', function() {
});

var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SimpleComponent />);
var result = shallowRenderer.getRenderOutput();
var result = shallowRenderer.render(<SimpleComponent />);
expect(result).toEqual(<div />);
});

Expand All @@ -210,10 +203,9 @@ describe('ReactTestUtils', function() {
});

var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SimpleComponent />, {
var result = shallowRenderer.render(<SimpleComponent />, {
name: 'foo',
});
var result = shallowRenderer.getRenderOutput();
expect(result).toEqual(<div>foo</div>);
});

Expand All @@ -229,7 +221,6 @@ describe('ReactTestUtils', function() {
'NonExistentClass'
);
expect(scryResults.length).toBe(0);

});

it('can scryRenderedDOMComponentsWithClass with className contains \\n', function() {
Expand Down Expand Up @@ -445,8 +436,8 @@ describe('ReactTestUtils', function() {
});
var handler = jasmine.createSpy('spy');
var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SomeComponent handleClick={handler} />);
var result = shallowRenderer.getRenderOutput();
var result = shallowRenderer.render(<SomeComponent handleClick={handler} />);

expect(() => ReactTestUtils.Simulate.click(result)).toThrow(
'TestUtils.Simulate expects a component instance and not a ReactElement.' +
'TestUtils.Simulate will not work if you are using shallow rendering.'
Expand Down

0 comments on commit c8b8c81

Please sign in to comment.