From 63179407c424ea1334a3736cf86bdbc8e10b24c8 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Mon, 1 May 2023 19:33:50 +0200 Subject: [PATCH 1/4] add test for file input --- test/e2e/app-dir/actions/app-action.test.ts | 22 +++++++++++++++++++++ test/e2e/app-dir/actions/app/server/form.js | 13 ++++++++++++ 2 files changed, 35 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..cc65969a7a6b0 100644 --- a/test/e2e/app-dir/actions/app-action.test.ts +++ b/test/e2e/app-dir/actions/app-action.test.ts @@ -85,6 +85,28 @@ createNextDescribe( }, 'my-not-found') }) + it('should support uploading files', async () => { + const logs: string[] = [] + next.on('stdout', (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() + + expect( + logs.some((log) => log.includes('File name: hello.txt size: 5')) + ).toBe(true) + }) + 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 +
+
+ + +
) } From f2cbb7f71cc1e58bfd5f3212be2e647d5a1ca5c7 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Mon, 1 May 2023 20:10:18 +0200 Subject: [PATCH 2/4] add a bit delay --- test/e2e/app-dir/actions/app-action.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/e2e/app-dir/actions/app-action.test.ts b/test/e2e/app-dir/actions/app-action.test.ts index cc65969a7a6b0..dc338e62e0992 100644 --- a/test/e2e/app-dir/actions/app-action.test.ts +++ b/test/e2e/app-dir/actions/app-action.test.ts @@ -90,6 +90,9 @@ createNextDescribe( next.on('stdout', (log) => { logs.push(log) }) + next.on('stderr', (log) => { + logs.push(log) + }) const browser = await next.browser('/server') @@ -100,6 +103,8 @@ createNextDescribe( list.items.add(file); document.getElementById('file').files = list.files; `) + + await new Promise((resolve) => setTimeout(resolve, 1000)) await browser.elementByCss('#upload').click() expect( From 0de421ee9d0f294360e81a7f6e6a67e9660e286f Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Mon, 1 May 2023 21:07:38 +0200 Subject: [PATCH 3/4] wait for a sec --- test/e2e/app-dir/actions/app-action.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/e2e/app-dir/actions/app-action.test.ts b/test/e2e/app-dir/actions/app-action.test.ts index dc338e62e0992..8439d2b873655 100644 --- a/test/e2e/app-dir/actions/app-action.test.ts +++ b/test/e2e/app-dir/actions/app-action.test.ts @@ -104,9 +104,10 @@ createNextDescribe( document.getElementById('file').files = list.files; `) - await new Promise((resolve) => setTimeout(resolve, 1000)) await browser.elementByCss('#upload').click() + await new Promise((resolve) => setTimeout(resolve, 1000)) + expect( logs.some((log) => log.includes('File name: hello.txt size: 5')) ).toBe(true) From def39006376f70e7df2b16d9a7271a31c8b4a5f9 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Mon, 1 May 2023 23:07:11 +0200 Subject: [PATCH 4/4] use check --- test/e2e/app-dir/actions/app-action.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/e2e/app-dir/actions/app-action.test.ts b/test/e2e/app-dir/actions/app-action.test.ts index 8439d2b873655..ea9a5a40e7485 100644 --- a/test/e2e/app-dir/actions/app-action.test.ts +++ b/test/e2e/app-dir/actions/app-action.test.ts @@ -106,11 +106,11 @@ createNextDescribe( await browser.elementByCss('#upload').click() - await new Promise((resolve) => setTimeout(resolve, 1000)) - - expect( - logs.some((log) => log.includes('File name: hello.txt size: 5')) - ).toBe(true) + await check(() => { + return logs.some((log) => log.includes('File name: hello.txt size: 5')) + ? 'yes' + : '' + }, 'yes') }) it('should support hoc auth wrappers', async () => {