Skip to content

Commit

Permalink
test: typedRoutes for form
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Jul 31, 2024
1 parent 212c0f8 commit d7d84a2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions test/integration/app-types/app-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ const appDir = __dirname
)
})

it('should generate route types correctly and report form errors', async () => {
// Make sure all errors were reported and other Forms passed type checking
const errorLines = [
...errors.matchAll(
/\.\/src\/app\/type-checks\/form\/page\.tsx:(\d+):/g
),
].map(([, line]) => +line)

const ST = 8
const ED = 10
expect(errorLines).toEqual(
Array.from({ length: ED - ST + 1 }, (_, i) => i + ST)
)
})

it('should type check invalid entry exports', () => {
// Can't export arbitrary things.
expect(errors).toContain(`"foo" is not a valid Page export field.`)
Expand Down
36 changes: 36 additions & 0 deletions test/integration/app-types/src/app/type-checks/form/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react'
import type { Route } from 'next'
import Form from 'next/form'

export default function Page() {
const invalidRoutes = (
<>
<Form action="/wrong-link"></Form>
<Form action="/blog/a?1/b"></Form>
<Form action={`/blog/${'a/b/c'}`}></Form>
</>
)

const validRoutes = (
<>
<Form action="/dashboard/another"></Form>
<Form action="/about"></Form>
<Form action="/redirect"></Form>
<Form action={`/blog/${'a/b'}`}></Form>
<Form action={'/invalid' as Route}></Form>
<Form
action={async (formData) => {
'use server'
console.log('function action', formData.get('myInput'))
}}
></Form>
</>
)

return (
<>
{invalidRoutes}
{validRoutes}
</>
)
}

0 comments on commit d7d84a2

Please sign in to comment.