Skip to content

Commit

Permalink
feat: Add support for input file on fireEvent.update (#179)
Browse files Browse the repository at this point in the history
* add input file fail test

* fix incorrect render

* add input file to update fireEvent

* Update based on suggestion

Co-authored-by: Adrià Fontcuberta <afontcu@gmail.com>
  • Loading branch information
JeromeDeLeon and afontcu authored Nov 24, 2020
1 parent a051013 commit b762198
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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 () => {
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

0 comments on commit b762198

Please sign in to comment.