-
Notifications
You must be signed in to change notification settings - Fork 726
docs: add findByText example #616
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
docs: add findByText example #616
Conversation
Any other maintainers have thoughts on if we would want a recipe like this? We have docs under the Appearance/Disappearance section that are similar. https://testing-library.com/docs/guide-disappearance Edit: I see this is related to #381 actually - so more in the area of showing |
docs/eample-findBy*.md
Outdated
@@ -0,0 +1,142 @@ | |||
--- | |||
id: example-findBy* |
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.
Can we avoid using non-url safe characters like *
, and fix the filename? It also needs to be added to the table of contents somewhere in order to show up in site navigation.
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.
Done @alexkrolick! I used *
as I was trying to follow the convention in the docs but since this example file only contains a findByText
example, I've renamed it to example-findByText
.
I've also added this new example to the site navigation.
docs/eample-findBy*.md
Outdated
// expect(getByText(container, "Invalid Password")).toBeVisible() | ||
// }) | ||
// screen.debug(await findByText(container, "Invalid Password")); | ||
expect(await findByText(container, 'Invalid User Name')).toBeVisible() |
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 don't think findByText
excludes hidden elements by default, does it? That's why this assertion fails.
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.
Ah it turns out it was my own mistake. The test wasn't finding the input element at all, I should have been using el.querySelector
instead of document.getElementById
😅 . The tests pass as expected now!
6aae842
to
35043b4
Compare
docs/example-findByText.md
Outdated
// https://stackoverflow.com/questions/55509875/how-to-query-by-text-string-which-contains-html-tags-using-react-testing-library | ||
await findByText(container, (_, element) => { | ||
const hasText = (element) => | ||
element.textContent.replace(/(\r\n|\n|\r)/gm, "").trim() === |
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 formatted the html
in renderContent
and apparently textContent
will preserve line breaks and new lines, I've used this regex to remove it. Let me know if there's a better way to handle this!
I also used the custom text matcher function from the above StackOverflow answer.
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 try to keep the incidental complexity in the example to a minimum; focus on showing the key info about the findBy queries. Is there a way you can trim it down?
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.
Thanks, @alexkrolick, that's a good point - I've changed the example so the warning shows the same way that the "invalid user name/password" text does to avoid inline HTML elements to keep the complexity down! Let me know if there's anything else that needs fixing up.
@alexkrolick I've fixed the tests on Codesandbox and pushed the requested changes, let me know if there's anything else I can add to help out with closing #381! :) I committed some unrelated formatted files as well since I ran |
35043b4
to
d331cbc
Compare
@@ -110,6 +110,9 @@ title: This Doc Needs To Be Edited | |||
My new content here.. | |||
``` | |||
|
|||
Note: Ensure the file name and the id value do not include non-url safe | |||
characters i.e. '\*'. |
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.
Updated the website README to make sure future contributors don't use non-url safe characters.
@@ -138,7 +139,7 @@ | |||
"example-reach-router", | |||
"example-react-transition-group", | |||
"example-react-modal", | |||
"example-external" | |||
"example-update-props" |
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.
Reorganized these examples to be in alphabetical order
b947131
to
51d4d5a
Compare
Add a simple JS login form to show examples of how to use the `findBy*` query, in particular `findByText` for DOM elements that may not be visible immediately. Add two tests that assert for warnings that appear when the user tries to submit the form with no username and password and when the user enters invalid field values.
Reorganize Examples to be alphabetically sorted by example file name
Commit the results of `format-docs` to ensure files follow documentation format
add note to ensure any new docs pages do not include non-url safe characters in the file name or markdown id
51d4d5a
to
91ae836
Compare
@alexkrolick just a friendly ping to re-review the changes made - hope we can merge this in! :) |
Thanks @alexkrolick :) Would it be cool if I got added to the Contributors list as well? Thanks again for the approval + merge 🎉 |
@all-contributors please add @shermanhui for docs |
I've put up a pull request to add @shermanhui! 🎉 |
I'm opening a PR for issue #381 to add a simple JS login
form as an example of how to use the
findByText
queryto find DOM elements that may not be visible immediately.
I'm open to any feedback as I haven't written tests for
vanilla JS in a long time (too spoiled with React).
Specifically, Ithink I need some clarification on an issue I ran into with my example tests
not passing on Codesandbox. Here is the link to the sandbox,
it's interesting that the first set of tests pass when
I change the display value from
display: none
todisplay: inline
,but the same doesn't happen with the mock error message
span
elements 🤔.
Here's a screenshot of a type error I ran into with regards to using
userEvent.type
with the input element..perhaps that's why I'm notseeing a value in the
input
element when I debug and thereforethe element style is not being updated.
EDIT: Turns out it was failing because I was using
document.getElementById
anddocument
is actually empty as we're using the
renderContent
function to render the form.The live tests can be seen here
To fix it, I just used
el.querySelector
instead and this got the tests to pass! 🎉Thanks in advance!
Also let me know if I should add React specific examples for
findByText
or any other
findBy
queries!Closes #381