Skip to content

Commit 03a3934

Browse files
d4rky-plgaearon
authored andcommitted
Fix forceUpdate in shallow test renderer (#11239)
1 parent c539be0 commit 03a3934

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/renderers/testing/ReactShallowRendererEntry.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ReactShallowRenderer {
3131
this._newState = null;
3232
this._rendered = null;
3333
this._rendering = false;
34+
this._forcedUpdate = false;
3435
this._updater = new Updater(this);
3536
}
3637

@@ -154,11 +155,13 @@ class ReactShallowRenderer {
154155

155156
if (typeof this._instance.shouldComponentUpdate === 'function') {
156157
if (
158+
this._forcedUpdate ||
157159
this._instance.shouldComponentUpdate(props, state, context) === false
158160
) {
159161
this._instance.context = context;
160162
this._instance.props = props;
161163
this._instance.state = state;
164+
this._forcedUpdate = false;
162165

163166
return;
164167
}
@@ -188,6 +191,7 @@ class Updater {
188191
}
189192

190193
enqueueForceUpdate(publicInstance, callback, callerName) {
194+
this._renderer._forcedUpdate = true;
191195
this._renderer.render(this._renderer._element, this._renderer._context);
192196

193197
if (typeof callback === 'function') {

src/renderers/testing/__tests__/ReactShallowRenderer-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ describe('ReactShallowRenderer', () => {
122122
expect(shallowRenderer.getRenderOutput()).toEqual(<div>2</div>);
123123
});
124124

125+
it('should not run shouldComponentUpdate during forced update', () => {
126+
let scuCounter = 0;
127+
class SimpleComponent extends React.Component {
128+
shouldComponentUpdate() {
129+
scuCounter++;
130+
}
131+
render() {
132+
return <div />;
133+
}
134+
}
135+
136+
const shallowRenderer = createRenderer();
137+
shallowRenderer.render(<SimpleComponent />);
138+
expect(scuCounter).toEqual(0);
139+
140+
const instance = shallowRenderer.getMountedInstance();
141+
instance.forceUpdate();
142+
expect(scuCounter).toEqual(0);
143+
});
144+
125145
it('should shallow render a functional component', () => {
126146
function SomeComponent(props, context) {
127147
return (

0 commit comments

Comments
 (0)