Skip to content

feat: add support for alt text queries #1663

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
wants to merge 1 commit into from

Conversation

deecewan
Copy link

Summary

<Image /> has an alt prop, and this is what the screen reader uses to describe the image. There was previously no way to match on the alt text.

I've added matchers for alt text in this PR, so that we can find images using something accessible by screen readers.

Test plan

automated tests are included. these are based on the test-id tests.

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @deecewan, thanks for submitting this PR.

I've done some initial checks, and it seems that alt and aria-label/accessibilityLabel are closely related:

As far as I understand, this means that having alt or aria-label/accessibilityLabel + accessible props in runtime in indistinguishable. The same should be reflected in our queries and matchers.

Since thee above code is platform-specific code, not rendered in your snapshot tests, we would have to simulate that behavior.

In order to have proper support for querying images we would have to do following pieces:

  • getByRole query to support querying images using getRole('image' | 'img', { name: altText });
  • toHaveAccessibleName support checking alt text

As for getByAltText it should inspect alt props as now, but it should also match when all of the following conditions are met:

  • view is a host image
  • accessible prop is set
  • aria-label/accessibilityLabel matches expected alt text

I am still on the fence for adding *ByAltText matcher as on one hand it increases RTL compatibility but on the other hand is ok but not really recommended way to query things in RLT (see query priorities, KCD advice) and has not be request for so far by the users (you're first).

@deecewan would you be willing to submit a separate PR for Image alt prop support in existing queries (byRole) and matchers (toHaveAccessibleName) as described above? In the mean time we can discuss whether to include ByAltText matcher.

@mdjastrzebski
Copy link
Member

Closing as #1663 seems to provide the required functionality without expanding API surface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants