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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing into validated input types #317

Closed
wants to merge 2 commits into from

Conversation

tpict
Copy link

@tpict tpict commented Jun 6, 2020

What:

Temporarily sets the target element's type to text until the type function resolves

Why:

The type implementation depends on setSelectionRange which raises an error for validated input types like email, number

How:

Take a guess 馃槤

Checklist:

  • Documentation N/A (?)
  • Tests
  • Typings N/A
  • Ready to be merged

I did try replacing type with a getter property to log a warning along the lines of "hey, you're accessing type while typeing and the result will be inaccurate", but it turns out that it's referenced by testing machinery so frequently as to be useless. Does this behavior need to be documented anywhere?

@codecov
Copy link

codecov bot commented Jun 6, 2020

Codecov Report

Merging #317 into master will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #317      +/-   ##
==========================================
+ Coverage   99.31%   99.32%   +0.01%     
==========================================
  Files           2        3       +1     
  Lines         293      298       +5     
  Branches       74       76       +2     
==========================================
+ Hits          291      296       +5     
  Misses          2        2              
Impacted Files Coverage 螖
src/index.js 98.88% <酶> (-0.01%) 猬囷笍
src/type.js 100.00% <100.00%> (酶)
src/utils.js 100.00% <100.00%> (酶)

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update 5c11411...484183c. Read the comment docs.

@@ -495,3 +495,16 @@ test('ignored {backspace} in controlled input', async () => {
keyup: 4 (52)
`)
})

test('typing in an input with validated type works', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding a case for multiple input types, like here https://github.com/testing-library/user-event/blob/master/src/__tests__/clear.js#L77

@kentcdodds
Copy link
Member

I'm afraid this really isn't going to work. Changing the input's type will violate the principle of least surprise (especially with regard to async code and event handlers that may rely on the type being set to something specific).

In addition, it's not going to help people who try to set the selection range before typing.

I'm thinking the solution will be to assume that when the selectionStart is null, it's actually always at the end of the input. I think that's the best we can do.

Would you be able to add a test to make sure this works on an input with a number? await userEvent.type('12{backspace}34'). If it does not, could you make it work?

@kentcdodds
Copy link
Member

Hey @tpict, so I was working on type fixing several bugs and I fixed this on my own. Thanks for your help anyway!

@kentcdodds kentcdodds closed this Jun 8, 2020
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

Successfully merging this pull request may close these issues.

None yet

3 participants