Skip to content

Commit 93e6ec8

Browse files
RoystonSKent C. Dodds
authored and
Kent C. Dodds
committed
docs: replace data-testid using configure rather than custom render (#230)
**What**: Update documentation to suggest using `configure()` instead of writing a custom render, when needing to override `data-testid`. **Why**: We've just added a handy `configure()` function to `dom-testing-library` to make it easier to do this, so we might as well point it out here instead of recommending the hard way. **How**: Updated `README.md` **Checklist**: <!-- add "N/A" to the end of each line that's irrelevant to your changes --> <!-- to check an item, place an "x" in the box like so: "- [x] Documentation" --> - [x] Documentation - [ ] Tests - N/A - this is only a docs change - [x] Ready to be merged - [ ] Added myself to contributors table - N/A - already there for docs
1 parent f6cfa86 commit 93e6ec8

File tree

1 file changed

+11
-61
lines changed

1 file changed

+11
-61
lines changed

README.md

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -617,70 +617,20 @@ const usernameInputElement = getByTestId('username-input')
617617
> change"][data-testid-blog-post]
618618
619619
<details>
620-
<summary>What if my project already uses <code>data-test-id</code>? Do I have to migrate to <code>data-testid</code>?
620+
<summary>What if my project already uses <code>data-test-id</code> or another attribute?
621+
Do I have to migrate to <code>data-testid</code>?
621622
</summary>
622623
623-
`getByTestId` is looking for the `data-testid` attribute and that's not going to
624-
change. This follows the precedent set by
625-
[React Native Web](https://github.com/kentcdodds/react-testing-library/issues/1)
626-
which uses a `testID` prop which emits a `data-testid` attribute on the element.
624+
If you're starting out with a new codebase, it's recommended that you stick with
625+
<code>data-testid</code>, following the precedent set by
626+
[React Native Web](https://github.com/kentcdodds/react-testing-library/issues/1),
627+
but if you already have a codebase that uses a different attribute for this
628+
purpose, you can use the `configure` function of `dom-testing-library` to change
629+
the attribute that is used. This requires `dom-testing-library` version 3.13:
627630
628-
<!-- prettier-ignore-start -->
629-
[#76](https://github.com/kentcdodds/dom-testing-library/issues/76#issuecomment-406321122)
630-
[#128](https://github.com/kentcdodds/dom-testing-library/issues/128)
631-
[#204](https://github.com/kentcdodds/react-testing-library/issues/204).
632-
<!-- prettier-ignore-end -->
633-
634-
What you can do is to create a [custom render](#custom-render) that overwrites
635-
`getByTestId` and related methods, and also `rerender`:
636-
637-
```js
638-
// test-utils.js
639-
import {render, queryHelpers} from 'react-testing-library'
640-
641-
export const queryByTestId = (...rest) =>
642-
queryHelpers.queryByAttribute('data-test-id', ...rest)
643-
export const queryAllByTestId = (...rest) =>
644-
queryHelpers.queryAllByAttribute('data-test-id', ...rest)
645-
646-
export function getAllByTestId(container, id, ...rest) {
647-
const els = queryAllByTestId(container, id, ...rest)
648-
if (!els.length) {
649-
throw queryHelpers.getElementError(
650-
`Unable to find an element by: [data-test-id="${id}"]`,
651-
container,
652-
)
653-
}
654-
return els
655-
}
656-
657-
export const getByTestId = (...rest) =>
658-
queryHelpers.firstResultOrNull(getAllByTestId, ...rest)
659-
660-
const customRender = (ui, options) => {
661-
const rendered = render(ui, options)
662-
663-
return {
664-
...rendered,
665-
getByTestId: (...rest) => getByTestId(rendered.container, ...rest),
666-
getAllByTestId: (...rest) => getAllByTestId(rendered.container, ...rest),
667-
queryByTestId: (...rest) => queryByTestId(rendered.container, ...rest),
668-
queryAllByTestId: (...rest) =>
669-
queryAllByTestId(rendered.container, ...rest),
670-
671-
rerender: newUi =>
672-
customRender(newUi, {
673-
container: rendered.container,
674-
baseElement: rendered.baseElement,
675-
}),
676-
}
677-
}
678-
679-
// re-export everything
680-
export * from 'react-testing-library'
681-
682-
// override render method
683-
export {customRender as render}
631+
```javascript
632+
import {configure} from 'dom-testing-library'
633+
configure({testIdAttribute: 'data-test-id'})
684634
```
685635
686636
</details>

0 commit comments

Comments
 (0)