Skip to content

Commit aa74929

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

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,6 +3893,51 @@ 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(`<div className="b">
3930+
<div>B child</div>
3931+
</div>`));
3932+
expect(bChildParents).to.have.lengthOf(1);
3933+
*/
3934+
3935+
const aChildParents = aChild.parents('.a');
3936+
expect(aChildParents.debug(`<div className="a">
3937+
<div>A child</div>
3938+
</div>`));
3939+
expect(aChildParents).to.have.lengthOf(1);
3940+
});
38963941
});
38973942

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

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,6 +3556,51 @@ 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(`<div className="b">
3593+
<div>B child</div>
3594+
</div>`));
3595+
expect(bChildParents).to.have.lengthOf(1);
3596+
*/
3597+
3598+
const aChildParents = aChild.parents('.a');
3599+
expect(aChildParents.debug(`<div className="a">
3600+
<div>A child</div>
3601+
</div>`));
3602+
expect(aChildParents).to.have.lengthOf(1);
3603+
});
35593604
});
35603605

35613606
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
@@ -736,8 +736,10 @@ class ReactWrapper {
736736
* @returns {ReactWrapper}
737737
*/
738738
parents(selector) {
739-
const allParents = this.wrap(this.single('parents', n => nodeParents(this, n)));
740-
return selector ? allParents.filter(selector) : allParents;
739+
return this.single('parents', (n) => {
740+
const allParents = this.wrap(this.single('parents', n => nodeParents(this, n)));
741+
return selector ? allParents.filter(selector) : allParents;
742+
});
741743
}
742744

743745
/**

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(this.single('parents', n => nodeParents(this, n)));
977+
return selector ? allParents.filter(selector) : allParents;
978+
});
977979
}
978980

979981
/**

0 commit comments

Comments
 (0)