Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MountedTraversal.pathToNode() potentially returns descendants #790

Closed
begincalendar opened this issue Feb 1, 2017 · 5 comments
Closed

Comments

@begincalendar
Copy link

begincalendar commented Feb 1, 2017

Please correct me if I'm wrong, but I think the implementation of the MountedTraversal.js:pathToNode() function is flawed.

Consider this slice of a DOM:

<div data-name="reactRoot">                                                      
  <div data-name="irrelevant1">                                                  
  </div>                                                                         
  <div data-name="irrelevant2">                                                  
  </div>                                                                         
  <div data-name="wrapper">                                                      
    <div data-name="firstRootWrapper">                                           
      <div data-name="firstRoot">                            
        First Root                                                               
      </div>                                                                     
      <div data-name="firstRoot-children">                                       
        <div data-name="firstChild">                         
          First Child                                                            
        </div>                                                                   
        <div data-name="secondChildWrapper">                                     
          <div data-name="secondChild">                      
            Second Child                                                         
          </div>                                                                 
          <div data-name="secondChild-children">                                 
            <div data-name="firstGrandChild">                
              First Grand Child                                                  
            </div>                                                               
            <div data-name="secondGrandChild">               
              Second Grand Child                                                 
            </div>                                                               
          </div>                                                                 
        </div>                                                                   
      </div>                                                                     
    </div>                                                                       
    <div data-name="secondRoot">                             
      Second Root                                                                
    </div>                                                                       
  </div>                                                                         
</div>

If I call pathToNode() like so:

const node = document.querySelector('div[data-name="firstChild"]');
const root = document.querySelector('div[data-name="reactRoot"]');

const result = pathToNode(node, root);

...then I get the div[data-name="secondChildChildren"] element as the last element in the result array, yet that is not a parent/ancestor of the node, it is a descendant.

Enzyme version: 2.6.0

Am I missing something, or is this a legitimate problem?

@begincalendar
Copy link
Author

begincalendar commented Feb 1, 2017

This is what I come up with as a "trace" for the execution of the above problematic case:

# Loop 0
current  = reactRoot
children = [irrelevant1, irrelevant2, wrapper]
path     = [reactRoot]
queue    = [irrelevant1, irrelevant2, wrapper]

# Loop 1
current  = wrapper
children = [firstRootWrapper, secondRoot]
path     = [reactRoot, wrapper]
queue    = [irrelevant1, irrelevant2, firstRootWrapper, secondRoot]

# Loop 2
current  = secondRoot
children = []
path     = [reactRoot, wrapper]
queue    = [irrelevant1, irrelevant2, firstRootWrapper]

# Loop 3
current  = firstRootWrapper
children = [firstRoot, firstRoot-children]
path     = [reactRoot, wrapper, firstRootWrapper]
queue    = [irrelevant1, irrelevant2, firstRoot, firstRoot-children]

# Loop 4
current  = firstRoot-children
children = [firstChild, secondChild-children]
path     = [reactRoot, wrapper, firstRootWrapper]
queue    = [irrelevant1, irrelevant2, firstRoot, firstChild, secondChild-children]

# Loop 5
current  = secondChild-children
children = [firstGrandChild, secondGrandChild]
path     = [reactRoot, wrapper, firstRootWrapper, secondChild-children]
queue    = [irrelevant1, irrelevant2, firstRoot, firstChild, firstGrandChild, secondGrandChild]

# Loop 6
current  = secondGrandChild
children = []
path     = [reactRoot, wrapper, firstRootWrapper, secondChild-children]
queue    = [irrelevant1, irrelevant2, firstRoot, firstChild, firstGrandChild]

# Loop 7
current  = firstGrandChild
children = []
path     = [reactRoot, wrapper, firstRootWrapper, secondChild-children]
queue    = [irrelevant1, irrelevant2, firstRoot, firstChild]

# Loop 8
current  = firstChild
children = []
path     = [reactRoot, wrapper, firstRootWrapper, secondChild-children]
queue    = [irrelevant1, irrelevant2, firstRoot]

That manual trace is consistent with the results I actually get when calling the function.

So the problem is that secondChild-children is returned in the path result, when it is a descendent of the input node.

@ljharb
Copy link
Member

ljharb commented Feb 1, 2017

Which version of enzyme are you using?

@begincalendar
Copy link
Author

2.6.0

Updated the original post to reflect that, sorry.

@ljharb
Copy link
Member

ljharb commented Feb 1, 2017

Can you try on v2.7.1? I think this might have been fixed by any or all of #713, #691, #677.

@begincalendar
Copy link
Author

@ljharb Yep, that's done the trick.

Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants