Skip to content

Commit d4db93c

Browse files
authored
fix: React onChange/onInput events not triggering on input type="file" file upload (testing-library#381)
* fix: upload change/input events not bubbling up * test: upload function change/input event bubbling * test: updated upload with label test to reflect event bubbling Co-authored-by: Kent C. Dodds <[email protected]> Closes testing-library#380
1 parent 75bab48 commit d4db93c

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/__tests__/upload.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import userEvent from '../'
2-
import {setup} from './helpers/utils'
2+
import {setup, addListeners} from './helpers/utils'
33

44
test('should fire the correct events for input', () => {
55
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
@@ -61,6 +61,8 @@ test('should fire the correct events with label', () => {
6161
label[for="element"] - click: Left (0)
6262
input#element[value=""] - click: Left (0)
6363
input#element[value=""] - focusin
64+
input#element[value=""] - input
65+
input#element[value=""] - change
6466
`)
6567
})
6668

@@ -125,3 +127,37 @@ test('should not upload when is disabled', () => {
125127
expect(element.files.item(0)).toBeNull()
126128
expect(element.files).toHaveLength(0)
127129
})
130+
131+
test('should call onChange/input bubbling up the event when a file is selected', () => {
132+
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
133+
134+
const {element: form} = setup(`
135+
<form>
136+
<input type="file" />
137+
</form>
138+
`)
139+
const input = form.querySelector('input')
140+
141+
const onChangeInput = jest.fn()
142+
const onChangeForm = jest.fn()
143+
const onInputInput = jest.fn()
144+
const onInputForm = jest.fn()
145+
addListeners(input, {
146+
eventHandlers: {change: onChangeInput, input: onInputInput},
147+
})
148+
addListeners(form, {
149+
eventHandlers: {change: onChangeForm, input: onInputForm},
150+
})
151+
152+
expect(onChangeInput).toHaveBeenCalledTimes(0)
153+
expect(onChangeForm).toHaveBeenCalledTimes(0)
154+
expect(onInputInput).toHaveBeenCalledTimes(0)
155+
expect(onInputForm).toHaveBeenCalledTimes(0)
156+
157+
userEvent.upload(input, file)
158+
159+
expect(onChangeForm).toHaveBeenCalledTimes(1)
160+
expect(onChangeInput).toHaveBeenCalledTimes(1)
161+
expect(onInputInput).toHaveBeenCalledTimes(1)
162+
expect(onInputForm).toHaveBeenCalledTimes(1)
163+
})

src/upload.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ function upload(element, fileOrFiles, init) {
3535
input,
3636
createEvent('input', input, {
3737
target: {files: inputFiles},
38+
bubbles: true,
39+
cancelable: false,
40+
composed: true,
3841
...init,
3942
}),
4043
)
4144

42-
fireEvent(
43-
input,
44-
createEvent('change', input, {
45-
target: {files: inputFiles},
46-
...init,
47-
}),
48-
)
45+
fireEvent.change(input, {
46+
target: {files: inputFiles},
47+
...init,
48+
})
4949
}
5050

5151
export {upload}

0 commit comments

Comments
 (0)