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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zaicevas opened this issue Dec 22, 2020 · 2 comments
Closed

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

zaicevas opened this issue Dec 22, 2020 · 2 comments

Comments

@zaicevas
Copy link
Contributor

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?

@ph-fritsche
Copy link
Member

The function expects you to provide File for single file inputs and File[] for multiple file inputs.

userEvent.upload(multipleFileInput, [imageFile])

I think this could be improved by just converting it.

@ph-fritsche
Copy link
Member

resolved in 12.6.2

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

No branches or pull requests

2 participants