Skip to content

Add quickstart example #1140

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

Merged
merged 7 commits into from
Aug 31, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/react-testing-library/example-intro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,65 @@ title: Example
sidebar_label: Example
---

## Quickstart

This is a very simple setup to get you started.
If you want to see a description of what each line does,
scroll down to the [annotated version](#quickstart-annotated).
Scroll down to [Full Example](#full-example) to see a more advanced test setup.

```jsx
import {render, fireEvent, waitFor, screen} from '@testing-library/react'
import '@testing-library/jest-dom'
import Fetch from '../fetch'

test('loads and displays greeting', async () => {

// ARRANGE
render(<Fetch url="/greeting" />)

// ACT
fireEvent.click(screen.getByText('Load Greeting'))
await waitFor(() => screen.getByRole('heading'))

// ASSERT
expect(screen.getByRole('heading')).toHaveTextContent('hello there')
expect(screen.getByRole('button')).toBeDisabled()

})
```

<details id="quickstart-annotated">
<summary>Quickstart (Annotated Example)</summary>

```jsx
// import react-testing methods
import {render, fireEvent, waitFor, screen} from '@testing-library/react'
// add custom jest matchers from jest-dom
import '@testing-library/jest-dom'
// the component to test
import Fetch from '../fetch'

test('loads and displays greeting', async () => {

// Render a React element into the DOM
render(<Fetch url="/greeting" />)

fireEvent.click(screen.getByText('Load Greeting'))
// getByRole throws an error if it cannot find an element
await waitFor(() => screen.getByRole('heading'))

// assert that the alert message is correct using
// toHaveTextContent, a custom matcher from jest-dom.
expect(screen.getByRole('heading')).toHaveTextContent('hello there')
expect(screen.getByRole('button')).toBeDisabled()

})
```

</details>


## Full Example

See the following sections for a detailed breakdown of the test
Expand Down