Skip to content
21 changes: 21 additions & 0 deletions src/__tests__/fire-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const eventTypes = [
elementType: 'div',
},
]

beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {})
})
Expand Down Expand Up @@ -215,6 +216,26 @@ test('fireEvent.update does not trigger warning messages', async () => {
expect(console.warn).not.toHaveBeenCalled()
})

test('fireEvent.update should not crash with input file', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test example not enough? @afontcu

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is! But as mentioned, I wouldn't make fireEvent.update() smarter when it comes to type="file" inputs. I would just trigger the proper event, and then add a test showcasing how to handle files as in React Testing Library (and probably also a comment suggesting userEvent to do so:

test('fireEvent.update should not crash with input file', async () => {
  const {getByTestId} = render({
    template: `<input type="file" data-testid="test-update" />`
  })

  const file = new File(['(⌐□_□)'], 'chucknorris.png', { type: 'image/png' })

  const inputEl = getByTestId('test-update')

  Object.defineProperty(inputEl, 'files', {
    value: [file],
  })

  await fireEvent.update(inputEl)

  expect(console.warn).not.toHaveBeenCalled()
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a suggestion with the change I mention above 👍

const {getByTestId} = render({
template: `<input type="file" data-testid="test-update" />`,
})

const file = new File(['(⌐□_□)'], 'chucknorris.png', {type: 'image/png'})

const inputEl = getByTestId('test-update')

// You could replace the lines below with
// userEvent.upload(inputEl, file)
Object.defineProperty(inputEl, 'files', {
value: [file],
})

await fireEvent.update(inputEl)

expect(console.warn).not.toHaveBeenCalled()
})

test('fireEvent.update does not crash if non-input element is passed in', async () => {
const {getByText} = render({
template: `<div>Hi</div>`,
Expand Down
2 changes: 2 additions & 0 deletions src/vue-testing-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ fireEvent.update = (elem, value) => {
if (['checkbox', 'radio'].includes(type)) {
elem.checked = true
return fireEvent.change(elem)
} else if (type === 'file') {
return fireEvent.change(elem)
} else {
elem.value = value
if (elem._vModifiers && elem._vModifiers.lazy) {
Expand Down