Skip to content

upload() doesn't convert file to an array with input multiple #529

@zaicevas

Description

@zaicevas

Could someone explain to me the logic behind this line of code: https://github.com/testing-library/user-event/blob/master/src/upload.js#L17 ?

Consider this code snippet:

  function handleChooseImage(event: ChangeEvent<HTMLInputElement>) {
    const selectedFiles = event.target.files

    if (!selectedFiles?.length) return
   
    ...
  }

      <input
        data-testid="add-photos-input"
        ref={inputRef}
        type="file"
        onChange={handleChooseImage}
        accept="image/*"
        multiple
        hidden
      />

My goal is to write following lines of test code

      const imageFile = new File([], 'imgname', { type: 'image/png' })
      const imageInput = screen.getByTestId('add-photos-input')

      userEvent.upload(imageInput, imageFile)

This test code result in event handler being called with an object, where length is undefined and item() returns undefined:

selectedFiles: { length: undefined, item: [Function: item] }

Is that expected behaviour?

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