Unverified Commit b95f0129 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

test: fix next-form tests when running on react 18 (#70704)

Fixes some broken tests from #68333. They were skipped, but still crash
in `start` mode, because they try to use `useActionState` and crash
during prerendering (because 18 doesn't have that). We turn those pages
into noops instead -- the tests that use them are skipped anyway.
parent 772b0ea2
'use client' 'use client'
import * as React from 'react' import * as React from 'react'
import { useActionState, useState } from 'react'
import Form from 'next/form' import Form from 'next/form'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
export default function Page() { const isReact18 = typeof React.useActionState !== 'function'
export default isReact18 ? DummyPage : Page
function DummyPage() {
return <>This test cannot run in React 18</>
}
function Page() {
const destination = '/pages-dir/redirected-from-action' const destination = '/pages-dir/redirected-from-action'
const router = useRouter() const router = useRouter()
const [, dispatch] = useActionState(() => { const [, dispatch] = React.useActionState(() => {
const to = destination + '?' + new URLSearchParams({ query }) const to = destination + '?' + new URLSearchParams({ query })
router.push(to) router.push(to)
}, undefined) }, undefined)
const [query, setQuery] = useState('') const [query, setQuery] = React.useState('')
return ( return (
<Form action={dispatch} id="search-form"> <Form action={dispatch} id="search-form">
<input <input
......
'use client' 'use client'
import * as React from 'react' import * as React from 'react'
import { type ComponentProps, useActionState, useState } from 'react'
import Form from 'next/form' import Form from 'next/form'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
export default function Page() { const isReact18 = typeof React.useActionState !== 'function'
export default isReact18 ? DummyPage : Page
function DummyPage() {
return <>This test cannot run in React 18</>
}
function Page() {
const destination = '/pages-dir/redirected-from-action' const destination = '/pages-dir/redirected-from-action'
const [query, setQuery] = useState('') const [query, setQuery] = React.useState('')
return ( return (
<Form action="/pages-dir/search" id="search-form"> <Form action="/pages-dir/search" id="search-form">
<input <input
...@@ -25,9 +32,9 @@ export default function Page() { ...@@ -25,9 +32,9 @@ export default function Page() {
function NavigateButton({ function NavigateButton({
to, to,
...props ...props
}: { to: string } & ComponentProps<'button'>) { }: { to: string } & React.ComponentProps<'button'>) {
const router = useRouter() const router = useRouter()
const [, dispatch] = useActionState(() => { const [, dispatch] = React.useActionState(() => {
router.push(to) router.push(to)
}, undefined) }, undefined)
return <button type="submit" formAction={dispatch} {...props} /> return <button type="submit" formAction={dispatch} {...props} />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment