diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 946932d6f..f5e1b94c9 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -2056,9 +2056,12 @@ describe('shallow', () => { } } const wrapper = shallow(, { 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', () => { diff --git a/packages/enzyme-test-suite/test/shared/methods/find.jsx b/packages/enzyme-test-suite/test/shared/methods/find.jsx index 429fdf7ca..8fc66f8fd 100644 --- a/packages/enzyme-test-suite/test/shared/methods/find.jsx +++ b/packages/enzyme-test-suite/test/shared/methods/find.jsx @@ -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 ( +
+

Title

+ 1 + 2 +
+ ); + } + } + const component = Wrap(); + + 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 ( +
+

Title

+ 1 + 2 +
+ ); + } + const component = Wrap(); + + 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('
'); + }); + }); + it('works with an adjacent sibling selector', () => { const a = 'some'; const b = 'text'; diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index 7bb437223..d32dccc1a 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -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]; } @@ -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; }