From 2ac04ebeb15661bf94a25882d0a3668f5279b990 Mon Sep 17 00:00:00 2001 From: Esa-Matti Suuronen Date: Sat, 25 Mar 2023 15:59:27 +0200 Subject: [PATCH] Add failing test when reading uploaded file with FormData --- tests/utility/upload.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/utility/upload.ts b/tests/utility/upload.ts index ad887347..f63093d1 100644 --- a/tests/utility/upload.ts +++ b/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'}) @@ -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( + '
', + ) + + 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]) +})