Skip to content

Commit

Permalink
[fix] shallow: getNodesInternal: delegate to the adapter’s `shoul…
Browse files Browse the repository at this point in the history
…dUpdateComponent` method before updating the wrapper.

Fixes #1916.
  • Loading branch information
sstern6 authored and ljharb committed Jan 15, 2019
1 parent 6e7abd0 commit 531e6fb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -1785,6 +1785,7 @@ describe('shallow', () => {
const wrapper = shallow(<MyComponent />, { disableLifecycleMethods: true });
expect(wrapper.find(Table).length).to.equal(0);
wrapper.instance().componentDidMount();
wrapper.update();
expect(wrapper.find(Table).length).to.equal(1);
});

Expand Down
44 changes: 44 additions & 0 deletions packages/enzyme-test-suite/test/shared/methods/find.jsx
Expand Up @@ -293,6 +293,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().is('div')).to.equal(true);
});
});

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
Expand Up @@ -460,8 +460,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.shouldUpdateComponent || adapter.shouldUpdateComponent(prevProps, this[ROOT])) {
this.update();
}
}

return this[NODES];
}

Expand Down Expand Up @@ -556,8 +561,10 @@ class ShallowWrapper {
*/
unmount() {
this[RENDERER].unmount();
this.update();
if (this[ROOT][WRAPPING_COMPONENT]) {
this[ROOT][WRAPPING_COMPONENT].unmount();
this[ROOT][WRAPPING_COMPONENT].update();
}
return this;
}
Expand Down

0 comments on commit 531e6fb

Please sign in to comment.