Skip to content

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

Merged
merged 1 commit into from
Oct 23, 2018

Conversation

Gpx
Copy link
Member

@Gpx Gpx commented Oct 23, 2018

What:

Adding an explanation on how to implement a getByTestId that looks for data-test-id.

Why:

Because people tend to ask this question.

How:

Checklist:

  • Documentation
  • Tests N/A
  • Ready to be merged
  • Added myself to contributors table

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>?
Copy link
Collaborator

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>?

Copy link
Member

@kentcdodds kentcdodds left a 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}"]`)
Copy link
Collaborator

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

Copy link
Collaborator

@alexkrolick alexkrolick Oct 23, 2018

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

Copy link
Member

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!

Copy link
Member Author

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

Copy link
Member

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.

Copy link
Collaborator

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

@kentcdodds kentcdodds merged commit 3cc387b into testing-library:master Oct 23, 2018
@kentcdodds
Copy link
Member

🎉 This PR is included in version 5.2.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

lucbpz pushed a commit to lucbpz/react-testing-library that referenced this pull request Jul 26, 2020
lucbpz pushed a commit to lucbpz/react-testing-library that referenced this pull request Jul 26, 2020
…ab-respect-radio-groups

fix(tab): make tab respect radio groups (fix testing-library#207)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants