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

* Fix ReactShallowRenderer not rerendering when calling forceUpdate()

* Style nit
This commit is contained in:
Michał Matyas
2017-11-03 11:03:06 +00:00
committed by Dan Abramov
parent c2b68dae64
commit 779d23f72e
2 changed files with 22 additions and 2 deletions
+4 -2
View File
@@ -147,9 +147,11 @@ class ReactShallowRenderer {
// Fallback to previous instance state to support rendering React.cloneElement()
const state = this._newState || this._instance.state || emptyObject;
if (typeof this._instance.shouldComponentUpdate === 'function') {
if (
typeof this._instance.shouldComponentUpdate === 'function' &&
!this._forcedUpdate
) {
if (
this._forcedUpdate ||
this._instance.shouldComponentUpdate(props, state, context) === false
) {
this._instance.context = context;
@@ -142,6 +142,24 @@ describe('ReactShallowRenderer', () => {
expect(scuCounter).toEqual(0);
});
it('should rerender when calling forceUpdate', () => {
let renderCounter = 0;
class SimpleComponent extends React.Component {
render() {
renderCounter += 1;
return <div />;
}
}
const shallowRenderer = createRenderer();
shallowRenderer.render(<SimpleComponent />);
expect(renderCounter).toEqual(1);
const instance = shallowRenderer.getMountedInstance();
instance.forceUpdate();
expect(renderCounter).toEqual(2);
});
it('should shallow render a functional component', () => {
function SomeComponent(props, context) {
return (