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
…dComponentUpdate` method before updating the wrapper.

Fixes #1916.
  • Loading branch information
sstern6 authored and ljharb committed Jan 15, 2019
1 parent 28766ef commit 9ab718c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
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

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();
}
return this;
}
Expand Down

0 comments on commit 9ab718c

Please sign in to comment.