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

Generics not supported when using bound functions #1303

Open
JohnMarsden24 opened this issue Apr 21, 2024 · 1 comment
Open

Generics not supported when using bound functions #1303

JohnMarsden24 opened this issue Apr 21, 2024 · 1 comment

Comments

@JohnMarsden24
Copy link

Describe the feature you'd like:

Happy to give this a try myself!

I've found myself repeating the same pattern of typecasting elements returned from queries when using render when I need to access specific element properties as the return type for queries is always HTMLElement as seen in the two below screenshots:

Screenshot 2024-04-21 at 19 10 59 Screenshot 2024-04-21 at 19 12 54

Suggested implementation:

Firstly I thought about using function overloads and literal types to fix this problem but then realised any HTML element can be assigned a role so I then thought about using generics and came across this old issue which was implemented several years ago. In that thread it said the generics got lost when using screen, which was addressed and fixed but it looks like the same problem applies to render.

I imagine the fix will be similar to that found when adding generics to screen queries

Describe alternatives you've considered:

Open to suggestions

Teachability, Documentation, Adoption, Migration Strategy:

Allow users to pass generics to queries returned from render:

Screenshot 2024-04-21 at 21 34 23
@syi0808
Copy link

syi0808 commented Jun 18, 2024

I think this will be solved by importing a screen object.

import { screen } from 'react-testing-library';

expect(screen.getByRole<HTMLAnchorElement>('link').href).toEqual(...);

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

No branches or pull requests

2 participants