KonnorRogers / shadow-dom-testing-library

An extension of DOM-testing-library to provide hooks into the shadow dom
MIT License
98 stars 2 forks source link

Feature request: Generic types for query functions #52

Closed renet closed 11 months ago

renet commented 1 year ago

The @testing-library/dom uses generics for typing the return value of their query functions like so:

getByText<T extends HTMLElement = HTMLElement>(
  ...args: Parameters<BoundFunction<queries.GetByText<T>>>
): ReturnType<queries.GetByText<T>>

The shadow-dom-testing-library however only returns an element of type HTMLElement without the possibility to set a different type. It would be great, if one had the possibility to set a custom type when using a shadow query function like so:

screen.getByShadowText<HTMLButtonElement>("Submit")
KonnorRogers commented 1 year ago

@renet Thanks, I'll see what I can do, would definitely be a nice quality of life improvement. I think it should be doable.

KonnorRogers commented 11 months ago

@renet sorry I've let this slip, I'll see if I can put something together in the next couple days

KonnorRogers commented 11 months ago

Implemented in v1.11.1

Let me know if it works for you @renet !

renet commented 11 months ago

Thank you very much! 💛 Works like a charm with regular selectors, but sub-selectors of within(...) are still not typable.

KonnorRogers commented 11 months ago

Doh! Thanks! I'll take a look!

KonnorRogers commented 11 months ago

@renet Totally annoyed with how RTL doesn't preserve query function signatures, but I managed to get it working with a fantastically ugly hack.

https://github.com/KonnorRogers/shadow-dom-testing-library/pull/54

KonnorRogers commented 11 months ago

Released in v1.11.2