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
Show file tree
Hide file tree
Changes from all commits
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
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
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