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

fix: check for any Node in toHaveTextContent #307

Closed
wants to merge 1 commit into from

Conversation

nickserv
Copy link
Member

@nickserv nickserv commented Nov 11, 2020

What:

Fixes #306

How:

!(htmlElement instanceof Node)

Checklist:

  • Documentation N/A
  • Tests
  • Updated Type Definitions N/A
  • Ready to be merged

@nickserv nickserv changed the title fix: check for any Node in toHaveTextContent fix: check for any Node in toHaveTextContent (fixes #307) Nov 11, 2020
@nickserv nickserv changed the title fix: check for any Node in toHaveTextContent (fixes #307) fix: check for any Node (fixes #306) Nov 11, 2020
@nickserv nickserv linked an issue Nov 11, 2020 that may be closed by this pull request
@nickserv nickserv changed the title fix: check for any Node (fixes #306) fix: check for any Node Nov 11, 2020
@nickserv nickserv changed the title fix: check for any Node fix: check for any Node in toHaveTextContent Nov 11, 2020
@nickserv nickserv requested a review from gnapse November 11, 2020 22:14
@nickserv
Copy link
Member Author

This needs more work on the error handling.

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not test the error message? Shouldn't the error message change and mention Node instead of HTMLElement?

@nickserv
Copy link
Member Author

You're right. The error message is fixed, just need to fix the rest and add the error message to tests.

@gnapse
Copy link
Member

gnapse commented Nov 25, 2020

#312 reveals that this may not be the only custom matcher that has to allow any node and not HTML elements. It may be worth checking all our matchers to see which other ones make sense to have this change done. Although a quick look at our list of custom matchers makes me think these may be the only two exceptions, but I may be wrong.

@nickserv
Copy link
Member Author

nickserv commented Nov 30, 2020

That's an issue with DOM Testing Library, not Jest DOM. It makes sense to update all other matchers to use Element for consistency with TypeScript types (as they aren't checking the textContent property of Node), so I've opened #313 to track it separately.

@gnapse gnapse force-pushed the check-for-any-node branch from 9af9394 to d6dd1b7 Compare December 30, 2020 13:09
Copy link
Member

@gnapse gnapse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nickmccurdy I rebased this and tried to work out the CI build failure. It is due to there not being a test that covers the failure case (a test that explicitly makes it fail when it receives something other than a Node).

I then tried to do fix it myself but it became non-trivial. See my comment below. I then decided to leave it to you to decide how to approach this, as you are the PR author. Let me know and can take it from here if you are not able to deal with this in the short term.


export function toHaveTextContent(
htmlElement,
checkWith,
options = {normalizeWhitespace: true},
) {
checkHtmlElement(htmlElement, toHaveTextContent, this)
const window = htmlElement.ownerDocument.defaultView
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a check prior to this line. Otherwise this line can throw when receiving something that does not even have a ownerDocument property. Or if the obtained window value is not a window object and does not have a window.Node thing in it.

Base automatically changed from master to main January 20, 2021 11:47
@julienw
Copy link
Contributor

julienw commented Feb 11, 2021

hey @nickmccurdy, any luck with moving this forward? :-) Needs a hand?

@gnapse
Copy link
Member

gnapse commented Apr 22, 2021

Superseded by #358

Anyway, thanks @nickmccurdy for giving it a shot here.

@gnapse gnapse closed this Apr 22, 2021
@gnapse gnapse deleted the check-for-any-node branch April 22, 2021 11:22
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

Successfully merging this pull request may close these issues.

toHaveTextContent should support any Node
4 participants