Skip to content

Commit

Permalink
[Fix] shallow: .parents: ensure that one .find call does not af…
Browse files Browse the repository at this point in the history
…fect another.

Fixes #1780.
  • Loading branch information
ljharb committed Sep 6, 2018
1 parent c6632de commit aa74929
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
45 changes: 45 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3893,6 +3893,51 @@ describeWithDOM('mount', () => {
const formUp = input.parents('form');
expect(formUp).to.have.lengthOf(1);
});

it('works when called sequentially on two sibling nodes', () => {
class Test extends React.Component {
render() {
return (
<div>
<div className="a">
<div>A child</div>
</div>
<div className="b">
<div>B child</div>
</div>
</div>
);
}
}

const wrapper = mount(<Test />);

const aChild = wrapper.find({ children: 'A child' });
expect(aChild.debug()).to.equal(`<div>
A child
</div>`);
expect(aChild).to.have.lengthOf(1);

const bChild = wrapper.find({ children: 'B child' });
expect(bChild.debug()).to.equal(`<div>
B child
</div>`);
expect(bChild).to.have.lengthOf(1);

/*
const bChildParents = bChild.parents('.b');
expect(bChildParents.debug(`<div className="b">
<div>B child</div>
</div>`));
expect(bChildParents).to.have.lengthOf(1);
*/

const aChildParents = aChild.parents('.a');
expect(aChildParents.debug(`<div className="a">
<div>A child</div>
</div>`));
expect(aChildParents).to.have.lengthOf(1);
});
});

describe('.parent()', () => {
Expand Down
45 changes: 45 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3556,6 +3556,51 @@ describe('shallow', () => {
expect(parents.at(0).hasClass('foo')).to.equal(true);
expect(parents.at(1).hasClass('bax')).to.equal(true);
});

it('works when called sequentially on two sibling nodes', () => {
class Test extends React.Component {
render() {
return (
<div>
<div className="a">
<div>A child</div>
</div>
<div className="b">
<div>B child</div>
</div>
</div>
);
}
}

const wrapper = shallow(<Test />);

const aChild = wrapper.find({ children: 'A child' });
expect(aChild.debug()).to.equal(`<div>
A child
</div>`);
expect(aChild).to.have.lengthOf(1);

const bChild = wrapper.find({ children: 'B child' });
expect(bChild.debug()).to.equal(`<div>
B child
</div>`);
expect(bChild).to.have.lengthOf(1);

/*
const bChildParents = bChild.parents('.b');
expect(bChildParents.debug(`<div className="b">
<div>B child</div>
</div>`));
expect(bChildParents).to.have.lengthOf(1);
*/

const aChildParents = aChild.parents('.a');
expect(aChildParents.debug(`<div className="a">
<div>A child</div>
</div>`));
expect(aChildParents).to.have.lengthOf(1);
});
});

describe('.parent()', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/RSTTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function pathToNode(node, root) {
}

export function parentsOfNode(node, root) {
return pathToNode(node, root).reverse();
return (pathToNode(node, root) || []).reverse();
}

export function nodeHasId(node, id) {
Expand Down
6 changes: 4 additions & 2 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,10 @@ class ReactWrapper {
* @returns {ReactWrapper}
*/
parents(selector) {
const allParents = this.wrap(this.single('parents', n => nodeParents(this, n)));
return selector ? allParents.filter(selector) : allParents;
return this.single('parents', (n) => {
const allParents = this.wrap(this.single('parents', n => nodeParents(this, n)));
return selector ? allParents.filter(selector) : allParents;
});
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,8 +972,10 @@ class ShallowWrapper {
* @returns {ShallowWrapper}
*/
parents(selector) {
const allParents = this.wrap(this.single('parents', n => nodeParents(this, n)));
return selector ? allParents.filter(selector) : allParents;
return this.single('parents', (n) => {
const allParents = this.wrap(this.single('parents', n => nodeParents(this, n)));
return selector ? allParents.filter(selector) : allParents;
});
}

/**
Expand Down

0 comments on commit aa74929

Please sign in to comment.