Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReactShallowRenderer not rerendering when calling forceUpdate() #11439

Merged
merged 2 commits into from Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Expand Up @@ -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;
Expand Down
Expand Up @@ -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 (
Expand Down