Skip to content

Commit 516945a

Browse files
committed
docs: add example usage of buildQueries to custom queries example
1 parent 93a6e42 commit 516945a

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

docs/react-testing-library/setup.md

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,31 +147,72 @@ module.exports = {
147147
<details>
148148
<summary>Customize queries globally with custom render</summary>
149149

150-
You can override and append queries via the render function by passing a [`queries`](api.md#render-options) option.
150+
In the example below, a new set of query variants are created for getting
151+
elements by `data-cy`, an attribute used and recommended by (Cypress.io)[https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements].
151152

152-
You can define your own custom queries as described in the example in the [Helpers API](api-helpers.md) documentation.
153+
It is worth noting that generally you should not need to use this feature of
154+
react-testing-library and, where you do use it, you should really consider whether
155+
your new queries encourage you to test in a user-centric way, without testing
156+
implementation detail.
153157

154-
If you want to add custom queries globally, you can do this by defining a custom render method:
158+
You can define your own custom queries as described in the example in the
159+
[Helpers API](/doc/dom-testing-library/api-helpers.md) documentation, or via the
160+
[`buildQueries`](/doc/dom-testing-library/api-helpers#buildQueries) helper:
155161

156162
```js
157-
// test-utils.js
158-
import { render, queries, queryHelpers } from '@testing-library/react'
163+
// custom-queries.js
164+
import { queryHelpers, buildQueries } from '@testing-library/react';
165+
166+
const queryAllByDataCy = (...args) =>
167+
queryHelpers.queryAllByAttribute('data-cy', ...args);
159168

160-
const customQueries = {
161-
getByDataCy: queryHelpers.queryByAttribute.bind(
162-
null,
163-
'data-cy'
164-
),
169+
const getMultipleError = (c, dataCyValue) => (
170+
`Found multiple elements with the data-cy attribute of: ${dataCyValue}`
171+
);
172+
const getMissingError = (c, dataCyValue) => (
173+
`Unable to find an element with the data-cy attribute of: ${dataCyValue}`
174+
);
175+
176+
const [
177+
queryByDataCy,
178+
getAllByDataCy,
179+
getByDataCy,
180+
findAllByDataCy,
181+
findByDataCy
182+
] = buildQueries(
183+
queryAllByDataCy,
184+
getMultipleError,
185+
getMissingError,
186+
);
187+
188+
export {
189+
queryByDataCy,
190+
queryAllByDataCy,
191+
getByDataCy,
192+
getAllByDataCy,
193+
findAllByDataCy,
194+
findByDataCy,
165195
};
196+
```
197+
198+
You can then override and append queries via the render function by passing a
199+
[`queries`](api.md#render-options) option.
200+
201+
If you want to add custom queries globally, you can do this by defining a custom render method:
202+
203+
```js
204+
// test-utils.js
205+
import { render, queries, queryHelpers } from '@testing-library/react';
206+
import * as customQueries from './custom-queries';
166207

167208
const customRender = (ui, options) =>
168-
render(ui, { queries: { ...queries, ...customQueries } })
209+
render(ui, { queries: { ...queries, ...customQueries } });
169210

170211
// re-export everything
171-
export * from '@testing-library/react'
212+
export * from '@testing-library/react';
172213

173214
// override render method
174-
export { customRender as render }
215+
export { customRender as render };
175216
```
176217

177218
You can then use your custom queries as you would any other query:

0 commit comments

Comments
 (0)