@@ -159,6 +159,11 @@ elements by `data-cy`, a "test ID" convention mentioned in the
159159[ Cypress.io] ( https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements )
160160documentation.
161161
162+ <Tabs groupId = " test-utils" defaultValue = " js" values = { [ {label: ' JavaScript' ,
163+ value: ' js' }, {label: ' TypeScript' , value: ' ts' }, ]} >
164+
165+ <TabItem value = " js" >
166+
162167``` js title="custom-queries.js"
163168import {queryHelpers , buildQueries } from ' @testing-library/react'
164169
@@ -191,6 +196,54 @@ export {
191196}
192197```
193198
199+ </TabItem >
200+
201+ <TabItem value = " ts" >
202+
203+ ``` ts title="custom-queries.ts"
204+ import {
205+ queryHelpers ,
206+ buildQueries ,
207+ Matcher ,
208+ MatcherOptions ,
209+ } from ' @testing-library/react'
210+
211+ // The queryAllByAttribute is a shortcut for attribute-based matchers
212+ // You can also use document.querySelector or a combination of existing
213+ // testing library utilities to find matching nodes for your query
214+ const queryAllByDataCy = (
215+ container : HTMLElement ,
216+ id : Matcher ,
217+ options ? : MatcherOptions | undefined
218+ ) => queryHelpers .queryAllByAttribute (' data-cy' , container , id , options )
219+
220+ const getMultipleError = (c , dataCyValue ) =>
221+ ` Found multiple elements with the data-cy attribute of: ${dataCyValue } `
222+ const getMissingError = (c , dataCyValue ) =>
223+ ` Unable to find an element with the data-cy attribute of: ${dataCyValue } `
224+
225+ const [
226+ queryByDataCy,
227+ getAllByDataCy,
228+ getByDataCy,
229+ findAllByDataCy,
230+ findByDataCy,
231+ ] = buildQueries (queryAllByDataCy , getMultipleError , getMissingError )
232+
233+ export {
234+ queryByDataCy ,
235+ queryAllByDataCy ,
236+ getByDataCy ,
237+ getAllByDataCy ,
238+ findAllByDataCy ,
239+ findByDataCy ,
240+ }
241+ ```
242+
243+ </TabItem >
244+
245+ </Tabs >
246+
194247You can then override and append the new queries via the render function by
195248passing a [ ` queries ` ] ( api.mdx#render-options ) option.
196249
0 commit comments