Skip to content

Commit afc82f7

Browse files
sstern6ljharb
authored andcommitted
[fix] shallow: getNodesInternal: delegate to the adapter’s shouldUpdateComponent method before updating the wrapper.
Fixes enzymejs#1916.
1 parent 10483c3 commit afc82f7

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,9 +2056,12 @@ describe('shallow', () => {
20562056
}
20572057
}
20582058
const wrapper = shallow(<MyComponent />, { disableLifecycleMethods: true });
2059-
expect(wrapper.find(Table).length).to.equal(0);
2059+
expect(wrapper.find(Table)).to.have.lengthOf(0);
2060+
20602061
wrapper.instance().componentDidMount();
2061-
expect(wrapper.find(Table).length).to.equal(1);
2062+
// wrapper.update(); // TODO: uncomment or delete
2063+
2064+
expect(wrapper.find(Table)).to.have.lengthOf(1);
20622065
});
20632066

20642067
it('calls shouldComponentUpdate when disableLifecycleMethods flag is true', () => {

packages/enzyme-test-suite/test/shared/methods/find.jsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,50 @@ export default function describeFind({
293293
expect(wrapper.find('.b').find('.c')).to.have.lengthOf(6);
294294
});
295295

296+
it('can call find on the same wrapper more than once', () => {
297+
class TestComponent extends React.Component {
298+
render() {
299+
return (
300+
<div>
301+
<h1>Title</h1>
302+
<span key="1">1</span>
303+
<span key="2">2</span>
304+
</div>
305+
);
306+
}
307+
}
308+
const component = Wrap(<TestComponent />);
309+
310+
const cards = component.find('span');
311+
312+
const title = component.find('h1'); // for side effects
313+
expect(title.is('h1')).to.equal(true);
314+
315+
expect(cards.at(0).parent().is('div')).to.equal(true);
316+
});
317+
318+
describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
319+
it('can call find on the same wrapper more than once', () => {
320+
function TestComponentSFC() {
321+
return (
322+
<div>
323+
<h1>Title</h1>
324+
<span key="1">1</span>
325+
<span key="2">2</span>
326+
</div>
327+
);
328+
}
329+
const component = Wrap(<TestComponentSFC />);
330+
331+
const cards = component.find('span');
332+
333+
const title = component.find('h1'); // for side effects
334+
expect(title.is('h1')).to.equal(true);
335+
336+
expect(cards.at(0).parent().debug()).to.equal('<div />');
337+
});
338+
});
339+
296340
it('works with an adjacent sibling selector', () => {
297341
const a = 'some';
298342
const b = 'text';

packages/enzyme/src/ShallowWrapper.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,13 @@ class ShallowWrapper {
460460
*/
461461
getNodesInternal() {
462462
if (this[ROOT] === this && this.length === 1) {
463-
this.update();
463+
const adapter = getAdapter(this[OPTIONS]);
464+
const prevProps = (this[UNRENDERED] && this[UNRENDERED].props) || {};
465+
if (!adapter.shouldUpdateComponent || adapter.shouldUpdateComponent(prevProps, this[ROOT])) {
466+
this.update();
467+
}
464468
}
469+
465470
return this[NODES];
466471
}
467472

@@ -556,8 +561,10 @@ class ShallowWrapper {
556561
*/
557562
unmount() {
558563
this[RENDERER].unmount();
564+
this.update();
559565
if (this[ROOT][WRAPPING_COMPONENT]) {
560566
this[ROOT][WRAPPING_COMPONENT].unmount();
567+
this[ROOT][WRAPPING_COMPONENT].update();
561568
}
562569
return this;
563570
}

0 commit comments

Comments
 (0)