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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>?
</summary>

`getByTestId` is looking for the `data-testid` attribute and that's not going to
change
[#76](https://github.com/kentcdodds/dom-testing-library/issues/76#issuecomment-406321122)
[#128](https://github.com/kentcdodds/dom-testing-library/issues/128)
[#204](https://github.com/kentcdodds/react-testing-library/issues/204).

What you can do is to create a [custom render](#custom-render) that overwrites
`getByTestId`:

```js
// test-utils.js
import {render} from 'react-testing-library'

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

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

if (!el) {
throw Error(`Unable to find an element by: [data-test-id="${id}"]`)
}
return el
},
}
}

// re-export everything
export * from 'react-testing-library'

// override render method
export {customRender as render}
```

</details>

#### `asFragment(): DocumentFragment`

Returns a `DocumentFragment` of your rendered component. This can be useful if
Expand Down