-
Notifications
You must be signed in to change notification settings - Fork 404
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: normalize expected value in toContainHTML #349
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
import {checkHtmlElement} from './utils' | ||
|
||
function getNormalizedHtml(container, htmlText) { | ||
const div = container.ownerDocument.createElement('div') | ||
div.innerHTML = htmlText | ||
return div.innerHTML | ||
} | ||
|
||
export function toContainHTML(container, htmlText) { | ||
checkHtmlElement(container, toContainHTML, this) | ||
|
||
if (typeof htmlText !== 'string') { | ||
throw new Error(`.toContainHTML() expects a string value, got ${htmlText}`) | ||
} | ||
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this change, the Now the |
||
|
||
return { | ||
pass: container.outerHTML.includes(htmlText), | ||
pass: container.outerHTML.includes(getNormalizedHtml(container, htmlText)), | ||
message: () => { | ||
return [ | ||
this.utils.matcherHint( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to remove the test on the invalid html? What happens after these changes if we give invalid html like the one in this string? It'd be nice to know and add a test for that. And hopefully that what it does is still close to what one would expect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, the tests can stay, just need to flip the direction, as this assertion now passes.
Fixed in the next revision
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it that these tests now pass? I would expect them not to pass. The rendered DOM in this test does contain the invalid html given. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTML parser ignores unmatching closing tags. Currently we have this content:
<span data-testid="child"></div>
. After normalization viainnerHTML
it becomes<span data-testid="child"></span>
and this matches the actual content.With normalization, this test will not pass, for example:
<table>test</table>
does not expand to the full table and does not match the real contentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I wonder if we could detect that, and disallow invalid html. This also makes it technically a breaking change, although I'm thinking that I'll pass on that, since it is very unlikely someone was passing invalid html to explicitly expect a failure.
I think this is good to go. Will merge soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure this is possible without rolling your own parser. Built-in DOM parser does not expose enough information.
I am also wondering about the use-case here. Spotting the typos is nice, but is there any more important case where it is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, which is why I said I approve anyway. It's just a bit unintuitive, but I agree that it makes sense since it is how html works anyway. Developing our own parser is out of the question. This matcher is anyway an outlier, in the sense that we do not recommed its use except in some unique circumstances.