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

mount fails to find className on root component #1185

Closed
pfhayes opened this issue Sep 28, 2017 · 7 comments
Closed

mount fails to find className on root component #1185

pfhayes opened this issue Sep 28, 2017 · 7 comments

Comments

@pfhayes
Copy link
Contributor

pfhayes commented Sep 28, 2017

This fails when I would expect it to succeed (and it did succeed in 2.x)

it('enzyme', () => {
  const F = (props) => <div className={props.className}/>; 
  const test = mount(<F className="x"/>);
  expect(test.find('.x').length).toBe(1);
});

Notably, this slight modification does succeed:

it('enzyme', () => {
  const test = mount(<div className="x"/>);
  expect(test.find('.x').length).toBe(1);
});

as does this one

it('enzyme', () => {
  const F = (props) => <div className={props.className}/>; 
  const test = shallow(<F className="x"/>);
  expect(test.find('.x').length).toBe(1);
});

so it appears to be specifically related to custom components and mount

@pfhayes
Copy link
Contributor Author

pfhayes commented Oct 2, 2017

cc @ljharb - is this indeed a regression or intended behaviour?

@ljharb
Copy link
Member

ljharb commented Oct 2, 2017

What does test.debug() output?

@pfhayes
Copy link
Contributor Author

pfhayes commented Oct 2, 2017

It outputs the following:

      <F className="x">
        <div className="x" />
      </F>

@ljharb
Copy link
Member

ljharb commented Oct 2, 2017

So there's two things with the className of .x - it seems like maybe you want #1179's hostNodes, such that you can do test.hostNodes().find('.x') - in the meantime, test.filter(n => typeof n.type() === 'string').find('.x') will work.

@pfhayes
Copy link
Contributor Author

pfhayes commented Oct 2, 2017

The debugging output has two items with className="x" but the actual rendered DOM output would not include the <F/> tag, right?

For example, in the shallow example test.debug() outputs this:

<div className="x" />

It seems to me that the <F/> turning up in the .find call is quite unexpected (and certainly inconsistent between mount and shallow). Is using hostNodes() the intended solution going forward?

Overall this issue does just appear to be a duplicate of #1174, though that issue does not address the inconsistency between mount and shallow. Which is the correct behaviour?

@ljharb
Copy link
Member

ljharb commented Oct 2, 2017

They need not be consistent; they're different conceptual APIs.

@ljharb
Copy link
Member

ljharb commented Jul 6, 2018

This is answered; please see the attached project for tracking this discontinuity between mount and shallow.

@ljharb ljharb closed this as completed Jul 6, 2018
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