Skip to content

Commit e007d84

Browse files
committed
docs: explain API mocking in "React Example"
1 parent 46415a2 commit e007d84

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,22 @@ import { render, fireEvent, waitFor, screen } from '@testing-library/react'
6464
import '@testing-library/jest-dom/extend-expect'
6565
// the component to test
6666
import Fetch from '../fetch'
67+
```
68+
69+
```jsx
70+
test('loads and displays greeting', async () => {
71+
// Arrange
72+
// Act
73+
// Assert
74+
})
75+
```
76+
77+
### Mock
6778

79+
Use the `setupServer` function from `msw` to mock an API request that our tested
80+
component makes.
81+
82+
```js
6883
// declare which API requests to mock
6984
const server = setupServer(
7085
// capture "GET /greeting" requests
@@ -80,14 +95,6 @@ beforeAll(() => server.listen())
8095
afterAll(() => server.close())
8196
```
8297

83-
```jsx
84-
test('loads and displays greeting', async () => {
85-
// Arrange
86-
// Act
87-
// Assert
88-
})
89-
```
90-
9198
### Arrange
9299

93100
The [`render`](./api#render) method renders a React element into the DOM and

0 commit comments

Comments
 (0)