Skip to content

Commit

Permalink
Add test case for file uploads (#49048)
Browse files Browse the repository at this point in the history
This PR adds a test case of handling file inputs in a form.

Fixes NEXT-1062
  • Loading branch information
shuding authored May 2, 2023
1 parent e76c881 commit ee48c66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'`)
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/app-dir/actions/app/server/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand All @@ -33,6 +39,13 @@ export default function Form() {
Go nowhere
</button>
</form>
<hr />
<form action={file}>
<input type="file" name="file" id="file" required />
<button type="submit" id="upload">
Upload file
</button>
</form>
</>
)
}

0 comments on commit ee48c66

Please sign in to comment.