Skip to content

Commit aec4fcd

Browse files
committed
add input file to update fireEvent
1 parent bfaeee7 commit aec4fcd

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/__tests__/fire-event.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ const mockFile = ({
124124
name,
125125
size = 0,
126126
type = 'text/plain',
127-
lastModified = new Date()
127+
lastModified = new Date(),
128128
}) => {
129-
const blob = new Blob(['0'.repeat(size)], { type });
130-
blob.lastModifiedDate = lastModified;
131-
return new File([blob], name);
132-
};
129+
const blob = new Blob(['0'.repeat(size)], {type})
130+
blob.lastModifiedDate = lastModified
131+
return new File([blob], name)
132+
}
133133

134134
beforeEach(() => {
135135
jest.spyOn(console, 'warn').mockImplementation(() => {})
@@ -228,27 +228,28 @@ test('fireEvent.update does not trigger warning messages', async () => {
228228
})
229229

230230
test('fireEvent.update should not crash with input file', async () => {
231-
const inputSpy = jest.fn();
232-
const changeSpy = jest.fn();
231+
const spy = jest.fn()
233232

234233
const {getByTestId} = render({
235234
render(h) {
236235
return h('input', {
237236
on: {
238-
input: inputSpy,
239-
change: changeSpy
237+
change: spy,
240238
},
241239
attrs: {
242240
type: 'file',
243241
'data-testid': 'test-update',
244242
},
245243
})
246-
}
244+
},
247245
})
248246

249-
// should expect a array of list since it's a fileList
250-
await fireEvent.update(getByTestId('test-update'), [mockFile({ name: 'random.txt', size: 524288 })])
247+
// should expect an array of list since it's a fileList
248+
await fireEvent.update(getByTestId('test-update'), [
249+
mockFile({name: 'random.txt', size: 524288}),
250+
])
251251

252+
expect(spy).toHaveBeenCalledTimes(1)
252253
expect(console.warn).not.toHaveBeenCalled()
253254
})
254255

src/vue-testing-library.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ fireEvent.update = (elem, value) => {
155155
if (['checkbox', 'radio'].includes(type)) {
156156
elem.checked = true
157157
return fireEvent.change(elem)
158+
} else if (type === 'file') {
159+
Object.defineProperty(elem, 'files', {value})
160+
return fireEvent.change(elem)
158161
} else {
159162
elem.value = value
160163
return fireEvent.input(elem)

0 commit comments

Comments
 (0)