Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for using general sibling selector on root #1698

Merged
merged 2 commits into from
Jun 30, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Refactor] selectors: clean up matchDirectChild, `matchGeneralSib…
…ling`, `reduceTreeBySelector`
  • Loading branch information
krawaller authored and ljharb committed Jun 29, 2018
commit 01f1af9224ab2238e29fcbec7fe5be677386b74d
30 changes: 11 additions & 19 deletions packages/enzyme/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,8 @@ function matchGeneralSibling(nodes, predicate, root) {
return uniqueReduce((matches, node) => {
const parent = findParentNode(root, node);
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 +316,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 +344,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