Skip to content

Commit b762198

Browse files
feat: Add support for input file on fireEvent.update (#179)
* add input file fail test * fix incorrect render * add input file to update fireEvent * Update based on suggestion Co-authored-by: Adrià Fontcuberta <[email protected]>
1 parent a051013 commit b762198

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/__tests__/fire-event.js

+21
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ const eventTypes = [
119119
elementType: 'div',
120120
},
121121
]
122+
122123
beforeEach(() => {
123124
jest.spyOn(console, 'warn').mockImplementation(() => {})
124125
})
@@ -215,6 +216,26 @@ test('fireEvent.update does not trigger warning messages', async () => {
215216
expect(console.warn).not.toHaveBeenCalled()
216217
})
217218

219+
test('fireEvent.update should not crash with input file', async () => {
220+
const {getByTestId} = render({
221+
template: `<input type="file" data-testid="test-update" />`,
222+
})
223+
224+
const file = new File(['(⌐□_□)'], 'chucknorris.png', {type: 'image/png'})
225+
226+
const inputEl = getByTestId('test-update')
227+
228+
// You could replace the lines below with
229+
// userEvent.upload(inputEl, file)
230+
Object.defineProperty(inputEl, 'files', {
231+
value: [file],
232+
})
233+
234+
await fireEvent.update(inputEl)
235+
236+
expect(console.warn).not.toHaveBeenCalled()
237+
})
238+
218239
test('fireEvent.update does not crash if non-input element is passed in', async () => {
219240
const {getByText} = render({
220241
template: `<div>Hi</div>`,

src/vue-testing-library.js

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ fireEvent.update = (elem, value) => {
158158
if (['checkbox', 'radio'].includes(type)) {
159159
elem.checked = true
160160
return fireEvent.change(elem)
161+
} else if (type === 'file') {
162+
return fireEvent.change(elem)
161163
} else {
162164
elem.value = value
163165
if (elem._vModifiers && elem._vModifiers.lazy) {

0 commit comments

Comments
 (0)