-
Notifications
You must be signed in to change notification settings - Fork 1.1k
docs: ✏️ explain how to implement custo getByTestId #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
README.md
Outdated
@@ -557,6 +557,47 @@ const usernameInputElement = getByTestId('username-input') | |||
> about `data-testid`s from the blog post ["Making your UI tests resilient to | |||
> change"][data-testid-blog-post] | |||
|
|||
<details> | |||
<summary>What if my project already uses <code>data-test-id</code> do I have to migrate to <code>data-testid</code>? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<summary>What if my project already uses <code>data-test-id</code>? Do I have to migrate to <code>data-testid</code>?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super! Thank you!!
return { | ||
...utils, | ||
getByTestId: id => { | ||
const el = utils.container.querySelector(`[data-test-id="${id}"]`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to use the query helpers from dom-testing-library
to allow the custom function to support functions, strings, and regex and the exact
and preserveWhitespace
options in the same way as the other queries:
https://github.com/kentcdodds/dom-testing-library/blob/master/src/query-helpers.js#L36-L47
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could mirror the implementation of the base query:
const queryByTestId = queryByAttribute.bind(null, 'data-test-id')
const queryAllByTestId = queryAllByAttribute.bind(null, 'data-test-id')
function getAllByTestId(container, id, ...rest) {
const els = queryAllByTestId(container, id, ...rest)
if (!els.length) {
throw getElementError(
`Unable to find an element by: [data-test-id="${id}"]`,
container,
)
}
return els
}
function getByTestId(...args) {
return firstResultOrNull(getAllByTestId, ...args)
}
https://github.com/kentcdodds/dom-testing-library/blob/master/src/queries.js#L153-L154
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, feel fee to make another PR to improve that :) Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the query helpers in a firs implementation but I was worried the example would go out of sync if the internals for dom-testing-library
changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. If it's an explicitly exported value, then it should be pretty solid. But if you're importing things from dist
then that's probably not safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Query helpers are exported directly for this very use case
https://github.com/kentcdodds/dom-testing-library/blob/master/src/index.js
🎉 This PR is included in version 5.2.2 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
…ab-respect-radio-groups fix(tab): make tab respect radio groups (fix testing-library#207)
What:
Adding an explanation on how to implement a
getByTestId
that looks fordata-test-id
.Why:
Because people tend to ask this question.
How:
Checklist: