Skip to content

Commit ca2e941

Browse files
author
mattstobbs
authored
docs: use toBeDisabled() in example react-testing-library(#808)
* Update docs to use toBeDisabled() The examples in the docs currently use `.toHaveAttribute('disabled')`. However, jest-dom provides `toBeDisabled()`, which does the same thing but is more expressive and gives a better failure message. * Add comment descriptions
1 parent 21f409d commit ca2e941

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

docs/react-testing-library/example-intro.mdx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test('loads and displays greeting', async () => {
3535
await waitFor(() => screen.getByRole('heading'))
3636

3737
expect(screen.getByRole('heading')).toHaveTextContent('hello there')
38-
expect(screen.getByRole('button')).toHaveAttribute('disabled')
38+
expect(screen.getByRole('button')).toBeDisabled()
3939
})
4040

4141
test('handles server error', async () => {
@@ -52,7 +52,7 @@ test('handles server error', async () => {
5252
await waitFor(() => screen.getByRole('alert'))
5353

5454
expect(screen.getByRole('alert')).toHaveTextContent('Oops, failed to fetch!')
55-
expect(screen.getByRole('button')).not.toHaveAttribute('disabled')
55+
expect(screen.getByRole('button')).not.toBeDisabled()
5656
})
5757
```
5858

@@ -158,11 +158,13 @@ await waitFor(() =>
158158
### Assert
159159

160160
```jsx
161-
// assert that the alert message is correct.
161+
// assert that the alert message is correct using
162+
// toHaveTextContent, a custom matcher from jest-dom.
162163
expect(screen.getByRole('alert')).toHaveTextContent('Oops, failed to fetch!')
163164

164-
// assert that the button is not disabled.
165-
expect(screen.getByRole('button')).not.toHaveAttribute('disabled')
165+
// assert that the button is not disabled using
166+
// toBeDisabled, a custom matcher from jest-dom.
167+
expect(screen.getByRole('button')).not.toBeDisabled()
166168
```
167169

168170
### System Under Test

0 commit comments

Comments
 (0)