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

Problems with custom input types #501

Closed
kserjey opened this issue Nov 23, 2020 · 3 comments · Fixed by #619
Closed

Problems with custom input types #501

kserjey opened this issue Nov 23, 2020 · 3 comments · Fixed by #619
Labels
bug Something isn't working released

Comments

@kserjey
Copy link

kserjey commented Nov 23, 2020

  • @testing-library/user-event version: 12.2.2
  • Testing Framework and version: CodeSandbox (actually I don't know how to get it)
  • DOM Environment: CodeSandbox (actually I don't know how to get it)

Relevant code or config:

const { getByRole } = render(<input type="number"/>);
const input = getByRole('spinbutton');
userEvent.type(input, '123{selectall}{backspace}');

Reproduction repository:

https://codesandbox.io/s/user-event-and-typenumber-c5i10

Problem description:

After spending some time trying to solve this issue I've found what causing it. JSDOM has a limitation on setSelectionRange:

https://github.com/jsdom/jsdom/blob/c2fb8ff94917a4d45e2398543f5dd2a8fed0bdab/lib/jsdom/living/nodes/HTMLInputElement-impl.js#L45

And you have a little hack to make it work with userEvent.clear:

user-event/src/clear.js

Lines 12 to 27 in f7620ab

// TODO: track the selection range ourselves so we don't have to do this input "type" trickery
// just like cypress does: https://github.com/cypress-io/cypress/blob/8d7f1a0bedc3c45a2ebf1ff50324b34129fdc683/packages/driver/src/dom/selection.ts#L16-L37
const elementType = element.type
// type is a readonly property on textarea, so check if element is an input before trying to modify it
if (element.tagName === 'INPUT') {
// setSelectionRange is not supported on certain types of inputs, e.g. "number" or "email"
element.type = 'text'
}
type(element, '{selectall}{del}', {
delay: 0,
initialSelectionStart: element.selectionStart,
initialSelectionEnd: element.selectionEnd,
})
if (element.tagName === 'INPUT') {
element.type = elementType
}

But if we are talking about type actions like {selectall}, well, it doesn't work for types other than ["text", "search", "url", "tel", "password"]. For now I'm just using:

const { getByRole } = render(<input type="number"/>);
const input = getByRole('spinbutton');
userEvent.type(input, '123');
userEvent.clear(input);

When I tried to move your hack to the type.js, I faced problems with testing (I can't test selectionStart, because it null) and I wasn't sure how to temporary change element.type.

@nickserv nickserv added bug Something isn't working needs investigation Someone has to do research on this labels Nov 30, 2020
@ph-fritsche ph-fritsche mentioned this issue Dec 14, 2020
8 tasks
@kayo-campos
Copy link

Had the same problem here, thanks for pointing the workaround! :D

@ph-fritsche
Copy link
Member

We should apply the workaround in the helper:

} else {
;(element as HTMLInputElement).setSelectionRange(
newSelectionStart,
newSelectionEnd,
)
}

@github-actions
Copy link

🎉 This issue has been resolved in version 13.0.11 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ph-fritsche ph-fritsche removed the needs investigation Someone has to do research on this label Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants