Skip to content

Commit 4dd2780

Browse files
committed
[Fix] shallow: .parents: ensure that one .find call does not affect another.
Fixes #1780.
1 parent f281fef commit 4dd2780

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,6 +3893,55 @@ describeWithDOM('mount', () => {
38933893
const formUp = input.parents('form');
38943894
expect(formUp).to.have.lengthOf(1);
38953895
});
3896+
3897+
it('works when called sequentially on two sibling nodes', () => {
3898+
class Test extends React.Component {
3899+
render() {
3900+
return (
3901+
<div>
3902+
<div className="a">
3903+
<div>A child</div>
3904+
</div>
3905+
<div className="b">
3906+
<div>B child</div>
3907+
</div>
3908+
</div>
3909+
);
3910+
}
3911+
}
3912+
3913+
const wrapper = mount(<Test />);
3914+
3915+
const aChild = wrapper.find({ children: 'A child' });
3916+
expect(aChild.debug()).to.equal(`<div>
3917+
A child
3918+
</div>`);
3919+
expect(aChild).to.have.lengthOf(1);
3920+
3921+
const bChild = wrapper.find({ children: 'B child' });
3922+
expect(bChild.debug()).to.equal(`<div>
3923+
B child
3924+
</div>`);
3925+
expect(bChild).to.have.lengthOf(1);
3926+
3927+
/*
3928+
const bChildParents = bChild.parents('.b');
3929+
expect(bChildParents.debug()).to.equal(`<div className="b">
3930+
<div>
3931+
B child
3932+
</div>
3933+
</div>`);
3934+
expect(bChildParents).to.have.lengthOf(1);
3935+
*/
3936+
3937+
const aChildParents = aChild.parents('.a');
3938+
expect(aChildParents.debug()).to.equal(`<div className="a">
3939+
<div>
3940+
A child
3941+
</div>
3942+
</div>`);
3943+
expect(aChildParents).to.have.lengthOf(1);
3944+
});
38963945
});
38973946

38983947
describe('.parent()', () => {

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,6 +3556,55 @@ describe('shallow', () => {
35563556
expect(parents.at(0).hasClass('foo')).to.equal(true);
35573557
expect(parents.at(1).hasClass('bax')).to.equal(true);
35583558
});
3559+
3560+
it('works when called sequentially on two sibling nodes', () => {
3561+
class Test extends React.Component {
3562+
render() {
3563+
return (
3564+
<div>
3565+
<div className="a">
3566+
<div>A child</div>
3567+
</div>
3568+
<div className="b">
3569+
<div>B child</div>
3570+
</div>
3571+
</div>
3572+
);
3573+
}
3574+
}
3575+
3576+
const wrapper = shallow(<Test />);
3577+
3578+
const aChild = wrapper.find({ children: 'A child' });
3579+
expect(aChild.debug()).to.equal(`<div>
3580+
A child
3581+
</div>`);
3582+
expect(aChild).to.have.lengthOf(1);
3583+
3584+
const bChild = wrapper.find({ children: 'B child' });
3585+
expect(bChild.debug()).to.equal(`<div>
3586+
B child
3587+
</div>`);
3588+
expect(bChild).to.have.lengthOf(1);
3589+
3590+
/*
3591+
const bChildParents = bChild.parents('.b');
3592+
expect(bChildParents.debug()).to.equal(`<div className="b">
3593+
<div>
3594+
B child
3595+
</div>
3596+
</div>`);
3597+
expect(bChildParents).to.have.lengthOf(1);
3598+
*/
3599+
3600+
const aChildParents = aChild.parents('.a');
3601+
expect(aChildParents.debug()).to.equal(`<div className="a">
3602+
<div>
3603+
A child
3604+
</div>
3605+
</div>`);
3606+
expect(aChildParents).to.have.lengthOf(1);
3607+
});
35593608
});
35603609

35613610
describe('.parent()', () => {

packages/enzyme/src/RSTTraversal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function pathToNode(node, root) {
106106
}
107107

108108
export function parentsOfNode(node, root) {
109-
return pathToNode(node, root).reverse();
109+
return (pathToNode(node, root) || []).reverse();
110110
}
111111

112112
export function nodeHasId(node, id) {

packages/enzyme/src/ReactWrapper.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,10 @@ class ReactWrapper {
737737
* @returns {ReactWrapper}
738738
*/
739739
parents(selector) {
740-
const allParents = this.wrap(this.single('parents', n => nodeParents(this, n)));
741-
return selector ? allParents.filter(selector) : allParents;
740+
return this.single('parents', (n) => {
741+
const allParents = this.wrap(nodeParents(this, n));
742+
return selector ? allParents.filter(selector) : allParents;
743+
});
742744
}
743745

744746
/**

packages/enzyme/src/ShallowWrapper.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,10 @@ class ShallowWrapper {
972972
* @returns {ShallowWrapper}
973973
*/
974974
parents(selector) {
975-
const allParents = this.wrap(this.single('parents', n => nodeParents(this, n)));
976-
return selector ? allParents.filter(selector) : allParents;
975+
return this.single('parents', (n) => {
976+
const allParents = this.wrap(nodeParents(this, n));
977+
return selector ? allParents.filter(selector) : allParents;
978+
});
977979
}
978980

979981
/**

0 commit comments

Comments
 (0)