@samitouri / QOS-React / commits / 779d23f72e

Fix ReactShallowRenderer not rerendering when calling forceUpdate() (#11439)

* Fix ReactShallowRenderer not rerendering when calling forceUpdate() * Style nit

Michał Matyas committed Nov 3, 2017 at 12:03 UTC 779d23f72e7f2c763f7a3e3373a4134c54356fd6
2 files changed +22 -2
packages/react-test-renderer/src/ReactShallowRenderer.js
+4 -2
@@ -147,9 +147,11 @@ class ReactShallowRenderer {
147 // Fallback to previous instance state to support rendering React.cloneElement()
148 const state = this._newState || this._instance.state || emptyObject;
149
150 - if (typeof this._instance.shouldComponentUpdate === 'function') {
150 + if (
151 + typeof this._instance.shouldComponentUpdate === 'function' &&
152 + !this._forcedUpdate
153 + ) {
154 if (
152 - this._forcedUpdate ||
155 this._instance.shouldComponentUpdate(props, state, context) === false
156 ) {
157 this._instance.context = context;
packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.js
+18
@@ -142,6 +142,24 @@ describe('ReactShallowRenderer', () => {
142 expect(scuCounter).toEqual(0);
143 });
144
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 +
163 it('should shallow render a functional component', () => {
164 function SomeComponent(props, context) {
165 return (