Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failing test when reading uploaded file with FormData #1109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion tests/utility/upload.ts
@@ -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])
})