Skip to content

Commit 3d55c96

Browse files
committed
document within/custom queries
1 parent a98c2d7 commit 3d55c96

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

docs/api-helpers.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ module.exports = {
5050
}
5151
```
5252

53+
> **Note**
54+
>
55+
> Custom queries can be added to `react-testing-library`'s `render` method by
56+
> adding `queries` to the options config object. See the render
57+
> [options](/docs/react-testing-library/api#render-options).
58+
5359
### Using other assertion libraries
5460

5561
If you're not using jest, you may be able to find a similar set of custom
@@ -105,13 +111,37 @@ in use in `react-testing-library` and `vue-testing-library`.
105111
Example: To get the text 'hello' only within a section called 'messages', you
106112
could do:
107113

108-
```javascript
114+
<!--DOCUSAURUS_CODE_TABS-->
115+
116+
<!--Native-->
117+
118+
```js
109119
import { within } from 'dom-testing-library'
110120

111121
const { getByText } = within(document.getElementById('messages'))
112122
const helloMessage = getByText('hello')
113123
```
114124

125+
<!--React-->
126+
127+
```js
128+
import { render, within } from 'react-testing-library'
129+
130+
const { getByLabelText } = render(<MyComponent />)
131+
const signinModal = getByLabelText('Sign In')
132+
within(signinModal).getByPlaceholderText('Username')
133+
```
134+
135+
<!--Cypress-->
136+
137+
```js
138+
cy.get('form').within(() => {
139+
cy.getByText('Button Text').should('be.disabled')
140+
})
141+
```
142+
143+
<!--END_DOCUSAURUS_CODE_TABS-->
144+
115145
## Debugging
116146

117147
When you use any `get` calls in your test cases, the current state of the

docs/react-testing-library/api.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ Pass a React Component as the `wrapper` option to have it rendered around the
9292
inner element. This is most useful for creating reusable custom render functions
9393
for common data providers. See [setup](setup.md#custom-render) for examples.
9494

95+
### `queries`
96+
97+
Queries to bind. Overrides the default set from `dom-testing-library` unless
98+
merged.
99+
100+
```js
101+
// Example, a function to traverse table contents
102+
import * as tableQueries from 'my-table-query-libary'
103+
import queries from 'react-testing-library'
104+
105+
const { getByRowColumn, getByText } = render(<MyTable />, {
106+
queries: { ...queries, ...tableQueries },
107+
})
108+
```
109+
110+
See [helpers](../api-helpers.md) for guidance on using utility functions to
111+
create custom queries.
112+
113+
Custom queries can also be added globally by following the
114+
[custom render guide](./setup#custom-render).
115+
95116
## `render` Result
96117

97118
The `render` method returns an object that has a few properties:

0 commit comments

Comments
 (0)