Skip to content

Commit

Permalink
test: add test for file inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Jul 30, 2024
1 parent 850d6f6 commit b854edf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react'
import Form from 'next/form'

export default function Home() {
return (
<Form action="/search" id="search-form">
<input name="query" type="text" />
<input name="file" type="file" />
<button type="submit">Submit</button>
</Form>
)
}
43 changes: 40 additions & 3 deletions test/e2e/app-dir/next-form/default/next-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,46 @@ describe('app dir - form', () => {
)
})

// TODO(lubieowoce): implement this
// (we don't have any methods on BrowserInterface to deal with files)
it.todo('handles file inputs, but warns about them')
it('url-encodes file inputs, but warns about them', async () => {
const session = await next.browser(`/forms/with-file-input`)

const fileInputSelector = 'input[type="file"]'
// Fake a file to upload
await session.eval(`
const fileInput = document.querySelector(${JSON.stringify(fileInputSelector)});
const file = new File(['hello'], 'hello.txt', { type: 'text/plain' });
const list = new DataTransfer();
list.items.add(file);
fileInput.files = list.files;
`)

const searchInput = await session.elementByCss('input[name="query"]')
await searchInput.fill('my search')

const submitButton = await session.elementByCss('[type="submit"]')
await submitButton.click()

if (isNextDev) {
const logs = await session.log()
expect(logs).toContainEqual(
expect.objectContaining({
source: 'warning',
message: expect.stringContaining(
`<Form> only supports file inputs if \`action\` is a function`
),
})
)
}

const result = await session.waitForElementByCss('#search-results').text()
expect(result).toMatch(/query: "my search"/)

const url = new URL(await session.url())
expect([...url.searchParams.entries()]).toEqual([
['query', 'my search'],
['file', 'hello.txt'],
])
})
})

async function trackMpaNavs(session: BrowserInterface) {
Expand Down

0 comments on commit b854edf

Please sign in to comment.