Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion tests/utility/upload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {setup} from '#testHelpers'
import {render, setup} from '#testHelpers'
import userEvent from '#src'

test('change file input', async () => {
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
Expand Down Expand Up @@ -225,3 +226,25 @@ test('throw error if trying to use upload on an invalid element', async () => {
`The associated INPUT element does not accept file uploads`,
)
})

test('uploaded file can be read with FormData', async () => {
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
const {element: form} = render<HTMLFormElement>(
'<form><input name="file" type="file" /></form>',
)

const input = form.querySelector('input') as HTMLInputElement

await userEvent.upload(input, file)

const data = new FormData(form)

const formFile = data.get('file')
if (!(formFile instanceof File)) {
throw new Error('formFile is not a File')
}

expect(formFile).toBeInstanceOf(File)
expect(formFile.name).toBe('hello.png')
expect(formFile).toBe(input.files?.[0])
})