Skip to content

Commit ddbc409

Browse files
alexkrolickKent C. Dodds
authored and
Kent C. Dodds
committed
docs: implement data-test-id query using helpers (#209)
<!-- Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated! Please make sure that you are familiar with and follow the Code of Conduct for this project (found in the CODE_OF_CONDUCT.md file). Also, please make sure you're familiar with and follow the instructions in the contributing guidelines (found in the CONTRIBUTING.md file). If you're new to contributing to open source projects, you might find this free video course helpful: http://kcd.im/pull-request Please fill out the information below to expedite the review and (hopefully) merge of your pull request! --> <!-- What changes are being made? (What feature/bug is being fixed here?) --> **What**: <!-- Why are these changes necessary? --> **Why**: <!-- How were these changes implemented? --> **How**: <!-- Have you done all of these things? --> **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" --> - [ ] Documentation - [ ] Tests - [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? --> - [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions --> <!-- feel free to add additional comments -->
1 parent 3cc387b commit ddbc409

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -572,20 +572,35 @@ What you can do is to create a [custom render](#custom-render) that overwrites
572572
573573
```js
574574
// test-utils.js
575-
import {render} from 'react-testing-library'
575+
import {render, queryHelpers} from 'react-testing-library'
576+
577+
export const queryByTestId = queryHelpers.queryByAttribute.bind(null, 'data-test-id')
578+
export const queryAllByTestId = queryHelpers.queryAllByAttribute.bind(null, 'data-test-id')
579+
580+
export function getAllByTestId(container, id, ...rest) {
581+
const els = queryAllByTestId(container, id, ...rest)
582+
if (!els.length) {
583+
throw getElementError(
584+
`Unable to find an element by: [data-test-id="${id}"]`,
585+
container,
586+
)
587+
}
588+
return els
589+
}
590+
591+
export function getByTestId(...args) {
592+
return queryHelpers.firstResultOrNull(getAllByTestId, ...args)
593+
}
576594

577595
const customRender = (node, ...options) => {
578596
const utils = render(node, ...options)
579597

580598
return {
581599
...utils,
582-
getByTestId: id => {
583-
const el = utils.container.querySelector(`[data-test-id="${id}"]`)
584-
if (!el) {
585-
throw Error(`Unable to find an element by: [data-test-id="${id}"]`)
586-
}
587-
return el
588-
},
600+
getByTestId: getByTestId.bind(container),
601+
getAllByTestId: getAllByTestId.bind(container),
602+
queryByTestId: queryByTestId.bind(container),
603+
queryAllByTestId: queryAllByTestId.bind(container),
589604
}
590605
}
591606

0 commit comments

Comments
 (0)