Skip to content

queryByTestId, getByTestId use partial case-insensitive text match by default, which is unexpected #8

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

Closed
sompylasar opened this issue Apr 8, 2018 · 3 comments · Fixed by #29
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@sompylasar
Copy link
Collaborator

  • dom-testing-library version: 1.1.0
  • node version: N/A
  • npm (or yarn) version: N/A

Relevant code or config

import { getByTestId } from "dom-testing-library";

document.getElementById("test").innerHTML = `
<div data-testid="supertest-123"></div>
`;

document.getElementById("output").innerHTML = `
${getByTestId(document, "tEsT")
  .outerHTML.replace(/</g, "&lt;")
  .replace(/>/g, "&gt;")}
`;

What you did:

  1. Added an element with data-testid="supertest-123" to an empty DOM.
  2. Queried the DOM via getByTestId by string "tEsT".

What happened:

The element with data-testid="supertest-123" was found.

Reproduction repository:

https://codesandbox.io/s/pwrpl1yy9x?module=%2Fsrc%2Findex.js

Problem description:

The current behavior is unexpected, the identifiers aren't supposed to be matched partially and case-insensitively.

Suggested solution:

Use case-sensitive full-string match in queryByTestId, getByTestId.

@kentcdodds
Copy link
Member

I know this is how it works and added it intentionally. Perhaps it is unexpected because an ID is supposed to be unique and it could cause problems if a partially matched item is found before the fully matched item.

For that reason I'd be willing to call this a bug and accept a PR to fix it.

@kentcdodds kentcdodds added bug Something isn't working help wanted Extra attention is needed good first issue Good for newcomers labels Apr 8, 2018
@antsmartian
Copy link
Contributor

antsmartian commented Apr 8, 2018

I can pick this up provided others didn't picked it ;)

alexkrolick added a commit to alexkrolick/dom-testing-library that referenced this issue May 4, 2018
kentcdodds pushed a commit that referenced this issue May 4, 2018
* fix: use exact match for data-testid

[fixes #8]

* add tests for exact matches

* disallow substring match

* fix: heading level for getByTestId

* Add docs for ExactTextMatch

* Update README.md
@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 1.10.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants