Skip to content

Commit

Permalink
Merge branch 'master' into fix-child-selector
Browse files Browse the repository at this point in the history
  • Loading branch information
ReactiveRaven authored Jun 30, 2018
2 parents 6c9dbda + 0d446f4 commit 428f6f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 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 @@ -194,6 +194,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
33 changes: 14 additions & 19 deletions packages/enzyme/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,12 @@ 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);
parent.rendered.forEach((sibling, i) => {
if (i > nodeIndex && predicate(sibling)) {
matches.push(sibling);
}
});
return matches;
const youngerSiblings = parent.rendered.slice(nodeIndex + 1);
return matches.concat(youngerSiblings.filter(predicate));
}, nodes);
}

Expand All @@ -320,15 +319,10 @@ function matchGeneralSibling(nodes, predicate, root) {
* @param {Function} predicate
*/
function matchDirectChild(nodes, predicate) {
return uniqueReduce((matches, node) => {
const children = childrenOfNode(node);
children.forEach((child) => {
if (predicate(child)) {
matches.push(child);
}
});
return matches;
}, nodes);
return uniqueReduce(
(matches, node) => matches.concat(childrenOfNode(node).filter(predicate)),
nodes,
);
}

/**
Expand All @@ -353,11 +347,12 @@ function matchDescendant(nodes, predicate) {
* @param {RSTNode} wrapper
*/
export function reduceTreeBySelector(selector, root) {
let results = [];

if (typeof selector === 'function' || typeof selector === 'object') {
results = treeFilter(root, buildPredicate(selector));
} else if (typeof selector === 'string') {
return treeFilter(root, buildPredicate(selector));
}

let results = [];
if (typeof selector === 'string') {
const tokens = safelyGenerateTokens(selector);
let index = 0;
let token = null;
Expand Down

0 comments on commit 428f6f5

Please sign in to comment.