Skip to content

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

Conversation

shermanhui
Copy link
Contributor

@shermanhui shermanhui commented Sep 15, 2020

I'm opening a PR for issue #381 to add a simple JS login
form as an example of how to use the findByText query
to 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, I
think 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 to display: 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 not
seeing a value in the input element when I debug and therefore
the element style is not being updated.

EDIT: Turns out it was failing because I was using document.getElementById and document
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! 🎉

Screen shot of hasAttribute type error

Screen shot of error on Codesandbox

Thanks in advance!

Also let me know if I should add React specific examples for findByText
or any other findBy queries!

Closes #381

@shermanhui shermanhui marked this pull request as draft September 15, 2020 02:30
@alexkrolick
Copy link
Collaborator

alexkrolick commented Oct 1, 2020

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 await findBy... in general, than just about appearance. I think it would be good to go forward with something like this.

@@ -0,0 +1,142 @@
---
id: example-findBy*
Copy link
Collaborator

@alexkrolick alexkrolick Oct 1, 2020

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.

Copy link
Contributor Author

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.

@alexkrolick alexkrolick added the 🚧 work in progress Not ready, being worked on label Oct 1, 2020
// expect(getByText(container, "Invalid Password")).toBeVisible()
// })
// screen.debug(await findByText(container, "Invalid Password"));
expect(await findByText(container, 'Invalid User Name')).toBeVisible()
Copy link
Collaborator

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.

Copy link
Contributor Author

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!

@shermanhui shermanhui force-pushed the add-vanilla-js-findByText-example branch 4 times, most recently from 6aae842 to 35043b4 Compare October 3, 2020 03:05
// 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() ===
Copy link
Contributor Author

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.

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

@shermanhui shermanhui marked this pull request as ready for review October 3, 2020 03:17
@shermanhui shermanhui requested a review from alexkrolick October 3, 2020 03:17
@shermanhui
Copy link
Contributor Author

@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 format-docs just to make sure my changeset was compliant with the current documentation format.

@shermanhui shermanhui force-pushed the add-vanilla-js-findByText-example branch from 35043b4 to d331cbc Compare October 3, 2020 04:45
@@ -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. '\*'.
Copy link
Contributor Author

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"
Copy link
Contributor Author

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

@shermanhui shermanhui force-pushed the add-vanilla-js-findByText-example branch 2 times, most recently from b947131 to 51d4d5a Compare October 3, 2020 20:27
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
@shermanhui shermanhui force-pushed the add-vanilla-js-findByText-example branch from 51d4d5a to 91ae836 Compare October 3, 2020 20:33
@shermanhui
Copy link
Contributor Author

@alexkrolick just a friendly ping to re-review the changes made - hope we can merge this in! :)

@alexkrolick alexkrolick merged commit 238792f into testing-library:master Oct 7, 2020
@shermanhui
Copy link
Contributor Author

Thanks @alexkrolick :) Would it be cool if I got added to the Contributors list as well? Thanks again for the approval + merge 🎉

@alexkrolick alexkrolick removed the 🚧 work in progress Not ready, being worked on label Oct 7, 2020
@alexkrolick
Copy link
Collaborator

@all-contributors please add @shermanhui for docs

@allcontributors
Copy link
Contributor

@alexkrolick

I've put up a pull request to add @shermanhui! 🎉

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.

More examples for FindBy* queries
2 participants