@@ -247,44 +247,66 @@ export {
247
247
You can then override and append the new queries via the render function by
248
248
passing a [ ` queries ` ] ( api.mdx#render-options ) option.
249
249
250
- If you want to add custom queries globally, you can do this by defining a custom
251
- render method :
250
+ If you want to add custom queries globally, you can do this by defining your customized
251
+ ` render ` , ` screen ` and ` within ` methods :
252
252
253
253
<Tabs groupId = " test-utils" defaultValue = " jsx" values = { [ {label: ' Javascript' ,
254
254
value: ' jsx' }, {label: ' Typescript' , value: ' tsx' }, ]} >
255
255
256
256
<TabItem value = " jsx" >
257
257
258
258
``` js title="test-utils.js"
259
- import {render , queries } from ' @testing-library/react'
259
+ import {render , queries , within } from ' @testing-library/react'
260
260
import * as customQueries from ' ./custom-queries'
261
261
262
+ const allQueries = {
263
+ ... queries,
264
+ ... customQueries,
265
+ }
266
+
267
+ const customScreen = within (document .body , allQueries)
268
+ const customWithin = element => within (element, allQueries)
262
269
const customRender = (ui , options ) =>
263
- render (ui, {queries: { ... queries, ... customQueries} , ... options})
270
+ render (ui, {queries: allQueries , ... options})
264
271
265
272
// re-export everything
266
273
export * from ' @testing-library/react'
267
274
268
275
// override render method
269
- export {customRender as render }
276
+ export {
277
+ customScreen as screen ,
278
+ customWithin as within ,
279
+ customRender as render ,
280
+ }
270
281
```
271
282
272
283
</TabItem >
273
284
274
285
<TabItem value = " tsx" >
275
286
276
287
``` ts title="test-utils.ts"
277
- import {render , queries , RenderOptions } from ' @testing-library/react'
288
+ import {render , queries , within , RenderOptions } from ' @testing-library/react'
278
289
import * as customQueries from ' ./custom-queries'
279
290
import {ReactElement } from ' react'
280
291
292
+ const allQueries = {
293
+ ... queries ,
294
+ ... customQueries ,
295
+ }
296
+
297
+ const customScreen = within (document .body , allQueries )
298
+ const customWithin = (element : ReactElement ) => within (element , allQueries )
281
299
const customRender = (
282
300
ui : ReactElement ,
283
301
options ? : Omit <RenderOptions , ' queries' >,
284
- ) => render (ui , {queries: { ... queries , ... customQueries } , ... options })
302
+ ) => render (ui , {queries: allQueries , ... options })
285
303
286
304
export * from ' @testing-library/react'
287
- export {customRender as render }
305
+ export {
306
+ customScreen as screen ,
307
+ customWithin as within ,
308
+ customRender as render ,
309
+ }
288
310
```
289
311
290
312
</TabItem >
0 commit comments