File tree Expand file tree Collapse file tree 5 files changed +99
-5
lines changed
Expand file tree Collapse file tree 5 files changed +99
-5
lines changed Original file line number Diff line number Diff 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()' , ( ) => {
Original file line number Diff line number Diff 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()' , ( ) => {
Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ export function pathToNode(node, root) {
106106}
107107
108108export function parentsOfNode ( node , root ) {
109- return pathToNode ( node , root ) . reverse ( ) ;
109+ return ( pathToNode ( node , root ) || [ ] ) . reverse ( ) ;
110110}
111111
112112export function nodeHasId ( node , id ) {
Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff 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 /**
You can’t perform that action at this time.
0 commit comments