Skip to content

Commit 9f25251

Browse files
d4rky-plgaearon
authored andcommitted
Fix ReactShallowRenderer not rerendering when calling forceUpdate() (#11439)
* Fix ReactShallowRenderer not rerendering when calling forceUpdate() * Style nit
1 parent 0cd509d commit 9f25251

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/ReactShallowRenderer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ class ReactShallowRenderer {
147147
// Fallback to previous instance state to support rendering React.cloneElement()
148148
const state = this._newState || this._instance.state || emptyObject;
149149

150-
if (typeof this._instance.shouldComponentUpdate === 'function') {
150+
if (
151+
typeof this._instance.shouldComponentUpdate === 'function' &&
152+
!this._forcedUpdate
153+
) {
151154
if (
152-
this._forcedUpdate ||
153155
this._instance.shouldComponentUpdate(props, state, context) === false
154156
) {
155157
this._instance.context = context;

src/__tests__/ReactShallowRenderer-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ describe('ReactShallowRenderer', () => {
142142
expect(scuCounter).toEqual(0);
143143
});
144144

145+
it('should rerender when calling forceUpdate', () => {
146+
let renderCounter = 0;
147+
class SimpleComponent extends React.Component {
148+
render() {
149+
renderCounter += 1;
150+
return <div />;
151+
}
152+
}
153+
154+
const shallowRenderer = createRenderer();
155+
shallowRenderer.render(<SimpleComponent />);
156+
expect(renderCounter).toEqual(1);
157+
158+
const instance = shallowRenderer.getMountedInstance();
159+
instance.forceUpdate();
160+
expect(renderCounter).toEqual(2);
161+
});
162+
145163
it('should shallow render a functional component', () => {
146164
function SomeComponent(props, context) {
147165
return (

0 commit comments

Comments
 (0)