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 forceUpdate in shallow test renderer #11239

Merged
merged 1 commit into from
Oct 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/renderers/testing/ReactShallowRendererEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ReactShallowRenderer {
this._newState = null;
this._rendered = null;
this._rendering = false;
this._forcedUpdate = false;
this._updater = new Updater(this);
}

Expand Down Expand Up @@ -154,11 +155,13 @@ class ReactShallowRenderer {

if (typeof this._instance.shouldComponentUpdate === 'function') {
if (
this._forcedUpdate ||
this._instance.shouldComponentUpdate(props, state, context) === false
) {
this._instance.context = context;
this._instance.props = props;
this._instance.state = state;
this._forcedUpdate = false;

return;
}
Expand Down Expand Up @@ -188,6 +191,7 @@ class Updater {
}

enqueueForceUpdate(publicInstance, callback, callerName) {
this._renderer._forcedUpdate = true;
Copy link
Collaborator

@gaearon gaearon Oct 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're adding a new field can you also initialize it in the constructor please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

this._renderer.render(this._renderer._element, this._renderer._context);

if (typeof callback === 'function') {
Expand Down
20 changes: 20 additions & 0 deletions src/renderers/testing/__tests__/ReactShallowRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ describe('ReactShallowRenderer', () => {
expect(shallowRenderer.getRenderOutput()).toEqual(<div>2</div>);
});

it('should not run shouldComponentUpdate during forced update', () => {
let scuCounter = 0;
class SimpleComponent extends React.Component {
shouldComponentUpdate() {
scuCounter++;
}
render() {
return <div />;
}
}

const shallowRenderer = createRenderer();
shallowRenderer.render(<SimpleComponent />);
expect(scuCounter).toEqual(0);

const instance = shallowRenderer.getMountedInstance();
instance.forceUpdate();
expect(scuCounter).toEqual(0);
});

it('should shallow render a functional component', () => {
function SomeComponent(props, context) {
return (
Expand Down