Fix forceUpdate in shallow test renderer (#11239)
Michał Matyas committed
Oct 18, 2017 at 15:54 UTC
03a3934e0e6f9cc58575083b6886bec8f9903674
2 files changed
+24
src/renderers/testing/ReactShallowRendererEntry.js
+4
@@ -31,6 +31,7 @@ class ReactShallowRenderer {
31
this._newState = null;
32
this._rendered = null;
33
this._rendering = false;
34
+ this._forcedUpdate = false;
35
this._updater = new Updater(this);
36
}
37
@@ -154,11 +155,13 @@ class ReactShallowRenderer {
155
156
if (typeof this._instance.shouldComponentUpdate === 'function') {
157
if (
158
+ this._forcedUpdate ||
159
this._instance.shouldComponentUpdate(props, state, context) === false
160
) {
161
this._instance.context = context;
162
this._instance.props = props;
163
this._instance.state = state;
164
+ this._forcedUpdate = false;
165
166
return;
167
}
@@ -188,6 +191,7 @@ class Updater {
191
}
192
193
enqueueForceUpdate(publicInstance, callback, callerName) {
194
+ this._renderer._forcedUpdate = true;
195
this._renderer.render(this._renderer._element, this._renderer._context);
196
197
if (typeof callback === 'function') {
src/renderers/testing/__tests__/ReactShallowRenderer-test.js
+20
@@ -122,6 +122,26 @@ describe('ReactShallowRenderer', () => {
122
expect(shallowRenderer.getRenderOutput()).toEqual(<div>2</div>);
123
});
124
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
+
145
it('should shallow render a functional component', () => {
146
function SomeComponent(props, context) {
147
return (