Skip to content

userEvent.upload(input, file) seems by pass the form validity check #1293

@junyongz

Description

@junyongz

just recently pay more effort into react library & javascript development for the past few months.

I wonder below line would causing problem for validity() routine call in HTMLInputElement-impl.js

  Object.defineProperties(el, {
    files: {
      configurable: true,
      get: () => files,
    },

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

            // https://html.spec.whatwg.org/multipage/input.html#file-upload-state-(type=file)
            // Constraint validation: If the element is required and the list of selected files is
            // empty, then the element is suffering from being missing.
            case "file":
              if (this._required && this.files.length === 0) {
                return true;
              }

I can't really get to pass the form.validity() check after done the userEvent.upload(element, files) call

simple snippet to simulate, by right, that only file upload input in the form should be valid

test('file upload', async () => {
    const user = userEvent.setup()
    render(<form aria-label='upload form'>
        <input type="file" name="upload-file" role="button" required="true" accept='image/*' aria-label='upload image'></input>
    </form>)

    const file = new File([Uint8Array.from(atob('/9j/4AAQSkZJRgABAQEAAAAAAA=='), c => c.charCodeAt(0)).buffer], 'test.jpg', { type: 'image/jpeg' })

    const input = screen.getByRole('button', {name: 'upload image'})
    await user.upload(input, [file])
    expect(input.files[0]).toEqual(file)

    const form = screen.getByLabelText('upload form')

    expect(form.checkValidity()).toBeTruthy()
    expect(Array.from(form.elements).filter(v => !v.validity.valid)).toHaveLength(0)
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions