Skip to content

Commit

Permalink
[Fix] mount: findWhere: avoid passing empty wrappers to predicate
Browse files Browse the repository at this point in the history
Fixes #1993.
  • Loading branch information
ljharb committed Feb 1, 2019
1 parent a7c541b commit 9717f61
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,21 @@ describeWithDOM('mount', () => {
expect(wrapper.findWhere(() => false)).to.have.lengthOf(0);
});

it('does not pass empty wrappers', () => {
class EditableText extends React.Component {
render() {
return <div>{''}</div>;
}
}

const wrapper = mount(<EditableText />);

const stub = sinon.stub();
wrapper.findWhere(stub);
const passedNodeLengths = stub.getCalls().map(({ args: [firstArg] }) => firstArg.length);
expect(passedNodeLengths).to.eql([1, 1]);
});

it('calls the predicate with the wrapped node as the first argument', () => {
const wrapper = mount((
<div>
Expand Down
15 changes: 15 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,21 @@ describe('shallow', () => {
expect(wrapper.findWhere(() => false)).to.have.lengthOf(0);
});

it('does not pass empty wrappers', () => {
class EditableText extends React.Component {
render() {
return <div>{''}</div>;
}
}

const wrapper = shallow(<EditableText />);

const stub = sinon.stub();
wrapper.findWhere(stub);
const passedNodeLengths = stub.getCalls().map(({ args: [firstArg] }) => firstArg.length);
expect(passedNodeLengths).to.eql([1, 1]);
});

it('calls the predicate with the wrapped node as the first argument', () => {
const wrapper = shallow((
<div>
Expand Down
5 changes: 4 additions & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,10 @@ class ReactWrapper {
* @returns {ReactWrapper}
*/
findWhere(predicate) {
return findWhereUnwrapped(this, n => predicate(this.wrap(n)));
return findWhereUnwrapped(this, (n) => {
const node = this.wrap(n);
return node.length > 0 && predicate(node);
});
}

/**
Expand Down
5 changes: 4 additions & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,10 @@ class ShallowWrapper {
* @returns {ShallowWrapper}
*/
findWhere(predicate) {
return findWhereUnwrapped(this, n => predicate(this.wrap(n)));
return findWhereUnwrapped(this, (n) => {
const node = this.wrap(n);
return node.length > 0 && predicate(node);
});
}

/**
Expand Down

0 comments on commit 9717f61

Please sign in to comment.