|
1 | 1 | import userEvent from '../'
|
2 |
| -import {setup} from './helpers/utils' |
| 2 | +import {setup, addListeners} from './helpers/utils' |
3 | 3 |
|
4 | 4 | test('should fire the correct events for input', () => {
|
5 | 5 | const file = new File(['hello'], 'hello.png', {type: 'image/png'})
|
@@ -61,6 +61,8 @@ test('should fire the correct events with label', () => {
|
61 | 61 | label[for="element"] - click: Left (0)
|
62 | 62 | input#element[value=""] - click: Left (0)
|
63 | 63 | input#element[value=""] - focusin
|
| 64 | + input#element[value=""] - input |
| 65 | + input#element[value=""] - change |
64 | 66 | `)
|
65 | 67 | })
|
66 | 68 |
|
@@ -125,3 +127,37 @@ test('should not upload when is disabled', () => {
|
125 | 127 | expect(element.files.item(0)).toBeNull()
|
126 | 128 | expect(element.files).toHaveLength(0)
|
127 | 129 | })
|
| 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 | +}) |
0 commit comments