question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

maxlength attribute is ignored

See original GitHub issue

(Source: how to test input maxlength attribute).


  • dom-testing-library version: 5.6.1
  • react version: -
  • node version: 10.16
  • npm 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:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

28reactions
niltonvasquescommented, Jun 30, 2020

I could test it using the userEvent library:

  it('should not support more than 10000 chars', () => {
    expect(input.maxLength).toBe(10000)
    const text = 'loong text'
    const longText = text.repeat(999)

    fireEvent.change(input, { target: { value: longText } })
    userEvent.type(input, text + '!')
    expect(input.value).toBe(text.repeat(1000))
    expect(input.value.length).toBe(10000)
  })
4reactions
kentcdoddscommented, Aug 2, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found