question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intellisense with getByRole

See original GitHub issue

Describe the feature you’d like:

I got the feeling that we could make it easier to use the getByRole queries. From what I can see not all developers know (all of) the WAI-ARIA Roles. The suggestions and the playground are definitely helping, but I think it would be nice if we can type the roles so that the IDE can pick them up and suggest them (shorter feedback loop).

Suggested implementation:

Add a ByRoleMatcher that includes all of the ARIA Roles, and use this type in the byRole queries.

export type ByRoleMatcher =  'button' | 'listitem' | 'textbox' | String | RegExp | MatcherFunction

export type AllByRole = (container: HTMLElement, role: ByRoleMatcher, options?: ByRoleOptions) => HTMLElement[];
export type GetByRole = (container: HTMLElement, role: ByRoleMatcher, options?: ByRoleOptions) => HTMLElement;
export type QueryByRole = (container: HTMLElement, role: ByRoleMatcher, options?: ByRoleOptions) => HTMLElement | null;

This won’t be a breaking change because the user would still have the possibility to use a role that isn’t included in the type.

This looks like this:

image

Describe alternatives you’ve considered:

It’s important to use String while defining the type, otherwise it will either result that the passed role must be one of the options, or that there won’t be any intellisense. That’s also the reason why it isn’t possible to reuse the existing Matcher type (e.g. export type ByRoleMatcher = Matcher | 'button' | 'listitem' | 'textbox')

See the TS playground for examples.

Teachability, Documentation, Adoption, Migration Strategy:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
timdeschryvercommented, Jun 24, 2020

@eps1lon it does, if String is used. See the TS playground in the issue description.

1reaction
eps1loncommented, Jun 23, 2020

The types for aria-query list all supported roles. Though I think we need to allow any string regardless. Not sure if vscode will still display all string literals if you have AllowedRoles | string;

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test material ui autocomplete with react testing library
getByRole is the value of the label of your autocomplete (in your example: openOnFocus: false . then simulate a click on the expected...
Read more >
Cypress Testing Library
Cypress Testing Library extends Cypress's cy commands. ... You can now use all of DOM Testing Library 's findBy , findAllBy , queryBy...
Read more >
Autocompletion not working for "react-testing-library" and " ...
What steps will reproduce the problem? Create a CRA application; Add "react-testing-library" to your dependencies; Write a test as simple as: import ...
Read more >
Using React Testing Library
getByRole - This should be the go-to selector for almost everything. ... You only need to type screen and let your editor's autocomplete...
Read more >
Common mistakes with React Testing Library
const {getByRole} = render(<Example />) const errorMessageNode ... screen. and let your editor's magic autocomplete take care of the rest.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found