Skip to content

Commit

Permalink
[Fix] selectors: make general sibling not throw on root
Browse files Browse the repository at this point in the history
  • Loading branch information
krawaller authored and ljharb committed Jun 29, 2018
1 parent 01f1af9 commit 0d446f4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/enzyme-test-suite/test/selector-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ describe('selectors', () => {
siblings.map(sibling => expect(sibling.text()).to.not.equal('Top'));
});

it('handles using general siblings on root', () => {
const wrapper = renderMethod(<div className="foo" />);
expect(wrapper.find('.foo ~ .bar')).to.have.lengthOf(0);
});

it('not() pseudo selector', () => {
const wrapper = renderMethod((
<div>
Expand Down
3 changes: 3 additions & 0 deletions packages/enzyme/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ function matchAdjacentSiblings(nodes, predicate, root) {
function matchGeneralSibling(nodes, predicate, root) {
return uniqueReduce((matches, node) => {
const parent = findParentNode(root, node);
if (!parent) {
return matches;
}
const nodeIndex = parent.rendered.indexOf(node);
const youngerSiblings = parent.rendered.slice(nodeIndex + 1);
return matches.concat(youngerSiblings.filter(predicate));
Expand Down

0 comments on commit 0d446f4

Please sign in to comment.