@@ -96,7 +96,7 @@ import defaultStrings from 'i18n/en-x-default'
96
96
97
97
const AllTheProviders = ({ children }) => {
98
98
return (
99
- < ThemeProvider theme= " light" >
99
+ < ThemeProvider theme= ' light' >
100
100
< TranslationProvider messages= {defaultStrings}>
101
101
{children}
102
102
< / TranslationProvider>
@@ -144,6 +144,91 @@ module.exports = {
144
144
145
145
</details >
146
146
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
+
147
232
### Configuring Jest with Test Utils
148
233
149
234
To make your custom test file accessible in your Jest test files without using
0 commit comments