Skip to content

Commit 1598374

Browse files
committed
Fix forceUpdate in shallow test renderer
1 parent b623bf4 commit 1598374

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/renderers/testing/ReactShallowRendererEntry.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ class ReactShallowRenderer {
154154

155155
if (typeof this._instance.shouldComponentUpdate === 'function') {
156156
if (
157+
this._forcedUpdate ||
157158
this._instance.shouldComponentUpdate(props, state, context) === false
158159
) {
159160
this._instance.context = context;
160161
this._instance.props = props;
161162
this._instance.state = state;
163+
this._forcedUpdate = false;
162164

163165
return;
164166
}
@@ -188,6 +190,7 @@ class Updater {
188190
}
189191

190192
enqueueForceUpdate(publicInstance, callback, callerName) {
193+
this._renderer._forcedUpdate = true;
191194
this._renderer.render(this._renderer._element, this._renderer._context);
192195

193196
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)