From ee48c667313524f5effeb0e32af1acc78299e849 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Tue, 2 May 2023 14:49:15 +0200 Subject: [PATCH] Add test case for file uploads (#49048) This PR adds a test case of handling file inputs in a form. Fixes NEXT-1062 --- test/e2e/app-dir/actions/app-action.test.ts | 28 +++++++++++++++++++++ test/e2e/app-dir/actions/app/server/form.js | 13 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/test/e2e/app-dir/actions/app-action.test.ts b/test/e2e/app-dir/actions/app-action.test.ts index d6526460cfd2f..ea9a5a40e7485 100644 --- a/test/e2e/app-dir/actions/app-action.test.ts +++ b/test/e2e/app-dir/actions/app-action.test.ts @@ -85,6 +85,34 @@ createNextDescribe( }, 'my-not-found') }) + it('should support uploading files', async () => { + const logs: string[] = [] + next.on('stdout', (log) => { + logs.push(log) + }) + next.on('stderr', (log) => { + logs.push(log) + }) + + const browser = await next.browser('/server') + + // Fake a file to upload + await browser.eval(` + const file = new File(['hello'], 'hello.txt', { type: 'text/plain' }); + const list = new DataTransfer(); + list.items.add(file); + document.getElementById('file').files = list.files; + `) + + await browser.elementByCss('#upload').click() + + await check(() => { + return logs.some((log) => log.includes('File name: hello.txt size: 5')) + ? 'yes' + : '' + }, 'yes') + }) + it('should support hoc auth wrappers', async () => { const browser = await next.browser('/header') await await browser.eval(`document.cookie = 'auth=0'`) diff --git a/test/e2e/app-dir/actions/app/server/form.js b/test/e2e/app-dir/actions/app/server/form.js index 5e47336cc7e40..2fc99dbc16403 100644 --- a/test/e2e/app-dir/actions/app/server/form.js +++ b/test/e2e/app-dir/actions/app/server/form.js @@ -15,6 +15,12 @@ async function nowhere() { notFound() } +async function file(formData) { + 'use server' + const file = formData.get('file') + console.log('File name:', file.name, 'size:', file.size) +} + export default function Form() { return ( <> @@ -33,6 +39,13 @@ export default function Form() { Go nowhere +
+
+ + +
) }