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

descendants(selector) returns true for root element #142

Open
lukebatchelor opened this issue Jan 10, 2017 · 3 comments
Open

descendants(selector) returns true for root element #142

lukebatchelor opened this issue Jan 10, 2017 · 3 comments

Comments

@lukebatchelor
Copy link

I'm just wondering if the following is truly the expected behaviour (from the readme)

import React from 'react'
import {mount, render, shallow} from 'enzyme'

class User extends React.Component {
  render () {
    return (
      <span>User</span>
    )
  }
}

class Fixture extends React.Component {
  render () {
    return (
      <div id='root'>
        <User />
      </div>
    )
  }
}

const wrapper = mount(<Fixture />) // mount/render/shallow when applicable

expect(wrapper).to.have.descendants('#root')
expect(wrapper).to.have.descendants(User)

should the expect(wrapper).to.have.descendants('#root') line really be passing? I would expect that descendants would only check actual descendants, not the root level element?

That could just be my interpretation of the word though, so happy to close this, just wanted to check.

@marcodejongh
Copy link
Contributor

Descendants uses enzyme find internally which always starts at the top of the render, NOT the descendants of the first element of the render. I'm pretty sure this is intended behaviour and all enzyme methods and as a result chai-enzyme methods will have the same behaviour.

To achieve what you're trying to do you could do something like:

const firstChild = wrapper.children().first();
expect(firstChild).to.not.have.descendants('#root');

@ayrton
Copy link
Contributor

ayrton commented Jan 30, 2017

Wondering if we should alias this to

expect(wrapper).to.have.found('#root')

to avoid the confusion

@marcodejongh
Copy link
Contributor

Not sure if found communicates it clearly enough. Maybe the descendants behaviour can be merged into contains?

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

3 participants