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 enzymejs#1916.
  • Loading branch information
sstern6 authored and ljharb committed Jan 15, 2019
1 parent e7106c4 commit 4507396
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 @@ -1783,9 +1783,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 @@ -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().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 @@ -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 4507396

Please sign in to comment.