Skip to content

Commit 5f81c5e

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

File tree

1 file changed

+58
-13
lines changed

1 file changed

+58
-13
lines changed

docs/react-testing-library/setup.md

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,31 +147,76 @@ 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+
`data-testid` is supported by Cypress.io, so even in this case, you would not technically
159+
need to create a new set of queries and you could just replace `data-cy` with
160+
`data-testid` attributes throughout your project.
161+
162+
You can define your own custom queries as described in the example in the
163+
[Helpers API](/doc/dom-testing-library/api-helpers.md) documentation, or via the
164+
[`buildQueries`](/doc/dom-testing-library/api-helpers#buildQueries) helper:
155165

156166
```js
157-
// test-utils.js
158-
import { render, queries, queryHelpers } from '@testing-library/react'
167+
// custom-queries.js
168+
import { queryHelpers, buildQueries } from '@testing-library/react';
169+
170+
const queryAllByDataCy = (...args) =>
171+
queryHelpers.queryAllByAttribute('data-cy', ...args);
172+
173+
const getMultipleError = (c, dataCyValue) => (
174+
`Found multiple elements with the data-cy attribute of: ${dataCyValue}`
175+
);
176+
const getMissingError = (c, dataCyValue) => (
177+
`Unable to find an element with the data-cy attribute of: ${dataCyValue}`
178+
);
179+
180+
const [
181+
queryByDataCy,
182+
getAllByDataCy,
183+
getByDataCy,
184+
findAllByDataCy,
185+
findByDataCy
186+
] = buildQueries(
187+
queryAllByDataCy,
188+
getMultipleError,
189+
getMissingError,
190+
);
159191

160-
const customQueries = {
161-
getByDataCy: queryHelpers.queryByAttribute.bind(
162-
null,
163-
'data-cy'
164-
),
192+
export {
193+
queryByDataCy,
194+
queryAllByDataCy,
195+
getByDataCy,
196+
getAllByDataCy,
197+
findAllByDataCy,
198+
findByDataCy,
165199
};
200+
```
201+
202+
You can then override and append queries via the render function by passing a
203+
[`queries`](api.md#render-options) option.
204+
205+
If you want to add custom queries globally, you can do this by defining a custom 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';
166211

167212
const customRender = (ui, options) =>
168-
render(ui, { queries: { ...queries, ...customQueries } })
213+
render(ui, { queries: { ...queries, ...customQueries } });
169214

170215
// re-export everything
171-
export * from '@testing-library/react'
216+
export * from '@testing-library/react';
172217

173218
// override render method
174-
export { customRender as render }
219+
export { customRender as render };
175220
```
176221

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

0 commit comments

Comments
 (0)