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

toHaveTextContent should support any Node #306

Closed
julienw opened this issue Nov 10, 2020 · 5 comments · Fixed by #358
Closed

toHaveTextContent should support any Node #306

julienw opened this issue Nov 10, 2020 · 5 comments · Fixed by #358
Assignees
Labels
enhancement New feature or request released

Comments

@julienw
Copy link
Contributor

julienw commented Nov 10, 2020

  • @testing-library/jest-dom version: 5.11.5
  • node version: 12.18.0
  • yarn version: 1.22.5
  • react-testing-library version: 11.1.0

Relevant code or config:

    const resourceLabel = getByText('Resource:');
    const valueElement = ensureExists(resourceLabel.nextSibling);
    expect(valueElement).toHaveTextContent('libxul.so');

What you did:

I'm in the process of installing and using jest-dom in our project. I installed the eslint plugin which converted this code:

    const resourceLabel = getByText('Resource:');
    const valueElement = ensureExists(resourceLabel.nextSibling);
    expect(valueElement.textContent).toBe('libxul.so');

What happened:

Here is the error I get:

    received value must be an HTMLElement or an SVGElement.
    Received has type:  object
    Received has value: libxul.so

       95 |     const resourceLabel = getByText('Resource:');
       96 |     const valueElement = ensureExists(resourceLabel.nextSibling);
    >  97 |     expect(valueElement).toHaveTextContent('libxul.so');

Reproduction:

https://codesandbox.io/s/react-testing-library-demo-forked-0puq7

Problem description:

The property textContent is present in all Nodes, not only HTMLElement. Therefore it would be nice if this expectation worked on all Nodes too, especially text nodes.

See the MDN page about textContent:
https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent

Suggested solution:

Is removing this line enough:

checkHtmlElement(htmlElement, toHaveTextContent, this)

Or should we replace it with a check for instanceof Node ?

@julienw

This comment has been minimized.

@gnapse
Copy link
Member

gnapse commented Nov 10, 2020

Makes sense. I think we should keep the check, even if changing it to check instanceof Node. I'm not sure it makes sense to support anything with a .textContent property.

@moltar
Copy link

moltar commented Apr 21, 2021

Same issue here, but with DocumentFragment.

    const { asFragment } = render(html`<${Home} />`)
    expect(asFragment()).toHaveTextContent(/Hello from Preact/)

Error:

    received value must be an HTMLElement or an SVGElement.
    Received has type:  object
    Received has value: <DocumentFragment><div class="container"><h1 style="background: grey;">Hello from Preact</h1><button class="btn btn-primary" type="button">Increment <span class="badge badge-light">1</span></button></div></DocumentFragment>

@julienw
Copy link
Contributor Author

julienw commented Apr 22, 2021

Thanks
I'm taking it to finish the existing PR #307

julienw added a commit to julienw/jest-dom that referenced this issue Apr 22, 2021
julienw added a commit to julienw/jest-dom that referenced this issue Apr 22, 2021
@github-actions
Copy link

🎉 This issue has been resolved in version 5.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
4 participants