-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Cheat Sheet should be updated to reflect that wait and waitForElement are deprecated #673
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
Comments
Ah yeah, that'll need to get updated. @bcarroll22 do you still have the original source file for that? |
The React Example also uses |
I will pick this ticket up :) |
This is a first draft of the content; please ignore the shaky layout. Oh, this an slightly version a newer version refers to |
Thanks for working on this @weyert! Here are a few bits of feedback (these are just my thoughts, feel free to disagree): render a component:
search the DOM: This doesn't seem like the place to include references to assertions. Remove the comment. fire an event:
interact with element:
wait for something: Place an assertion in the callback. Something like: await waitFor(() => expect(mockFn).toHaveBeenCalledWith('some argument')) If you're waiting for an element, you should use search variants (return value):
(DOM attribute) search types:
text matches:
render(<label>Remember Me <input type="checkbox" /></label>)
screen.getByRole('checkbox', {name: /remember me/i}) // ✅
screen.getByRole('checkbox', {name: 'remember me'}) // ❌
screen.getByRole('checkbox', {name: 'Remember Me'}) // ✅
// other queries accept text matches as well
// the text match argument can also be a function
screen.getByText((text, element) => {/* return true/false */}) wait for appearance: This should use test('movie title appears', async () => {
render(<Movie />)
// element is initially not present...
// wait for appearance
const movieTitle = await findByText(/the lion king/i)
}) wait for element to be removed: Let's do this: test('submit button disappears', async () => {
// Element is initially present...
// Wait for element to be removed
await waitForElementToBeRemoved(screen.getByRole('button', {name: /submit/i}))
}) the screen object (description): I believe this used to be the "render result" section, so it's mostly wrong. All the Thanks so much fo this! Love it! |
Thank you for your feedback @kentcdodds. I will play around and apply your changes :) |
I have worked a bit more on the cheat sheet |
render a component: ✅ search the DOM: Make sure we import import {screen, render} from '@testing-library/react'
render(<label>Remember Me <input type="checkbox" /></label>)
const checkboxInput = screen.getByRole('checkbox', {name: /remember me/i}) interact with element: Make sure we import that correctly: import {userEvent} from '@testing-library/react' The Make sure to add screen: "Queries for the baseElement" should actually say "Functions to query the DOM." search variants (result): I think we could avoid the last line being on multiple lines if we reduce the padding in the left column a bit. search types: The table is not alternating white and gray rows. text matches: This example isn't quite right. The first line is rendering "Hello World" and is missing a closing parentheses. The example I gave in my previous comment should work perfectly as a copy/paste I think. wait for appearance: ✅ wait for element to be removed: The argument passed to await waitForElementToBeRemoved(
() => screen.getByRole('button', {name: /submit/i})
) wait for something: Could we reformat this a tiny bit? Could probably be: await waitFor(() =>
expect(mockFn).toHaveBeenCalledWith('some argument')
) render() options: Let's leave off Making great progress :) |
Thank you for your feedback, I have applied some of the feedback already this morning. I will have a second look over my lunch break to double check. If anyone see any other typos/mistakes, don't hesitate to let me know! Also as you can see there is still bits of a space left, so if think of anything else that could be added (examples or callouts), let me know. |
Updated the cheat sheet so it's referring to |
Cool! search the DOM: Could we reformat that a tiny bit so it doesn't run onto two lines like it does? const checkboxInput = screen.getByRole(
'checkbox',
{name: /remember me/i}
) interact with element: We backtracked on the async stuff (which is why the merge didn't happen) and now everything is wait for appearance, wait for element to be removed, and wait for something: I think all three of these could be simplified and combined into a single box titled wait for things. And here's what it could have in it: // the element isn't available yet, so wait for it:
const movieTitle = await screen.findByText(/the lion king/i)
// the element is there but we want to wait for it to be removed
await waitForElementToBeRemoved(() => screen.getByLabelText(/loading/i))
// we want to wait until an assertion passes
await waitFor(() => expect(mockFn).toHaveBeenCalledWith('some arg')) If those lines are too long, then use this and adjust the print width option. Looking great! |
@kentcdodds I have updated the PDF with your latest comments and I also have added the colour coding as shown by Prettier to it: Let me know, if you are happy with it, then I will make a PR with the source file + PDF |
Sweet, I spend some time fixing the margins: |
🎉 This issue has been resolved in version 10.3.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Awesome thank you for merging :) |
Cheat sheet URL: https://github.com/testing-library/react-testing-library/raw/master/other/cheat-sheet.pdf
Section in question:

The text was updated successfully, but these errors were encountered: