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 test case for file uploads #49048

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 new Promise((resolve) => setTimeout(resolve, 1000))
shuding marked this conversation as resolved.
Show resolved Hide resolved

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'`)
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>
</>
)
}