Skip to content

Commit 95adcd3

Browse files
committed
add tests
1 parent 9cf9183 commit 95adcd3

File tree

13 files changed

+252
-0
lines changed

13 files changed

+252
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const metadata = {
2+
title: 'Next.js',
3+
description: 'Generated by Next.js',
4+
}
5+
6+
export default function RootLayout({
7+
children,
8+
}: {
9+
children: React.ReactNode
10+
}) {
11+
return (
12+
<html lang="en">
13+
<body>{children}</body>
14+
</html>
15+
)
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { LinkAccordion } from '../../components/link-accordion'
2+
3+
export default async function Page() {
4+
return (
5+
<main>
6+
<h1>
7+
A page that links to another page which uses an async server reference
8+
</h1>
9+
<LinkAccordion href="/prefetch/target-page">
10+
/prefetch/target-page
11+
</LinkAccordion>
12+
</main>
13+
)
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use server'
2+
3+
await Promise.resolve() // make this an async module
4+
5+
export async function action() {
6+
console.log('hello from the server!')
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { action } from './async-module-with-actions'
2+
3+
export default async function Page() {
4+
return (
5+
<main id="target-page">
6+
<h1>A page that uses an async server reference</h1>
7+
<form action={action}>
8+
<button type="submit">Submit</button>
9+
</form>
10+
</main>
11+
)
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use server'
2+
3+
await Promise.resolve() // make this an async module
4+
5+
const EXPECTED_VALUE = 1
6+
7+
export async function runActionFromArgument(action: () => Promise<number>) {
8+
console.log('runActionFromArgument :: running action:', action)
9+
const result = await action()
10+
if (result !== EXPECTED_VALUE) {
11+
throw new Error(`Action did not return ${EXPECTED_VALUE}`)
12+
}
13+
}
14+
15+
export async function myAction(): Promise<1> {
16+
console.log('hello from the server!')
17+
return EXPECTED_VALUE
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use client'
2+
import { useActionState } from 'react'
3+
import { runActionFromArgument, myAction } from './async-module-with-actions'
4+
5+
export function Client() {
6+
const [state, dispatch, isPending] = useActionState(async () => {
7+
try {
8+
// This will execute the action passsed as an argument,
9+
// and throw if something goes wrong.
10+
await runActionFromArgument(myAction)
11+
return 'ok'
12+
} catch (err) {
13+
return 'error'
14+
}
15+
}, null)
16+
return (
17+
<div>
18+
<form action={dispatch}>
19+
<button type="submit">Submit</button>
20+
</form>
21+
{isPending
22+
? 'Submitting...'
23+
: state !== null && <div id="action-result">{state}</div>}
24+
</div>
25+
)
26+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Client } from './client'
2+
3+
export default function Page() {
4+
return <Client />
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use server'
2+
3+
import { redirect } from 'next/navigation'
4+
5+
await Promise.resolve() // make this an async module
6+
7+
export async function action() {
8+
console.log('hello from server! redirecting...')
9+
redirect('/use-cache/redirect-target')
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { action } from './async-module-with-actions'
2+
3+
export default async function Page() {
4+
return (
5+
<main>
6+
<h1>
7+
A page that uses a cached component whose result contains an async
8+
server reference
9+
</h1>
10+
<Cached />
11+
</main>
12+
)
13+
}
14+
15+
async function Cached() {
16+
'use cache'
17+
// 'use cache' decodes and re-encodes RSC data on the server,
18+
// so it can break if async references are not resolved correctly.
19+
return (
20+
<form action={action}>
21+
<button type="submit">Submit</button>
22+
</form>
23+
)
24+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <main id="redirect-target">This page is a redirect target.</main>
3+
}

0 commit comments

Comments
 (0)