Skip to content

Allow to select file twice in input element #138

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

Closed
weyert opened this issue Oct 24, 2018 · 2 comments
Closed

Allow to select file twice in input element #138

weyert opened this issue Oct 24, 2018 · 2 comments
Labels

Comments

@weyert
Copy link
Contributor

weyert commented Oct 24, 2018

  • dom-testing-library version:
  • react version:
  • node version:
  • npm (or yarn) version:

Relevant code or config:

    test('should allow selecting the same file again', async () => {
      const {getByLabelText, getByAltText, getByText, getByTestId, debug} = renderWithFormik(<TextInput component={ImageField} name="image" showFilename={true} />)

      const file = new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' })
      expect(file.name).toBe('chucknorris.png')
      const imageInput = getByLabelText('Choose a file…')
      fireEvent.click(imageInput)
      fireEvent.change(imageInput, { target: { files: [file] } })
      await wait(() => {
        getByAltText('Preview')
        getByText('chucknorris.png')
      })

      const secondFile = new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' })
      expect(secondFile.name).toBe('chucknorris.png')
      fireEvent.click(imageInput)
      fireEvent.change(imageInput, { target: { files: [secondFile] } })
      await wait(() => {
        getByAltText('Preview')
        getByText('chucknorris.png')
      })
  })

What you did:

I am trying to test a feature that my ImageField-component offers which is selecting an image and then select it again. The problem is that you need to clear out or nullify the value-property of the input-element to make this work in Safari and Chrome. I wanted to double check this possible but the unit test is failing for me with the following error:

FAILED
TypeError: Cannot redefine property: files
    at Function.defineProperty (<anonymous>)
    at Function.fireEvent.(anonymous function) [as change] (/app/node_modules/react-testing-library/node_modules/dom-testing-library/dist/events.js:337:14)
    at Object.change (/app/src/common-ui/components/form/ImageField.test.js:40:17)
    at <anonymous>

Reproduction:

You can reproduce the problem here:
https://codesandbox.io/s/x9mqvy6vqw

Problem description:

The problem is that the error Cannot redefine property: files due to special case made in the events.js of this library.

Suggested solution:

Allow to trigger change-event on a file-input element for a second time without the library giving a runtime error. The potential fix would be the following on line 317:

values: files, configurable: true

@kentcdodds
Copy link
Member

That error message was all I needed. Thanks @weyert!

aa0aab7

@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 3.11.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants