Unverified Commit dbfb6c49 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Add failing test for `useActionState` with `'use cache'` (#86292)

Adds a test case demonstrating that `useActionState` doesn't work correctly when the `'use cache'` function is exported separately, due to wrong function arity information in the server reference information byte. This is currently a limitation of the Next.js compiler, which will be addressed in a future PR.

Also cleans up `console.log` from the existing `useActionState` test.
parent e66e0eab
'use cache'
const getRandomValue = async () => {
return Math.random()
}
export { getRandomValue }
'use client'
import { useActionState } from 'react'
import { getRandomValue } from './cached'
export function Form() {
const [result, formAction, isPending] = useActionState(getRandomValue, -1)
return (
<form action={formAction}>
<button id="submit-button">Submit</button>
<p>{isPending ? 'loading...' : result}</p>
</form>
)
}
import { Form } from './form'
export default function Page() {
return <Form />
}
'use cache' 'use cache'
export async function getRandomValue() { export async function getRandomValue() {
const v = Math.random() return Math.random()
console.log(v)
return v
} }
...@@ -499,6 +499,7 @@ describe('use-cache', () => { ...@@ -499,6 +499,7 @@ describe('use-cache', () => {
'/static-class-method', '/static-class-method',
withCacheComponents && '/unhandled-promise-regression', withCacheComponents && '/unhandled-promise-regression',
'/use-action-state', '/use-action-state',
'/use-action-state-separate-export',
'/with-server-action', '/with-server-action',
].filter(Boolean) ].filter(Boolean)
) )
...@@ -709,6 +710,34 @@ describe('use-cache', () => { ...@@ -709,6 +710,34 @@ describe('use-cache', () => {
}) })
}) })
// TODO: This test doesn't work currently because the compiler doesn't
// properly compute the server reference information byte that includes the
// function arity. Without this information, the client can't optimize the
// arguments it sends to the server, so the (unused) previous state is also
// sent as an argument, leading to cache misses.
it.failing(
'works with useActionState if previousState parameter is not used in "use cache" function (separate export)',
async () => {
const browser = await next.browser('/use-action-state-separate-export')
let value = await browser.elementByCss('p').text()
expect(value).toBe('-1')
await browser.elementByCss('button').click()
await retry(async () => {
value = await browser.elementByCss('p').text()
expect(value).toMatch(/\d\.\d+/)
})
await browser.elementByCss('button').click()
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe(value)
})
}
)
it('works with "use cache" in method props', async () => { it('works with "use cache" in method props', async () => {
const browser = await next.browser('/method-props') const browser = await next.browser('/method-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