Skip to content

Fix typings of getQueriesForElement #246

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

Conversation

msimulcik
Copy link
Contributor

What:
Fix typings of getQueriesForElement function

Why:
There are currently two problems:

  1. Type of queriesToBind parameter is BoundFunctions<typeof queries> | BoundFunctions<typeof queries>[] which is IMO incorrect because the function is doing the binding and thus should receive unbound queries.
  2. Type parameter of BoundFunctions is hardcoded to typeof queries which prevents binding of custom queries

How:

  1. Parametrised getQueriesForElement to enable binding custom queries
  2. Changed type of queriesToBind to type parameter

Checklist:

  • Documentation added to the docs site N/A
  • Typescript definitions updated
  • Tests N/A
  • Ready to be merged

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

Seems fine to me! Thank you :)

@kentcdodds kentcdodds merged commit 8b3f767 into testing-library:master Apr 17, 2019
@kentcdodds
Copy link
Member

🎉 This PR is included in version 3.19.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@msimulcik
Copy link
Contributor Author

@kentcdodds I've just realised that maybe it would be better to make parameter type more specific. See code below. Those are types copied from react-testing-library typings. What do you think?

interface Query extends Function {
  (container: HTMLElement, ...args: any[]):
    | Error
    | Promise<HTMLElement[]>
    | Promise<HTMLElement>
    | HTMLElement[]
    | HTMLElement
    | null
}

interface Queries {
  [T: string]: Query
}

export function getQueriesForElement<T extends Queries = typeof queries>(
  element: HTMLElement,
  queriesToBind?: T,
): BoundFunctions<T>

@kentcdodds
Copy link
Member

Ah, sure. Feel free to make another PR :)

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

Successfully merging this pull request may close these issues.

2 participants