Unverified Commit b095e9e9 authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Test Progressive Enhancement of Server Actions (#52062)

Adds a regression test for testing progressive enhancement of Server
Actions. Both when passed from a Server Component and when imported into
a Client Component.

#51723 landed a bit too early which broke this but it'll be fixed again
once React is upgraded.
Co-authored-by: 's avatarShu Ding <g@shud.in>
parent 91415860
/* eslint-disable jest/no-standalone-expect */
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
createNextDescribe(
'app-dir action progressive enhancement',
{
files: __dirname,
dependencies: {
react: 'latest',
nanoid: 'latest',
'react-dom': 'latest',
'server-only': 'latest',
},
},
({ next, isNextDev, isNextStart, isNextDeploy }) => {
it('should support formData and redirect without JS', async () => {
const browser = await next.browser('/server', {
disableJavaScript: true,
})
await browser.eval(`document.getElementById('name').value = 'test'`)
await browser.elementByCss('#submit').click()
await check(() => {
return browser.eval('window.location.pathname + window.location.search')
}, '/header?name=test&constructor=FormData&hidden-info=hi')
})
it('should support actions from client without JS', async () => {
const browser = await next.browser('/server', {
disableJavaScript: true,
})
await browser.eval(
`document.getElementById('client-name').value = 'test'`
)
await browser.elementByCss('#there').click()
await check(() => {
return browser.eval('window.location.pathname + window.location.search')
}, '/header?name=test&constructor=FormData&hidden-info=hi')
})
}
)
'use server' 'use server'
import { redirect } from 'next/navigation'
export async function inc(value) { export async function inc(value) {
return value + 1 return value + 1
} }
...@@ -7,3 +9,15 @@ export async function inc(value) { ...@@ -7,3 +9,15 @@ export async function inc(value) {
export default async function dec(value) { export default async function dec(value) {
return value - 1 return value - 1
} }
export async function redirectAction(formData) {
'use server'
redirect(
'/header?name=' +
formData.get('name') +
'&constructor=' +
formData.constructor.name +
'&hidden-info=' +
formData.get('hidden-info')
)
}
'use client'
import { redirectAction } from './actions'
export default function Form() {
return (
<form>
<input type="text" name="hidden-info" value="hi" hidden />
<input type="text" name="name" id="client-name" required />
<button formAction={redirectAction} type="submit" id="there">
Go there
</button>
</form>
)
}
import Counter from './counter' import Counter from './counter'
import Form from './form' import Form from './form'
import ClientForm from './client-form'
import dec, { inc } from './actions' import dec, { inc } from './actions'
import { log } from './actions-2' import { log } from './actions-2'
...@@ -17,6 +18,7 @@ export default function Page() { ...@@ -17,6 +18,7 @@ export default function Page() {
}} }}
/> />
<Form /> <Form />
<ClientForm />
<form> <form>
<button id="log" formAction={log}> <button id="log" formAction={log}>
log log
......
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