Skip to content

Commit 7ad4b27

Browse files
authored
Merge branch 'main' into all-contributors/add-vadimshvetsov
2 parents dd28911 + e862067 commit 7ad4b27

File tree

25 files changed

+1306
-413
lines changed

25 files changed

+1306
-413
lines changed

.all-contributorsrc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,10 +2610,39 @@
26102610
"infra"
26112611
]
26122612
},
2613+
{
2614+
"login": "agentdylan",
2615+
"name": "Dylan Gordon",
2616+
"avatar_url": "https://avatars.githubusercontent.com/u/3656794?v=4",
2617+
"profile": "https://www.dylangordon.co.nz/",
2618+
"contributions": [
2619+
"doc"
2620+
]
2621+
},
2622+
{
2623+
"login": "mcous",
2624+
"name": "Michael Cousins",
2625+
"avatar_url": "https://avatars.githubusercontent.com/u/2963448?v=4",
2626+
"profile": "https://michael.cousins.io",
2627+
"contributions": [
2628+
"doc",
2629+
"review"
2630+
]
2631+
},
2632+
{
2633+
"login": "kettanaito",
2634+
"name": "Artem Zakharchenko",
2635+
"avatar_url": "https://avatars.githubusercontent.com/u/14984911?v=4",
2636+
"profile": "https://kettanaito.com",
2637+
"contributions": [
2638+
"doc"
2639+
]
2640+
},
26132641
{
26142642
"login": "vadimshvetsov",
26152643
"name": "Vadim Shvetsov",
26162644
"avatar_url": "https://avatars.githubusercontent.com/u/16336572?v=4",
2645+
"profile": "https://github.com/vadimshvetsov",
26172646
"contributions": [
26182647
"doc"
26192648
]

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,10 @@ Thanks goes to these wonderful people
506506
</tr>
507507
<tr>
508508
<td align="center" valign="top" width="14.28%"><a href="https://github.com/smk267"><img src="https://avatars.githubusercontent.com/u/88115182?v=4?s=100" width="100px;" alt="smk267"/><br /><sub><b>smk267</b></sub></a><br /><a href="#infra-smk267" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
509-
<td align="center" valign="top" width="14.28%"><a href="https://proling.ru"><img src="https://avatars.githubusercontent.com/u/16336572?v=4?s=100" width="100px;" alt="Vadim Shvetsov"/><br /><sub><b>Vadim Shvetsov</b></sub></a><br /><a href="https://github.com/testing-library/testing-library-docs/commits?author=vadimshvetsov" title="Documentation">📖</a></td>
509+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/vadimshvetsov"><img src="https://avatars.githubusercontent.com/u/16336572?v=4?s=100" width="100px;" alt="Vadim Shvetsov"/><br /><sub><b>Vadim Shvetsov</b></sub></a><br /><a href="https://github.com/testing-library/testing-library-docs/commits?author=vadimshvetsov" title="Documentation">📖</a></td>
510+
<td align="center" valign="top" width="14.28%"><a href="https://www.dylangordon.co.nz/"><img src="https://avatars.githubusercontent.com/u/3656794?v=4?s=100" width="100px;" alt="Dylan Gordon"/><br /><sub><b>Dylan Gordon</b></sub></a><br /><a href="https://github.com/testing-library/testing-library-docs/commits?author=agentdylan" title="Documentation">📖</a></td>
511+
<td align="center" valign="top" width="14.28%"><a href="https://michael.cousins.io"><img src="https://avatars.githubusercontent.com/u/2963448?v=4?s=100" width="100px;" alt="Michael Cousins"/><br /><sub><b>Michael Cousins</b></sub></a><br /><a href="https://github.com/testing-library/testing-library-docs/commits?author=mcous" title="Documentation">📖</a> <a href="https://github.com/testing-library/testing-library-docs/pulls?q=is%3Apr+reviewed-by%3Amcous" title="Reviewed Pull Requests">👀</a></td>
512+
<td align="center" valign="top" width="14.28%"><a href="https://kettanaito.com"><img src="https://avatars.githubusercontent.com/u/14984911?v=4?s=100" width="100px;" alt="Artem Zakharchenko"/><br /><sub><b>Artem Zakharchenko</b></sub></a><br /><a href="https://github.com/testing-library/testing-library-docs/commits?author=kettanaito" title="Documentation">📖</a></td>
510513
</tr>
511514
</tbody>
512515
</table>

docs/angular-testing-library/api.mdx

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,42 @@ await render(AppComponent, {
150150
})
151151
```
152152

153+
### `deferBlockBehavior`
154+
155+
Set the defer blocks behavior.
156+
157+
For more info see the
158+
[Angular docs](https://angular.io/api/core/testing/DeferBlockBehavior)
159+
160+
**default** : `undefined` (uses the Angular default, which is
161+
`DeferBlockBehavior.Manual`)
162+
163+
**example**:
164+
165+
```typescript
166+
await render(AppComponent, {
167+
deferBlockBehavior: DeferBlockBehavior.Playthrough,
168+
})
169+
```
170+
171+
### `deferBlockStates`
172+
173+
Set the initial state of a deferrable blocks in a component.
174+
175+
For more info see the
176+
[Angular docs](https://angular.io/api/core/testing/DeferBlockState)
177+
178+
**default** : `undefined` (uses the Angular default, which is
179+
`DeferBlockState.Placeholder`)
180+
181+
**example**:
182+
183+
```typescript
184+
await render(FixtureComponent, {
185+
deferBlockStates: DeferBlockState.Loading,
186+
})
187+
```
188+
153189
### `componentProviders`
154190

155191
A collection of providers needed to render the component via Dependency
@@ -395,20 +431,39 @@ properties that are not defined are cleared.
395431

396432
```typescript
397433
const {rerender} = await render(Counter, {
398-
componentProperties: {count: 4, name: 'Sarah'},
434+
componentInputs: {count: 4, name: 'Sarah'},
399435
})
400436
401437
expect(screen.getByTestId('count-value').textContent).toBe('4')
402438
expect(screen.getByTestId('name-value').textContent).toBe('Sarah')
403439
404-
await rerender({componentProperties: {count: 7}})
440+
await rerender({componentInputs: {count: 7}})
405441
406-
// count updated to 7
442+
// count is updated to 7
407443
expect(screen.getByTestId('count-value').textContent).toBe('7')
408444
// name is undefined because it's not provided in rerender
409445
expect(screen.getByTestId('name-value').textContent).toBeUndefined()
410446
```
411447

448+
Using `partialUpdate`, only the newly provided properties will be updated. Other
449+
input properties that aren't provided won't be cleared.
450+
451+
```typescript
452+
const {rerender} = await render(Counter, {
453+
componentInputs: {count: 4, name: 'Sarah'},
454+
})
455+
456+
expect(screen.getByTestId('count-value').textContent).toBe('4')
457+
expect(screen.getByTestId('name-value').textContent).toBe('Sarah')
458+
459+
await rerender({componentInputs: {count: 7}, partialUpdate: true})
460+
461+
// count is updated to 7
462+
expect(screen.getByTestId('count-value').textContent).toBe('7')
463+
// name is still rendered as "Sarah" because of the partial update
464+
expect(screen.getByTestId('name-value').textContent).toBe('Sarah')
465+
```
466+
412467
### `detectChanges`
413468

414469
Trigger a change detection cycle for the component.
@@ -473,3 +528,22 @@ screen.getByRole('heading', {
473528
})
474529
queryByLabelText(/First name/i')
475530
```
531+
532+
### `renderDeferBlock`
533+
534+
To test [Deferrable views](https://angular.dev/guide/defer#defer), you can make
535+
use of `renderDeferBlock`. `renderDeferBlock` will set the desired defer state
536+
for a specific deferrable block. The default value of a deferrable view is
537+
`Placeholder`, but you can also set the initial state while rendering the
538+
component.
539+
540+
```typescript
541+
const {renderDeferBlock} = await render(FixtureComponent, {
542+
deferBlockStates: DeferBlockState.Loading,
543+
})
544+
545+
expect(screen.getByText(/loading/i)).toBeInTheDocument()
546+
547+
await renderDeferBlock(DeferBlockState.Complete)
548+
expect(screen.getByText(/completed/i)).toBeInTheDocument()
549+
```

docs/angular-testing-library/version-compatibility.mdx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Library and Angular.
88

99
| Angular | Angular Testing Library |
1010
| ------- | ----------------------- |
11-
| 16.x | 13.x \|\| 14.x |
12-
| >= 15.1 | 13.x \|\| 14.x |
13-
| < 15.1 | 11.x \|\| 12.x |
14-
| 14.x | 11.x \|\| 12.x |
11+
| 17.x | 16.x, 15.x, 14.x, 13.x |
12+
| 16.x | 14.x, 13.x |
13+
| >= 15.1 | 14.x, 13.x |
14+
| < 15.1 | 12.x, 11.x |
15+
| 14.x | 12.x, 11.x |

docs/dom-testing-library/install.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ install the wrapper:
2828
- [Puppeteer Testing Library](pptr-testing-library/intro.mdx)
2929
- [Testcafe Testing Library](testcafe-testing-library/intro.mdx)
3030
- [Nightwatch Testing Library](nightwatch-testing-library/intro.mdx)
31+
- [Solid Testing Library](solid-testing-library/intro.mdx)
3132

3233
## Ecosystem
3334

docs/queries/about.mdx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,9 @@ can contain options that affect the precision of string matching:
317317

318318
- `exact`: Defaults to `true`; matches full strings, case-sensitive. When false,
319319
matches substrings and is not case-sensitive.
320-
- `exact` has no effect on `regex` or `function` arguments.
321-
- `exact` has no effect on accessible names specified with the `name` option
322-
of `*byRole` queries. You can use a regex for fuzzy matching on accessible
323-
names.
324-
- In most cases using a regex instead of a string gives you more control over
325-
fuzzy matching and should be preferred over `{ exact: false }`.
320+
- it has no effect when used together with `regex` or `function` arguments.
321+
- in most cases, using a regex instead of a string combined with `{ exact: false }`
322+
gives you more control over fuzzy matching so it should be preferred.
326323
- `normalizer`: An optional function which overrides normalization behavior. See
327324
[`Normalization`](#normalization).
328325

docs/queries/byrole.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,20 @@ You can query a specific element like this
462462
```js
463463
getByRole('alertdialog', {description: 'Your session is about to expire'})
464464
```
465+
466+
## Performance
467+
468+
`getByRole` is the most preferred query to use as it most closely resembles the
469+
user experience, however the calculations it must perform to provide this
470+
confidence can be expensive (particularly with large DOM trees).
471+
472+
Where test performance is a concern it may be desirable to trade some of this
473+
confidence for improved performance.
474+
475+
`getByRole` performance can be improved by setting the option
476+
[`hidden`](#hidden) to `true` and thereby avoid expensive visibility checks.
477+
Note that in doing so inaccessible elements will now be included in the result.
478+
479+
Another option may be to substitute `getByRole` for simpler `getByLabelText` and
480+
`getByText` queries which can be significantly faster though less robust
481+
alternatives.

docs/react-testing-library/api.mdx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ as these methods:
3232
- [`result`](#result)
3333
- [`rerender`](#rerender-1)
3434
- [`unmount`](#unmount-1)
35+
- [`configure`](#configure)
36+
- [`configure` Options](#configure-options)
3537

3638
---
3739

@@ -105,6 +107,12 @@ your components.
105107

106108
### `legacyRoot`
107109

110+
:::warning
111+
112+
This option is only available when tests run with React 18 and earlier.
113+
114+
:::
115+
108116
By default we'll render with support for concurrent features (i.e.
109117
[`ReactDOMClient.createRoot`](https://reactjs.org/docs/react-dom-client.html#createroot)).
110118
However, if you're dealing with a legacy app that requires rendering like in
@@ -338,7 +346,7 @@ function renderHook<
338346
Props,
339347
Q extends Queries = typeof queries,
340348
Container extends Element | DocumentFragment = HTMLElement,
341-
BaseElement extends Element | DocumentFragment = Container,
349+
BaseElement extends Element | DocumentFragment = Container
342350
>(
343351
render: (initialProps: Props) => Result,
344352
options?: RenderHookOptions<Props, Q, Container, BaseElement>,
@@ -451,3 +459,23 @@ const {unmount} = renderHook(({name = 'Alice'} = {}) => name)
451459

452460
unmount()
453461
```
462+
463+
## `configure`
464+
465+
Changes global options. Basic usage can be seen at
466+
[Configuration Options](dom-testing-library/api-configuration.mdx).
467+
468+
React Testing Library also has dedicated options.
469+
470+
```typescript
471+
import {configure} from '@testing-library/react'
472+
473+
configure({reactStrictMode: true})
474+
```
475+
476+
## `configure` Options
477+
478+
### `reactStrictMode`
479+
480+
When enabled, [`<StrictMode>`](https://react.dev/reference/react/StrictMode) is
481+
rendered around the inner element. Defaults to `false`.

docs/react-testing-library/cheatsheet.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ See [Events API](dom-testing-library/api-events.mdx)
131131
## Other
132132

133133
See [Querying Within Elements](dom-testing-library/api-within.mdx),
134-
[Config API](dom-testing-library/api-configuration.mdx),
134+
[Config API](react-testing-library/api.mdx#configure),
135135
[Cleanup](react-testing-library/api.mdx#cleanup),
136136

137137
- **within** take a node and return an object with all the queries bound to the

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,24 @@ test('loads and displays greeting', async () => {
6565

6666
See the following sections for a detailed breakdown of the test
6767

68+
:::note
69+
We recommend using the
70+
[Mock Service Worker (MSW)](https://github.com/mswjs/msw) library to
71+
declaratively mock API communication in your tests instead of stubbing
72+
`window.fetch`, or relying on third-party adapters.
73+
:::
74+
6875
```jsx title="__tests__/fetch.test.jsx"
6976
import React from 'react'
70-
import {rest} from 'msw'
77+
import {http, HttpResponse} from 'msw'
7178
import {setupServer} from 'msw/node'
7279
import {render, fireEvent, screen} from '@testing-library/react'
7380
import '@testing-library/jest-dom'
7481
import Fetch from '../fetch'
7582

7683
const server = setupServer(
77-
rest.get('/greeting', (req, res, ctx) => {
78-
return res(ctx.json({greeting: 'hello there'}))
84+
http.get('/greeting', () => {
85+
return HttpResponse.json({greeting: 'hello there'})
7986
}),
8087
)
8188

@@ -96,8 +103,8 @@ test('loads and displays greeting', async () => {
96103

97104
test('handles server error', async () => {
98105
server.use(
99-
rest.get('/greeting', (req, res, ctx) => {
100-
return res(ctx.status(500))
106+
http.get('/greeting', () => {
107+
return new HttpResponse(null, {status: 500})
101108
}),
102109
)
103110

@@ -112,10 +119,6 @@ test('handles server error', async () => {
112119
})
113120
```
114121

115-
> We recommend using [Mock Service Worker](https://github.com/mswjs/msw) library
116-
> to declaratively mock API communication in your tests instead of stubbing
117-
> `window.fetch`, or relying on third-party adapters.
118-
119122
---
120123

121124
## Step-By-Step
@@ -127,7 +130,7 @@ test('handles server error', async () => {
127130
import React from 'react'
128131

129132
// import API mocking utilities from Mock Service Worker
130-
import {rest} from 'msw'
133+
import {http, HttpResponse} from 'msw'
131134
import {setupServer} from 'msw/node'
132135

133136
// import react-testing methods
@@ -156,9 +159,9 @@ component makes.
156159
// declare which API requests to mock
157160
const server = setupServer(
158161
// capture "GET /greeting" requests
159-
rest.get('/greeting', (req, res, ctx) => {
162+
http.get('/greeting', (req, res, ctx) => {
160163
// respond using a mocked JSON body
161-
return res(ctx.json({greeting: 'hello there'}))
164+
return HttpResponse.json({greeting: 'hello there'})
162165
}),
163166
)
164167

@@ -176,8 +179,8 @@ test('handles server error', async () => {
176179
server.use(
177180
// override the initial "GET /greeting" request handler
178181
// to return a 500 Server Error
179-
rest.get('/greeting', (req, res, ctx) => {
180-
return res(ctx.status(500))
182+
http.get('/greeting', (req, res, ctx) => {
183+
return new HttpResponse(null, {status: 500})
181184
}),
182185
)
183186

docs/react-testing-library/intro.mdx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@ sidebar_label: Introduction
77
[`React Testing Library`][gh] builds on top of `DOM Testing Library` by adding
88
APIs for working with React components.
99

10-
> Projects created with [`Create React App`](https://create-react-app.dev/) have
11-
> out of the box support for **React Testing Library**. If that is not the case,
12-
> you can add it via `npm` like so:
10+
## Installation
1311

1412
```bash npm2yarn
1513
npm install --save-dev @testing-library/react
1614
```
1715

18-
- [React Testing Library on GitHub][gh]
16+
### With TypeScript
17+
18+
To get full type coverage, you need to install the types for `react` and `react-dom` as well:
19+
20+
```bash npm2yarn
21+
npm install --save-dev @testing-library/react @types/react-dom @types/react
22+
```
23+
1924

2025
[gh]: https://github.com/testing-library/react-testing-library
2126

0 commit comments

Comments
 (0)