testing-library / dom-testing-library

πŸ™ Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.26k stars 467 forks source link

fix(ByRole): filter by name or description when hidden #1161

Open viniciuslagedo opened 2 years ago

viniciuslagedo commented 2 years ago

What: Fixes the ByRole methods to accept filter hidden elements using name and description. Also fixes the logs to show the accessible name in case of not found error. Related to https://github.com/testing-library/dom-testing-library/issues/846

Why: If we have two hidden elements with the same role we can't filter that using the name. Also when we have hidden elements on logs they don't have the name filled, they are always equal ""

How: By adding the hidden option when use computeAccessibleDescription and computeAccessibleName to get the accessible description and name

Checklist:

codesandbox-ci[bot] commented 2 years ago

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 64e243b87bf12ab6b6ef2bab8414fcb814aca068:

Sandbox Source
react-testing-library-examples Configuration
codecov[bot] commented 1 year ago

Codecov Report

Merging #1161 (64e243b) into main (edffb7c) will not change coverage. The diff coverage is n/a.

@@            Coverage Diff            @@
##              main     #1161   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines          998       998           
  Branches       326       326           
=========================================
  Hits           998       998           
Flag Coverage Ξ”
node-12 100.00% <ΓΈ> (ΓΈ)
node-14 100.00% <ΓΈ> (ΓΈ)
node-16 100.00% <ΓΈ> (ΓΈ)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Ξ”
src/queries/role.js 100.00% <ΓΈ> (ΓΈ)
src/role-helpers.js 100.00% <ΓΈ> (ΓΈ)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

eps1lon commented 1 year ago

The problem is that the accessible name is different if the element is hidden. See https://github.com/testing-library/dom-testing-library/issues/846#issuecomment-742156607

So this adds a footgun where you just flip hidden and expect name to return the same set. I wasn't convinced by https://github.com/testing-library/dom-testing-library/issues/846 fully to implement this so let's add some concrete, high level use cases to https://github.com/testing-library/dom-testing-library/issues/846 first (e.g. what are you testing?).