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

parent() not work #1230

Closed
HsuTing opened this issue Oct 5, 2017 · 9 comments
Closed

parent() not work #1230

HsuTing opened this issue Oct 5, 2017 · 9 comments

Comments

@HsuTing
Copy link

HsuTing commented Oct 5, 2017

After I upgrade to react@16 and enzyme@3. parent() does not work for me.

I try to use wrapper.parent(), and it returns null to me.
However, wrapper.parents().first() does work. It returns the correct component.

@ljharb
Copy link
Member

ljharb commented Oct 5, 2017

The wrapper has no parent in enzyme 3 - it's the top of the tree.

@eddiemonge
Copy link

maybe wrapper isn't the actual wrapper. I think parent() is broken as well. See #1373

@chriswatrous
Copy link

chriswatrous commented Apr 27, 2018

I'm also having a problem with .parent(). I get an error when I try to simulate an event on the return from .parent() but not with .parents().at(0).

Test script:

import React from 'react';
import {mount} from 'enzyme';

const runTest = (findParent) => {
  let value = false;
  const wrapper = mount(
    <div>
      <div id='parent' onClick={() => value = true}>
        <div id='child'/>
      </div>
    </div>
  );
  const parent = findParent(wrapper);
  console.log('parent =', parent.debug());
  parent.simulate('click');
  expect(value).toBe(true);
}

describe('', () => {
  // passes
  it('', () => runTest((wrapper) => wrapper.find('#parent')));

  // passes
  it('', () => runTest((wrapper) => wrapper.find('#child').parents().at(0)));

  // fails
  it('', () => runTest((wrapper) => wrapper.find('#child').parent()));
});

Output:

 FAIL  test/integration/routes/event-test.spec.js
    ✓  (43ms)
    ✓  (5ms)
    ✕  (11ms)

  ●  ›

    TypeError: Cannot read property 'tagName' of null

      13 |   const parent = findParent(wrapper);
      14 |   console.log('parent =', parent.debug());
    > 15 |   parent.simulate('click');
      16 |   expect(value).toBe(true);
      17 | }
      18 |

      at node_modules/react-dom/lib/ReactTestUtils.js:323:34
      at Object.simulateEvent (node_modules/enzyme-adapter-react-15/build/ReactFifteenAdapter.js:199:15)
      at ReactWrapper.<anonymous> (node_modules/enzyme/build/ReactWrapper.js:868:29)
      at ReactWrapper.single (node_modules/enzyme/build/ReactWrapper.js:1534:25)
      at ReactWrapper.simulate (node_modules/enzyme/build/ReactWrapper.js:867:14)
      at runTest (test/integration/routes/event-test.spec.js:15:10)
      at Object.<anonymous> (test/integration/routes/event-test.spec.js:27:16)

  console.log test/integration/routes/event-test.spec.js:14
    parent = <div id="parent" onClick={[Function: onClick]}>
      <div id="child" />
    </div>

  console.log test/integration/routes/event-test.spec.js:14
    parent = <div id="parent" onClick={[Function: onClick]}>
      <div id="child" />
    </div>

  console.log test/integration/routes/event-test.spec.js:14
    parent = <div id="parent" onClick={[Function: onClick]} />

Test Suites: 1 failed, 1 total
Tests:       1 failed, 2 passed, 3 total
Snapshots:   0 total
Time:        2.342s, estimated 3s

[email protected]
[email protected]

@ljharb
Copy link
Member

ljharb commented Jun 26, 2018

@chriswatrous I believe your issue is covered by #1373.

I'm going to close this; please file new issues for other concerns.

@ljharb ljharb closed this as completed Jun 26, 2018
@huchenme
Copy link

@ljharb this still happens for 3.5.0 wrapper.parent() not working but wrapper.parents().first() does

@ljharb
Copy link
Member

ljharb commented Aug 28, 2018

@huchenme can you file a new issue, with repro steps?

@upupming
Copy link

upupming commented Feb 7, 2020

@ljharb Hi, I am encountering this bug, too. This is a repo for reproducing the bug: https://github.com/upupming/enzyme-parent-bug

@ljharb
Copy link
Member

ljharb commented Feb 7, 2020

@upupming thanks for filing a new issue, #1991.

@KorayAydemir
Copy link

parent() still does not work but parents().at(0) works and returns correct element

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

No branches or pull requests

7 participants