enzymejs/enzyme

another .find() will cause the previous .find() not able to get .parent()

Open

#1916 opened on Nov 27, 2018

View on GitHub
 (15 comments) (1 reaction) (1 assignee)JavaScript (19,979 stars) (2,016 forks)batch import
API: shallowbughelp wanted

Description

Current behavior

another .find() will cause the previous .find() not able to get .parent()

Expected behavior

should be able to get .parent(), it was ok in v3.3.0

Your environment

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.7.0
react 16.6.3
react-dom 16.6.3
enzyme-adapter-react-16 1.7.0

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )

Details

please check the code here, https://codesandbox.io/s/54j70o404k

import React from "react";

import Enzyme, { shallow } from "enzyme";
import Adapter from "enzyme-adapter-react-16";

Enzyme.configure({ adapter: new Adapter() });

it("another find causing bug", () => {
  class TestComponent extends React.Component {
    render() {
      return (
        <div>
          <h1>Title</h1>
          <span key="1">1</span>
          <span key="2">2</span>
        </div>
      );
    }
  }
  const component = shallow(<TestComponent />);
  const cards = component.find("span");
  expect(cards).toHaveLength(2);

  // #region select another component, comment out this region
  const title = component.find("h1");
  expect(title).toHaveLength(1);
  expect(title.text()).toBe("Title");
  // #endregion

  // failed since 3.4.0, was able to pass in 3.3.0
  // comment out the region above to make this passes
  expect(
    cards
      .at(0)
      .parent()
      .is("div")
  ).toBe(true);
});

update: I cloned the codesandbox with the latest dependencies but still fail, https://codesandbox.io/s/y3nrx89vwx

Contributor guide