Skip to content

Commit

Permalink
Add failing test for "use cache" with useActionState
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Nov 8, 2024
1 parent 0c22b00 commit aab8c17
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/e2e/app-dir/use-cache/app/use-action-state/cached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use cache'

export async function getRandomValue() {
const v = Math.random()
console.log(v)
return v
}
15 changes: 15 additions & 0 deletions test/e2e/app-dir/use-cache/app/use-action-state/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use client'

import { useActionState } from 'react'
import { getRandomValue } from './cached'

export default function Page() {
const [result, formAction, isPending] = useActionState(getRandomValue, -1)

return (
<form action={formAction}>
<button id="submit-button">Submit</button>
<p>{isPending ? 'loading...' : result}</p>
</form>
)
}
20 changes: 20 additions & 0 deletions test/e2e/app-dir/use-cache/use-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,24 @@ describe('use-cache', () => {

expect(await browser.elementByCss('#random').text()).toBe(initialValue)
})

it('works with useActionState if previousState parameter is not used in "use cache" function', async () => {
const browser = await next.browser('/use-action-state')

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)
})
})
})

0 comments on commit aab8c17

Please # to comment.