Skip to content

Commit 6844716

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

5 files changed

Lines changed: 95 additions & 5 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,6 +3450,49 @@ describeWithDOM('mount', () => {
34503450
expect(bar).to.have.lengthOf(1);
34513451
expect(bar.parents('.root')).to.have.lengthOf(1);
34523452
});
3453+
3454+
it('works when called sequentially on two sibling nodes', () => {
3455+
class Test extends React.Component {
3456+
render() {
3457+
return (
3458+
<div>
3459+
<div className="a">
3460+
<div>A child</div>
3461+
</div>
3462+
<div className="b">
3463+
<div>B child</div>
3464+
</div>
3465+
</div>
3466+
);
3467+
}
3468+
}
3469+
3470+
const wrapper = mount(<Test />);
3471+
3472+
const aChild = wrapper.find({ children: 'A child' });
3473+
expect(aChild.debug()).to.equal(`<div>
3474+
A child
3475+
</div>`);
3476+
expect(aChild).to.have.lengthOf(1);
3477+
3478+
const bChild = wrapper.find({ children: 'B child' });
3479+
expect(bChild.debug()).to.equal(`<div>
3480+
B child
3481+
</div>`);
3482+
expect(bChild).to.have.lengthOf(1);
3483+
3484+
// const bChildParents = bChild.parents('.b');
3485+
// expect(bChildParents.debug(`<div className="b">
3486+
// <div>B child</div>
3487+
// </div>`));
3488+
// expect(bChildParents).to.have.lengthOf(1);
3489+
3490+
const aChildParents = aChild.parents('.b');
3491+
expect(aChildParents.debug(`<div className="a">
3492+
<div>A child</div>
3493+
</div>`));
3494+
expect(aChildParents).to.have.lengthOf(1);
3495+
});
34533496
});
34543497

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

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,6 +3394,49 @@ describe('shallow', () => {
33943394
expect(parents.at(0).hasClass('foo')).to.equal(true);
33953395
expect(parents.at(1).hasClass('bax')).to.equal(true);
33963396
});
3397+
3398+
it('works when called sequentially on two sibling nodes', () => {
3399+
class Test extends React.Component {
3400+
render() {
3401+
return (
3402+
<div>
3403+
<div className="a">
3404+
<div>A child</div>
3405+
</div>
3406+
<div className="b">
3407+
<div>B child</div>
3408+
</div>
3409+
</div>
3410+
);
3411+
}
3412+
}
3413+
3414+
const wrapper = shallow(<Test />);
3415+
3416+
const aChild = wrapper.find({ children: 'A child' });
3417+
expect(aChild.debug()).to.equal(`<div>
3418+
A child
3419+
</div>`);
3420+
expect(aChild).to.have.lengthOf(1);
3421+
3422+
const bChild = wrapper.find({ children: 'B child' });
3423+
expect(bChild.debug()).to.equal(`<div>
3424+
B child
3425+
</div>`);
3426+
expect(bChild).to.have.lengthOf(1);
3427+
3428+
// const bChildParents = bChild.parents('.b');
3429+
// expect(bChildParents.debug(`<div className="b">
3430+
// <div>B child</div>
3431+
// </div>`));
3432+
// expect(bChildParents).to.have.lengthOf(1);
3433+
3434+
const aChildParents = aChild.parents('.a');
3435+
expect(aChildParents.debug(`<div className="a">
3436+
<div>A child</div>
3437+
</div>`));
3438+
expect(aChildParents).to.have.lengthOf(1);
3439+
});
33973440
});
33983441

33993442
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)