Skip to content

Commit

Permalink
Fix ReactShallowRenderer not rerendering when calling forceUpdate() (#…
Browse files Browse the repository at this point in the history
…11439)

* Fix ReactShallowRenderer not rerendering when calling forceUpdate()

* Style nit
  • Loading branch information
d4rky-pl authored and gaearon committed Nov 3, 2017
1 parent c2b68da commit 779d23f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ class ReactShallowRenderer {
// Fallback to previous instance state to support rendering React.cloneElement()
const state = this._newState || this._instance.state || emptyObject;

if (typeof this._instance.shouldComponentUpdate === 'function') {
if (
typeof this._instance.shouldComponentUpdate === 'function' &&
!this._forcedUpdate
) {
if (
this._forcedUpdate ||
this._instance.shouldComponentUpdate(props, state, context) === false
) {
this._instance.context = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ describe('ReactShallowRenderer', () => {
expect(scuCounter).toEqual(0);
});

it('should rerender when calling forceUpdate', () => {
let renderCounter = 0;
class SimpleComponent extends React.Component {
render() {
renderCounter += 1;
return <div />;
}
}

const shallowRenderer = createRenderer();
shallowRenderer.render(<SimpleComponent />);
expect(renderCounter).toEqual(1);

const instance = shallowRenderer.getMountedInstance();
instance.forceUpdate();
expect(renderCounter).toEqual(2);
});

it('should shallow render a functional component', () => {
function SomeComponent(props, context) {
return (
Expand Down

0 comments on commit 779d23f

Please sign in to comment.