Skip to content

Commit 5bca922

Browse files
Update setup.mdx (#1191)
* Update setup.mdx Add customized `screen` and `within` for using custom queries globally. * Update docs/react-testing-library/setup.mdx Co-authored-by: Tim Deschryver <[email protected]> * docs: make customScreen from allQueries * docs: update the ts part of customScreen * docs: reorder the exports for customRender Co-authored-by: Tim Deschryver <[email protected]>
1 parent 540991f commit 5bca922

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

docs/react-testing-library/setup.mdx

+30-8
Original file line numberDiff line numberDiff line change
@@ -247,44 +247,66 @@ export {
247247
You can then override and append the new queries via the render function by
248248
passing a [`queries`](api.mdx#render-options) option.
249249

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:
252252

253253
<Tabs groupId="test-utils" defaultValue="jsx" values={[ {label: 'Javascript',
254254
value: 'jsx'}, {label: 'Typescript', value: 'tsx'}, ]}>
255255

256256
<TabItem value="jsx">
257257

258258
```js title="test-utils.js"
259-
import {render, queries} from '@testing-library/react'
259+
import {render, queries, within} from '@testing-library/react'
260260
import * as customQueries from './custom-queries'
261261

262+
const allQueries = {
263+
...queries,
264+
...customQueries,
265+
}
266+
267+
const customScreen = within(document.body, allQueries)
268+
const customWithin = element => within(element, allQueries)
262269
const customRender = (ui, options) =>
263-
render(ui, {queries: {...queries, ...customQueries}, ...options})
270+
render(ui, {queries: allQueries, ...options})
264271

265272
// re-export everything
266273
export * from '@testing-library/react'
267274

268275
// override render method
269-
export {customRender as render}
276+
export {
277+
customScreen as screen,
278+
customWithin as within,
279+
customRender as render,
280+
}
270281
```
271282

272283
</TabItem>
273284

274285
<TabItem value="tsx">
275286

276287
```ts title="test-utils.ts"
277-
import {render, queries, RenderOptions} from '@testing-library/react'
288+
import {render, queries, within, RenderOptions} from '@testing-library/react'
278289
import * as customQueries from './custom-queries'
279290
import {ReactElement} from 'react'
280291

292+
const allQueries = {
293+
...queries,
294+
...customQueries,
295+
}
296+
297+
const customScreen = within(document.body, allQueries)
298+
const customWithin = (element: ReactElement) => within(element, allQueries)
281299
const customRender = (
282300
ui: ReactElement,
283301
options?: Omit<RenderOptions, 'queries'>,
284-
) => render(ui, {queries: {...queries, ...customQueries}, ...options})
302+
) => render(ui, {queries: allQueries, ...options})
285303

286304
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+
}
288310
```
289311

290312
</TabItem>

0 commit comments

Comments
 (0)