Skip to content
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

feature: implement toHaveAccessibleName Jest matcher #1503

Closed
mdjastrzebski opened this issue Sep 19, 2023 · 9 comments
Closed

feature: implement toHaveAccessibleName Jest matcher #1503

mdjastrzebski opened this issue Sep 19, 2023 · 9 comments
Assignees
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@mdjastrzebski
Copy link
Member

mdjastrzebski commented Sep 19, 2023

Describe the Feature

toHaveAccessibleName is a Jest DOM matcher asserting that given element has certain accessible name: text content or a11y label.

RNTL already contains some code working with accessible name, namely *ByRole query accepts an optional name param. This matcher should compare expect name parameter with all of these:

  1. text content of element referenced by accessibilityLabelledBy / aria-labelledby prop
  2. accessbilityLabel / aria-label prop
  3. text content of given element

These should be evaluated in that particular order.

Possible Implementations

Proposed API:

export function toHaveAccessibleName(
  this: jest.MatcherContext,
  element: ReactTestInstance,
  expectedName?: TextMatch,
  options?: TextMatchOptions,
) 

The matcher should:

  1. Validate that it is invoked on a host element
  2. When expectedName param is provided check if it matches element's accessible name
  3. When expectedName is not provided check if element has accessible name

Each matcher should have a fairly comprehensive test suite.

Related Issues

#1454

@mdjastrzebski mdjastrzebski added help wanted Extra attention is needed good first issue Good for newcomers labels Sep 19, 2023
@anishamalde
Copy link
Contributor

I can do this!

@mdjastrzebski
Copy link
Member Author

@anishamalde pls go ahead!

@anishamalde
Copy link
Contributor

anishamalde commented Sep 25, 2023

@mdjastrzebski just double checking this is correct:

export function toHaveAccessibleName(
  this: jest.MatcherContext,
  element: ReactTestInstance,
  expectedName?: TextMatch,
  options?: TextMatchOptions,
) 

@mdjastrzebski
Copy link
Member Author

Yes it's correct signature

@anishamalde
Copy link
Contributor

I have completed the implementation with the exception of it getting labelled by the text content of nested element referenced by accessibilityLabelledBy / aria-labelledby prop.

#1509

I tried the getHostSiblings but this just returns an empty array.

@mdjastrzebski
Copy link
Member Author

mdjastrzebski commented Sep 28, 2023

@anishamalde you should be able to reuse parts of matchLabelText in order match both label and labelledBy props.

@anishamalde
Copy link
Contributor

Just seen the function, thank-you, will update the PR!

@mdjastrzebski
Copy link
Member Author

Resolved by #1509

@mdjastrzebski
Copy link
Member Author

🎉 Release in version: v12.4.0

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

No branches or pull requests

2 participants