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] shallow: x.find() should not change state of x #2061

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,18 @@ class ReactSixteenAdapter extends EnzymeAdapter {
return isElement(element);
}

shouldComponentUpdate(prevProps, root) {
const instance = root.instance();
if (instance !== null) {
const { updater, props } = instance;
return (
updater._renderer._newState !== null || !shallowEqual(prevProps, props)
);
}

return true;
}

isValidElementType(object) {
return !!object && isValidElementType(object);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2237,9 +2237,12 @@ describe('shallow', () => {
}
}
const wrapper = shallow(<MyComponent />, { disableLifecycleMethods: true });
expect(wrapper.find(Table).length).to.equal(0);
expect(wrapper.find(Table)).to.have.lengthOf(0);

wrapper.instance().componentDidMount();
expect(wrapper.find(Table).length).to.equal(1);
// wrapper.update(); // TODO: uncomment or delete

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ljharb was able to remove the wrapper.update() by returning true in the adapter function.

Copy link
Member

Choose a reason for hiding this comment

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

This still seems to be failing.

expect(wrapper.find(Table)).to.have.lengthOf(1);
});

it('calls shouldComponentUpdate when disableLifecycleMethods flag is true', () => {
Expand Down
44 changes: 44 additions & 0 deletions packages/enzyme-test-suite/test/shared/methods/find.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,50 @@ export default function describeFind({
expect(wrapper.find('.b').find('.c')).to.have.lengthOf(6);
});

it('can call find on the same wrapper more than once', () => {
class TestComponent extends React.Component {
render() {
return (
<div>
<h1>Title</h1>
<span key="1">1</span>
<span key="2">2</span>
</div>
);
}
}
const component = Wrap(<TestComponent />);

const cards = component.find('span');

const title = component.find('h1'); // for side effects
expect(title.is('h1')).to.equal(true);

expect(cards.at(0).parent().is('div')).to.equal(true);
});

describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
it('can call find on the same wrapper more than once', () => {
function TestComponentSFC() {
return (
<div>
<h1>Title</h1>
<span key="1">1</span>
<span key="2">2</span>
</div>
);
}
const component = Wrap(<TestComponentSFC />);

const cards = component.find('span');

const title = component.find('h1'); // for side effects
expect(title.is('h1')).to.equal(true);

expect(cards.at(0).parent().debug()).to.equal('<div />');
});
});

it('works with an adjacent sibling selector', () => {
const a = 'some';
const b = 'text';
Expand Down
9 changes: 8 additions & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,13 @@ class ShallowWrapper {
*/
getNodesInternal() {
if (this[ROOT] === this && this.length === 1) {
this.update();
const adapter = getAdapter(this[OPTIONS]);
const prevProps = (this[UNRENDERED] && this[UNRENDERED].props) || {};
if (!adapter.shouldComponentUpdate || adapter.shouldComponentUpdate(prevProps, this[ROOT])) {
this.update();
}
}

return this[NODES];
}

Expand Down Expand Up @@ -569,8 +574,10 @@ class ShallowWrapper {
*/
unmount() {
this[RENDERER].unmount();
this.update();
if (this[ROOT][WRAPPING_COMPONENT]) {
this[ROOT][WRAPPING_COMPONENT].unmount();
this[ROOT][WRAPPING_COMPONENT].update();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Curious why these were added?

Copy link
Member

Choose a reason for hiding this comment

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

to make tests pass; they were failing.

}
return this;
}
Expand Down