Skip to content

Commit 9ab718c

Browse files
sstern6ljharb
authored andcommitted
[fix] shallow: getNodesInternal: delegate to the adapter’s shouldComponentUpdate method before updating the wrapper.
Fixes enzymejs#1916.
1 parent 28766ef commit 9ab718c

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -2237,9 +2237,12 @@ describe('shallow', () => {
22372237
}
22382238
}
22392239
const wrapper = shallow(<MyComponent />, { disableLifecycleMethods: true });
2240-
expect(wrapper.find(Table).length).to.equal(0);
2240+
expect(wrapper.find(Table)).to.have.lengthOf(0);
2241+
22412242
wrapper.instance().componentDidMount();
2242-
expect(wrapper.find(Table).length).to.equal(1);
2243+
// wrapper.update(); // TODO: uncomment or delete
2244+
2245+
expect(wrapper.find(Table)).to.have.lengthOf(1);
22432246
});
22442247

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

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

+44
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,50 @@ export default function describeFind({
308308
expect(wrapper.find('.b').find('.c')).to.have.lengthOf(6);
309309
});
310310

311+
it('can call find on the same wrapper more than once', () => {
312+
class TestComponent extends React.Component {
313+
render() {
314+
return (
315+
<div>
316+
<h1>Title</h1>
317+
<span key="1">1</span>
318+
<span key="2">2</span>
319+
</div>
320+
);
321+
}
322+
}
323+
const component = Wrap(<TestComponent />);
324+
325+
const cards = component.find('span');
326+
327+
const title = component.find('h1'); // for side effects
328+
expect(title.is('h1')).to.equal(true);
329+
330+
expect(cards.at(0).parent().is('div')).to.equal(true);
331+
});
332+
333+
describeIf(is('> 0.13'), 'stateless function components (SFCs)', () => {
334+
it('can call find on the same wrapper more than once', () => {
335+
function TestComponentSFC() {
336+
return (
337+
<div>
338+
<h1>Title</h1>
339+
<span key="1">1</span>
340+
<span key="2">2</span>
341+
</div>
342+
);
343+
}
344+
const component = Wrap(<TestComponentSFC />);
345+
346+
const cards = component.find('span');
347+
348+
const title = component.find('h1'); // for side effects
349+
expect(title.is('h1')).to.equal(true);
350+
351+
expect(cards.at(0).parent().debug()).to.equal('<div />');
352+
});
353+
});
354+
311355
it('works with an adjacent sibling selector', () => {
312356
const a = 'some';
313357
const b = 'text';

packages/enzyme/src/ShallowWrapper.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,13 @@ class ShallowWrapper {
473473
*/
474474
getNodesInternal() {
475475
if (this[ROOT] === this && this.length === 1) {
476-
this.update();
476+
const adapter = getAdapter(this[OPTIONS]);
477+
const prevProps = (this[UNRENDERED] && this[UNRENDERED].props) || {};
478+
if (!adapter.shouldComponentUpdate || adapter.shouldComponentUpdate(prevProps, this[ROOT])) {
479+
this.update();
480+
}
477481
}
482+
478483
return this[NODES];
479484
}
480485

@@ -569,8 +574,10 @@ class ShallowWrapper {
569574
*/
570575
unmount() {
571576
this[RENDERER].unmount();
577+
this.update();
572578
if (this[ROOT][WRAPPING_COMPONENT]) {
573579
this[ROOT][WRAPPING_COMPONENT].unmount();
580+
this[ROOT][WRAPPING_COMPONENT].update();
574581
}
575582
return this;
576583
}

0 commit comments

Comments
 (0)