-
Notifications
You must be signed in to change notification settings - Fork 412
feat: #9: Normalized whitespace using dom-testing-library #56
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
feat: #9: Normalized whitespace using dom-testing-library #56
Conversation
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.
Looks good. BTW, don't merge yet. I wonder if this is breaking change. It looks like it.
src/to-have-text-content.js
Outdated
import {checkHtmlElement, getMessage, matches} from './utils' | ||
|
||
export function toHaveTextContent(htmlElement, checkWith) { | ||
checkHtmlElement(htmlElement, toHaveTextContent, this) | ||
const textContent = htmlElement.textContent | ||
const textContent = getNodeText(htmlElement).replace(/\s+/g, ' ') |
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 wonder what happens with
s in the text? I guess it's a borderline feature and can probably wait until someone has the pain though.
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.
Hrm, this is a great question. I believe element.textContent
will see them as spaces and we would normalize them down to once space which is incorrect in that case (haha, as they are non breaking spaces). Do you think we should make the normalization (of extra spaces) to be optional like dom-testing-library does?
Step | ||
1 | ||
of 4 | ||
</span>`) |
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 would suggest indent this two spaces under the c
of const
. The extra spaces on the string won't make a difference, and it would be respecting more the code style.
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.
Will do!
expect(() => | ||
expect(queryByTestId('count-value')).not.toHaveTextContent('2'), | ||
).toThrowError() | ||
test('handles negative test cases', () => { |
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.
Good that you started separating the test cases. It was becoming hard to read. 👏 👏 👏
I do believe this is a breaking change. If someone was relying on the spacing their tests would start breaking. If we added this in optionally then maybe not, something like:
|
Hey @gnapse, on the last commit, I set the normalization of spaces to be optional (just in case a user desires to test against |
Hi @gnapse, Just checking in on this one! The last we left off we said we want to hold off on merging:
I agree that it is a breaking change, it is set to normalize spaces by default, with an option to turn if off. Let me know your thoughts, |
Ok, let's merge it then! |
Whoohoo! :-)! |
This change breaks testing for text within child elements (nested text), because you switched to using Example: <div class="error">
<ul>
<li>Email format is invalid</li>
</ul>
</div> I just want to write something like Since it now fails, the best I can come up with is something like |
I don't think that would have worked before. The regex is how I would have done that before and now 🤔 |
Hrm, these are really good points! @kentcdodds and @noinkling I am going to copy the conversation to #9 so it doesn't get lost! |
What:
Implementing #9, it required an adjustment from dom-testing-library as testing-library/dom-testing-library#31 makes fuzzy logic optional.
Why:
To allow users to test against text with spacing that varies. The text should be normalized to only have one space.
How:
As discussed in #9 used dom-testing-library's get-node-text function and also included the regex multiple space to one space replacement.
Checklist: