Skip to content

Commit 8469d0a

Browse files
shudingijjk
andauthored
Add test case for the permalink option of useFormState (#56329)
Ref: facebook/react#27372. Co-authored-by: JJ Kasper <jj@jjsweb.site>
1 parent b79a11f commit 8469d0a

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

test/e2e/app-dir/actions/app-action-form-state.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,21 @@ createNextDescribe(
5353
return browser.elementByCss('#hydrated').text()
5454
}, 'hydrated')
5555
})
56+
57+
it('should send the action to the provided permalink with form state when JS disabled', async () => {
58+
const browser = await next.browser('/client/form-state/page-2', {
59+
disableJavaScript: true,
60+
})
61+
62+
// Simulate a progressively enhanced form request
63+
await browser.eval(
64+
`document.getElementById('name-input').value = 'test-permalink'`
65+
)
66+
await browser.eval(`document.getElementById('form-state-form').submit()`)
67+
68+
await check(() => {
69+
return browser.elementByCss('#form-state').text()
70+
}, 'initial-state:test-permalink')
71+
})
5672
}
5773
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use client'
2+
3+
import { experimental_useFormState } from 'react-dom'
4+
import { appendName } from '../actions'
5+
import { useEffect, useState } from 'react'
6+
7+
export default function Page() {
8+
const [state, appendNameFormAction] = experimental_useFormState(
9+
appendName,
10+
'initial-state',
11+
'/client/form-state'
12+
)
13+
14+
const [hydrated, setHydrated] = useState(false)
15+
useEffect(() => {
16+
setHydrated(true)
17+
}, [])
18+
19+
return (
20+
<>
21+
<form id="form-state-form" action={appendNameFormAction}>
22+
<p id="form-state">{state}</p>
23+
<input id="name-input" name="name" />
24+
<button id="submit-form" type="submit">
25+
log
26+
</button>
27+
</form>
28+
{hydrated ? <p id="hydrated">hydrated</p> : null}
29+
</>
30+
)
31+
}

test/e2e/app-dir/actions/app/client/form-state/page.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { useEffect, useState } from 'react'
77
export default function Page() {
88
const [state, appendNameFormAction] = experimental_useFormState(
99
appendName,
10-
'initial-state'
10+
'initial-state',
11+
'/client/form-state'
1112
)
1213

1314
const [hydrated, setHydrated] = useState(false)

0 commit comments

Comments
 (0)