Skip to content

Commit d0e92f8

Browse files
kierenhughesalexkrolick
authored andcommitted
docs: add custom queries example to custom render docs (#152)
* docs: add custom queries example to custom render docs * docs: add buildQueries to helpers API docs * docs: add example usage of buildQueries to custom queries example * formatting/wording tweaks * make 'add custom queries' a subheading * update link to custom query section
1 parent 4253cbe commit d0e92f8

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

docs/dom-testing-library/api-helpers.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ module.exports = {
5656
> adding `queries` to the options config object. See the render
5757
> [options](/docs/react-testing-library/api#render-options).
5858
59+
## `buildQueries`
60+
61+
The `buildQueries` helper allows you to create custom queres with all standard [variants](api-queries.md) of queries in testing-library.
62+
63+
See the [Add custom queries](/docs/react-testing-library/setup#add-custom-queries) section of the custom render guide for example usage.
64+
5965
### Using other assertion libraries
6066

6167
If you're not using jest, you may be able to find a similar set of custom

docs/react-testing-library/setup.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import defaultStrings from 'i18n/en-x-default'
9696

9797
const AllTheProviders = ({ children }) => {
9898
return (
99-
<ThemeProvider theme="light">
99+
<ThemeProvider theme='light'>
100100
<TranslationProvider messages={defaultStrings}>
101101
{children}
102102
</TranslationProvider>
@@ -144,6 +144,91 @@ module.exports = {
144144

145145
</details>
146146

147+
### Add custom queries
148+
149+
> **Note**
150+
>
151+
> Generally you should not need to create custom attributes for
152+
> react-testing-library. Where you do use it, you should consider whether your
153+
> new queries encourage you to test in a user-centric way, without testing
154+
> implementation details.
155+
156+
You can define your own custom queries as described in the example in the
157+
[Helpers API](/doc/dom-testing-library/api-helpers.md) documentation, or via the
158+
[`buildQueries`](/doc/dom-testing-library/api-helpers#buildQueries) helper. Then
159+
you can use the in any render call using the `queries` option. To make the
160+
custom queries available globally, you can add them to your custom render method
161+
as shown below.
162+
163+
In the example below, a new set of query variants are created for getting
164+
elements by `data-cy`, a "test ID" convention mentioned in the
165+
[Cypress.io](https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements)
166+
documentation.
167+
168+
```js
169+
// custom-queries.js
170+
import { queryHelpers, buildQueries } from '@testing-library/react'
171+
172+
// The queryAllByAttribute is a shortcut for attribute-based matchers
173+
// You can also use document.querySelector or a combination of existing
174+
// testing library utilities to find matching nodes for your query
175+
const queryAllByDataCy = (...args) =>
176+
queryHelpers.queryAllByAttribute('data-cy', ...args)
177+
178+
const getMultipleError = (c, dataCyValue) =>
179+
`Found multiple elements with the data-cy attribute of: ${dataCyValue}`
180+
const getMissingError = (c, dataCyValue) =>
181+
`Unable to find an element with the data-cy attribute of: ${dataCyValue}`
182+
183+
const [
184+
queryByDataCy,
185+
getAllByDataCy,
186+
getByDataCy,
187+
findAllByDataCy,
188+
findByDataCy,
189+
] = buildQueries(queryAllByDataCy, getMultipleError, getMissingError)
190+
191+
export {
192+
queryByDataCy,
193+
queryAllByDataCy,
194+
getByDataCy,
195+
getAllByDataCy,
196+
findAllByDataCy,
197+
findByDataCy,
198+
}
199+
```
200+
201+
You can then override and append the new queries via the render function by
202+
passing a [`queries`](api.md#render-options) option.
203+
204+
If you want to add custom queries globally, you can do this by defining a custom
205+
render method:
206+
207+
```js
208+
// test-utils.js
209+
import { render, queries, queryHelpers } from '@testing-library/react'
210+
import * as customQueries from './custom-queries'
211+
212+
const customRender = (ui, options) =>
213+
render(ui, { queries: { ...queries, ...customQueries } })
214+
215+
// re-export everything
216+
export * from '@testing-library/react'
217+
218+
// override render method
219+
export { customRender as render }
220+
```
221+
222+
You can then use your custom queries as you would any other query:
223+
224+
```js
225+
const { getByDataCy } = render(<Component />)
226+
227+
expect(getByDataCy('my-component')).toHaveTextContent('Hello')
228+
```
229+
230+
</details>
231+
147232
### Configuring Jest with Test Utils
148233

149234
To make your custom test file accessible in your Jest test files without using

0 commit comments

Comments
 (0)