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

html() does not work on React components which return an array of elements #1178

Closed
Tracked by #1553
wojtekmaj opened this issue Sep 27, 2017 · 5 comments
Closed
Tracked by #1553

Comments

@wojtekmaj
Copy link

wojtekmaj commented Sep 27, 2017

Hi!
I have a fairly simple component that takes advantage of React 16 and renders multiple elements, <p> to be precise. It changes a string or an array of strings to a paragraph or an array of paaragrahps, respectively. One of my test cases is like so:

const component = (
    <Paragraph>['Test 1', 'Test 2', 'Test 3']</Paragraph>
);

const html = mount(component).html();

expect(html).toBe('<p>Test 1</p><p>Test 2</p><p>Test 3</p>');

I'd expect that in this case .html() would return either a string as provided above in my expect().toBe() or an array of three strings. Instead, this ends in a following error:

Trying to get host node of an array

  at _nodeToHostNode (../node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:172:11)
  at ReactSixteenAdapter.nodeToHostNode (../node_modules/enzyme-adapter-react-16/build/ReactSixteenAdapter.js:421:16)
  at ReactWrapper.<anonymous> (../node_modules/enzyme/build/ReactWrapper.js:823:30)
  at ReactWrapper.single (../node_modules/enzyme/build/ReactWrapper.js:1534:25)
  at ReactWrapper.html (../node_modules/enzyme/build/ReactWrapper.js:820:21)
  at Object.<anonymous> (components/_common/__tests__/Paragraph.jsx:141:50)

Thanks in advance!

@lelandrichardson
Copy link
Collaborator

Hey @wojtekmaj, we still don't have array-rendered support in the 16 adapter, though I don't think it will be difficult to add. I appreciate your patience at this time.

If you're interested, opening up a PR with this as a test would be a great start... and you could even try to patch up some of what's going on here. But that's up to you. I will be addressing this soon...

Related to #1149

@tcodes0
Copy link

tcodes0 commented Aug 22, 2018

    "enzyme": "^3.4.4",
    "enzyme-adapter-react-16": "^1.2.0",

As of above this seem to still be an issue. I worked around it wrapping the component in a div and calling .text()/.html() on that

@ljharb
Copy link
Member

ljharb commented Aug 22, 2018

Yes, this issue will be closed when we have that support; we haven't added it yet.

You can also handle it by returning a React.Fragment rather than an array.

@ljharb
Copy link
Member

ljharb commented Sep 1, 2018

(related to #1213)

@ljharb
Copy link
Member

ljharb commented Oct 7, 2024

This actually shipped many years ago; I'm not sure why the issue was still open.

@ljharb ljharb closed this as completed Oct 7, 2024
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

4 participants