Skip to content
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
42 changes: 39 additions & 3 deletions packages/enzyme-test-suite/test/selector-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('selectors', () => {

expect(wrapper.find('span')).to.have.lengthOf(2);
expect(wrapper.find('.top-div span')).to.have.lengthOf(1);
expect(wrapper.find('div div')).to.have.lengthOf(2);
});

it('nested descendent', () => {
Expand All @@ -68,7 +69,7 @@ describe('selectors', () => {
});

it('deep descendent', () => {
const wrapper = renderMethod((
const htmlWrapper = renderMethod((
<div>
<div>
<div className="inner">
Expand All @@ -83,8 +84,43 @@ describe('selectors', () => {
</div>
));

expect(wrapper.find('h1')).to.have.lengthOf(2);
expect(wrapper.find('div .inner span .way-inner h1')).to.have.lengthOf(1);
expect(htmlWrapper.find('h1')).to.have.lengthOf(2);
expect(htmlWrapper.find('div .inner span .way-inner h1')).to.have.lengthOf(1);
expect(htmlWrapper.find('div div div div')).to.have.lengthOf(1);
expect(htmlWrapper.find('div div div')).to.have.lengthOf(2);
expect(htmlWrapper.find('div span div')).to.have.lengthOf(1);

class ExampleComponent extends React.Component {
render() {
return <span>Hello world</span>;
}
}

const complexWrapper = renderMethod((
<div>
<div>
<ExampleComponent>
<main />
</ExampleComponent>
</div>
<ExampleComponent>
<nav />
<main />
</ExampleComponent>
</div>
));

expect(complexWrapper.find('div ExampleComponent')).to.have.lengthOf(2);
expect(complexWrapper.find('div div ExampleComponent')).to.have.lengthOf(1);
if (name === 'shallow') {
expect(complexWrapper.find('div ExampleComponent nav')).to.have.lengthOf(1);
expect(complexWrapper.find('div ExampleComponent main')).to.have.lengthOf(2);
} else { // shallow does not render the contents of components
expect(complexWrapper.find('div ExampleComponent span')).to.have.lengthOf(2);
expect(complexWrapper.find('div div ExampleComponent span')).to.have.lengthOf(1);
expect(complexWrapper.find('div span')).to.have.lengthOf(2);
expect(complexWrapper.find('div div span')).to.have.lengthOf(1);
}
});

it('direct descendent', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ function matchDirectChild(nodes, predicate) {
function matchDescendant(nodes, predicate) {
return uniqueReduce(
(matches, node) => matches.concat(treeFilter(node, predicate)),
nodes,
flat(nodes.map(childrenOfNode)),
);
}

Expand Down