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 Aug 21, 2018
1 parent 2ccacb4 commit c294dc0
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 @@ -3529,6 +3529,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('.b');
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 @@ -3444,6 +3444,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 @@ -702,8 +702,10 @@ class ReactWrapper {
* @returns {ReactWrapper}
*/
parents(selector) {
const allParents = this.wrap(this.single('parents', n => parentsOfNode(n, this[ROOT].getNodeInternal())));
return selector ? allParents.filter(selector) : allParents;
return this.single('parents', (n) => {
const allParents = this.wrap(parentsOfNode(n, this[ROOT].getNodeInternal()));
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 @@ -931,8 +931,10 @@ class ShallowWrapper {
* @returns {ShallowWrapper}
*/
parents(selector) {
const allParents = this.wrap(this.single('parents', n => parentsOfNode(n, getRootNodeInternal(this))));
return selector ? allParents.filter(selector) : allParents;
return this.single('parents', (n) => {
const allParents = this.wrap(parentsOfNode(n, getRootNodeInternal(this)));
return selector ? allParents.filter(selector) : allParents;
});
}

/**
Expand Down

0 comments on commit c294dc0

Please sign in to comment.