-
Notifications
You must be signed in to change notification settings - Fork 470
getByPlaceholderText Should Also Return Type HTMLInputElement? #65
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
Comments
Would that support a textarea as well? I'm not sure what other elements support a placeholder, but any elements that support a placeholder should be what this query should be able to return. |
Also, thank you for the detailed issue! Hopefully one of our TS maintainers can jump in and help with this one :) |
I’ll test it out on textarea and other elements that have placeholder. If it also works, I’ll work on a PR! Sent with GitHawk |
This is a problem with all the get and queries where the returned element is unknown, this could be a solution: // add generics in all the get and queries
export type GetByAttribute = <T extends HTMLElement>(
container: HTMLElement,
id: Matcher,
options?: MatcherOptions,
) => T
// in your code
getByPlaceholderText<HTMLInputElement>("Password") |
That sounds fine with me. I don't maintain the typescript typings so anyone who wants to help maintain them is welcome to do so. |
That's true. But I think the existing typings are right, type definition should not be changed. The element type is actually unknown. The return value is generic, |
I see. I don't have any counter arguments to @sompylasar
@sompylasar, could you provide some feedback regarding my method of typecasting to satisfy TypeScript? Is that they way you would go about it? const emailNode = getByPlaceholderText("E-mail address") as HTMLInputElement;
const passwordNode = getByPlaceholderText("Password") as HTMLInputElement; |
I just had to implement that same fix - my compiler refused to pass the input element as having a value property unless I cast it as HTMLInputElement. |
You don't need to cast to const emailNode = getByPlaceholderText("E-mail address");
const passwordNode = getByPlaceholderText("Password");
if (emailNode instanceof HTMLInputElement && passwordNode instanceof HTMLInputElement) {
// Do something with emailNode, passwordNode; TypeScript should narrow their types to be HTMLInputElement.
} else {
assert(false, 'expected emailNode and passwordNode to be HTMLInputElement');
} |
Thanks for your time and the advice! I'll use that approach in my test. |
…ary#70) * feat(rerender): implement rerender (testing-library#65) * update readme * update examples * tests * typescript typings * Update index.js * Update rerender.js * Update rerender.js * Update README.md Closes testing-library#65
dom-testing-library
version: 2.7.0react
version: 16.4.1redux-form
version: 7.4.2node
version: 8.11.3npm
(oryarn
) version: 6.1.0Relevant code or config:
renderWithRedux
is the same as the examples fromreact-testing-library
What you did:
I am using
redux-form
for my forms.I want to:
submitLogin
is firedWhat happened:
I get TypeScript errors from the returned Nodes from
getByPlaceholderText
, because it returns a typeHTMLElement
.Reproduction:
Problem description:
Suggested solution:
I'm not very well versed with TypeScript, but I'm wondering if updating the type definition for
queries.d.ts
, so thattype GetByAttribute
returns an intersection ofHTMLElement & HTMLInputElement
makes sense?In the meantime, I'm just typecasting:
The text was updated successfully, but these errors were encountered: