Skip to content

Commit c294dc0

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

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
@@ -3529,6 +3529,51 @@ describeWithDOM('mount', () => {
35293529
const formUp = input.parents('form');
35303530
expect(formUp).to.have.lengthOf(1);
35313531
});
3532+
3533+
it('works when called sequentially on two sibling nodes', () => {
3534+
class Test extends React.Component {
3535+
render() {
3536+
return (
3537+
<div>
3538+
<div className="a">
3539+
<div>A child</div>
3540+
</div>
3541+
<div className="b">
3542+
<div>B child</div>
3543+
</div>
3544+
</div>
3545+
);
3546+
}
3547+
}
3548+
3549+
const wrapper = mount(<Test />);
3550+
3551+
const aChild = wrapper.find({ children: 'A child' });
3552+
expect(aChild.debug()).to.equal(`<div>
3553+
A child
3554+
</div>`);
3555+
expect(aChild).to.have.lengthOf(1);
3556+
3557+
const bChild = wrapper.find({ children: 'B child' });
3558+
expect(bChild.debug()).to.equal(`<div>
3559+
B child
3560+
</div>`);
3561+
expect(bChild).to.have.lengthOf(1);
3562+
3563+
/*
3564+
const bChildParents = bChild.parents('.b');
3565+
expect(bChildParents.debug(`<div className="b">
3566+
<div>B child</div>
3567+
</div>`));
3568+
expect(bChildParents).to.have.lengthOf(1);
3569+
*/
3570+
3571+
const aChildParents = aChild.parents('.b');
3572+
expect(aChildParents.debug(`<div className="a">
3573+
<div>A child</div>
3574+
</div>`));
3575+
expect(aChildParents).to.have.lengthOf(1);
3576+
});
35323577
});
35333578

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

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,6 +3444,51 @@ describe('shallow', () => {
34443444
expect(parents.at(0).hasClass('foo')).to.equal(true);
34453445
expect(parents.at(1).hasClass('bax')).to.equal(true);
34463446
});
3447+
3448+
it('works when called sequentially on two sibling nodes', () => {
3449+
class Test extends React.Component {
3450+
render() {
3451+
return (
3452+
<div>
3453+
<div className="a">
3454+
<div>A child</div>
3455+
</div>
3456+
<div className="b">
3457+
<div>B child</div>
3458+
</div>
3459+
</div>
3460+
);
3461+
}
3462+
}
3463+
3464+
const wrapper = shallow(<Test />);
3465+
3466+
const aChild = wrapper.find({ children: 'A child' });
3467+
expect(aChild.debug()).to.equal(`<div>
3468+
A child
3469+
</div>`);
3470+
expect(aChild).to.have.lengthOf(1);
3471+
3472+
const bChild = wrapper.find({ children: 'B child' });
3473+
expect(bChild.debug()).to.equal(`<div>
3474+
B child
3475+
</div>`);
3476+
expect(bChild).to.have.lengthOf(1);
3477+
3478+
/*
3479+
const bChildParents = bChild.parents('.b');
3480+
expect(bChildParents.debug(`<div className="b">
3481+
<div>B child</div>
3482+
</div>`));
3483+
expect(bChildParents).to.have.lengthOf(1);
3484+
*/
3485+
3486+
const aChildParents = aChild.parents('.a');
3487+
expect(aChildParents.debug(`<div className="a">
3488+
<div>A child</div>
3489+
</div>`));
3490+
expect(aChildParents).to.have.lengthOf(1);
3491+
});
34473492
});
34483493

34493494
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
@@ -702,8 +702,10 @@ class ReactWrapper {
702702
* @returns {ReactWrapper}
703703
*/
704704
parents(selector) {
705-
const allParents = this.wrap(this.single('parents', n => parentsOfNode(n, this[ROOT].getNodeInternal())));
706-
return selector ? allParents.filter(selector) : allParents;
705+
return this.single('parents', (n) => {
706+
const allParents = this.wrap(parentsOfNode(n, this[ROOT].getNodeInternal()));
707+
return selector ? allParents.filter(selector) : allParents;
708+
});
707709
}
708710

709711
/**

packages/enzyme/src/ShallowWrapper.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,10 @@ class ShallowWrapper {
931931
* @returns {ShallowWrapper}
932932
*/
933933
parents(selector) {
934-
const allParents = this.wrap(this.single('parents', n => parentsOfNode(n, getRootNodeInternal(this))));
935-
return selector ? allParents.filter(selector) : allParents;
934+
return this.single('parents', (n) => {
935+
const allParents = this.wrap(parentsOfNode(n, getRootNodeInternal(this)));
936+
return selector ? allParents.filter(selector) : allParents;
937+
});
936938
}
937939

938940
/**

0 commit comments

Comments
 (0)