Skip to content

Commit b095e9e

Browse files
sebmarkbageshuding
andauthored
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: Shu Ding <g@shud.in>
1 parent 9141586 commit b095e9e

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-disable jest/no-standalone-expect */
2+
import { createNextDescribe } from 'e2e-utils'
3+
import { check } from 'next-test-utils'
4+
5+
createNextDescribe(
6+
'app-dir action progressive enhancement',
7+
{
8+
files: __dirname,
9+
dependencies: {
10+
react: 'latest',
11+
nanoid: 'latest',
12+
'react-dom': 'latest',
13+
'server-only': 'latest',
14+
},
15+
},
16+
({ next, isNextDev, isNextStart, isNextDeploy }) => {
17+
it('should support formData and redirect without JS', async () => {
18+
const browser = await next.browser('/server', {
19+
disableJavaScript: true,
20+
})
21+
22+
await browser.eval(`document.getElementById('name').value = 'test'`)
23+
await browser.elementByCss('#submit').click()
24+
25+
await check(() => {
26+
return browser.eval('window.location.pathname + window.location.search')
27+
}, '/header?name=test&constructor=FormData&hidden-info=hi')
28+
})
29+
30+
it('should support actions from client without JS', async () => {
31+
const browser = await next.browser('/server', {
32+
disableJavaScript: true,
33+
})
34+
35+
await browser.eval(
36+
`document.getElementById('client-name').value = 'test'`
37+
)
38+
await browser.elementByCss('#there').click()
39+
40+
await check(() => {
41+
return browser.eval('window.location.pathname + window.location.search')
42+
}, '/header?name=test&constructor=FormData&hidden-info=hi')
43+
})
44+
}
45+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
'use server'
22

3+
import { redirect } from 'next/navigation'
4+
35
export async function inc(value) {
46
return value + 1
57
}
68

79
export default async function dec(value) {
810
return value - 1
911
}
12+
13+
export async function redirectAction(formData) {
14+
'use server'
15+
redirect(
16+
'/header?name=' +
17+
formData.get('name') +
18+
'&constructor=' +
19+
formData.constructor.name +
20+
'&hidden-info=' +
21+
formData.get('hidden-info')
22+
)
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use client'
2+
3+
import { redirectAction } from './actions'
4+
5+
export default function Form() {
6+
return (
7+
<form>
8+
<input type="text" name="hidden-info" value="hi" hidden />
9+
<input type="text" name="name" id="client-name" required />
10+
<button formAction={redirectAction} type="submit" id="there">
11+
Go there
12+
</button>
13+
</form>
14+
)
15+
}

test/e2e/app-dir/actions/app/server/page.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Counter from './counter'
22
import Form from './form'
3+
import ClientForm from './client-form'
34

45
import dec, { inc } from './actions'
56
import { log } from './actions-2'
@@ -17,6 +18,7 @@ export default function Page() {
1718
}}
1819
/>
1920
<Form />
21+
<ClientForm />
2022
<form>
2123
<button id="log" formAction={log}>
2224
log

0 commit comments

Comments
 (0)