maxlength attribute is ignored
See original GitHub issue(Source: how to test input maxlength attribute).
dom-testing-library
version: 5.6.1react
version: -node
version: 10.16npm
version: 6.9
Relevant code or config:
import { getByRole, fireEvent } from '@testing-library/dom'
test('maxlength', () => {
const div = document.createElement('div')
div.innerHTML = `<input type="text" maxlength="10" />`
const input = getByRole(div, 'textbox')
fireEvent.input(input, { target: { value: '01234657890123456789' }})
expect(input.maxLength).toBe(10)
expect(input.value).toBe('0123456789') // <-- fails
})
What happened:
Expected the test above to pass.
● maxlength
expect(received).toBe(expected) // Object.is equality
Expected: "0123456789"
Received: "01234657890123456789"
26 |
27 | expect(input.maxLength).toBe(10)
> 28 | expect(input.value).toBe('0123456789') // <-- fails
| ^
29 | })
30 |
at Object.toBe (src/Input.spec.js:16:23)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 0.332s, estimated 1s
This might be an expected behavior (not sure if an Event
should honor maxlength
attribute), but felt weird. I thought about this a bit and explore some ideas, but couldn’t come up with a fix.
Tried with RTL and VTL and both failed too, obviously.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
maxlength ignored for input type="number" in Chrome
If the value of the type attribute is text , email , search , password , tel , or url , this attribute...
Read more >HTML attribute: maxlength - MDN Web Docs
The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> ....
Read more >MaxLength ignored in multiline textbox. - MSDN - Microsoft
Problem : Maxlength property is ignored in multiline textboxes. ... the MaxLength property will not limit the maximum length of user input.
Read more >Chrome is ignoring maxlength field on input type=number
A form control maxlength attribute , controlled by a dirty value flag , declares a limit on the number of characters a user...
Read more >HTML input maxlength Attribute - W3Schools
Definition and Usage. The maxlength attribute specifies the maximum number of characters allowed in the <input> element.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I could test it using the userEvent library:
maxlength
is imposed on the user who’s interacting with the field, but not on imperative code changing the field. I think we may be able to take it into account in user-event, but here in DTL, I think we should leave it as-is.If you wanted to test
maxlength
, then I’d just assert the attribute is set properly and rely on the browser to deal with that properly.