Skip to content

Commit 6b28b3b

Browse files
committed
test: first batch of actual integration tests
1 parent 32b550d commit 6b28b3b

File tree

9 files changed

+373
-111
lines changed

9 files changed

+373
-111
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { revalidateTag } from 'next/cache'
2+
import { NextRequest } from 'next/server'
3+
4+
export async function GET(request: NextRequest, { params }) {
5+
const { slug } = await params
6+
7+
const tagToInvalidate = slug.join('/')
8+
9+
revalidateTag(tagToInvalidate)
10+
11+
return Response.json({ tagToInvalidate })
12+
}

tests/fixtures/use-cache/app/cache-fetch-no-store/page.tsx

-18
This file was deleted.

tests/fixtures/use-cache/app/cache-fetch/[slug]/page.tsx

-25
This file was deleted.

tests/fixtures/use-cache/app/cache-fetch/page.tsx

-25
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { unstable_cacheTag as cacheTag, unstable_cacheLife as cacheLife } from 'next/cache'
2+
import {
3+
getDataImplementation,
4+
Data,
5+
ResultComponentProps,
6+
PageComponentImplementation,
7+
ResultComponentImplementation,
8+
BasePageComponentProps,
9+
} from '../../../../../helpers'
10+
11+
async function getData(route: string) {
12+
'use cache'
13+
cacheTag(`data/${route}`)
14+
cacheLife('1year')
15+
16+
return await getDataImplementation(route)
17+
}
18+
19+
async function ResultComponent(props: ResultComponentProps) {
20+
return <ResultComponentImplementation {...props} />
21+
}
22+
23+
export default async function PageComponent({ params }: BasePageComponentProps) {
24+
return (
25+
<PageComponentImplementation
26+
routeRoot="default/dynamic/use-cache-data/ttl-1year"
27+
params={params}
28+
getData={getData}
29+
ResultComponent={ResultComponent}
30+
/>
31+
)
32+
}
33+
34+
export const dynamic = 'force-dynamic'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { unstable_cacheTag as cacheTag, unstable_cacheLife as cacheLife } from 'next/cache'
2+
import {
3+
getDataImplementation,
4+
Data,
5+
ResultComponentProps,
6+
PageComponentImplementation,
7+
ResultComponentImplementation,
8+
BasePageComponentProps,
9+
} from '../../../../../helpers'
10+
11+
async function getData(route: string) {
12+
'use cache'
13+
cacheTag(`data/${route}`)
14+
cacheLife('5seconds')
15+
16+
return await getDataImplementation(route)
17+
}
18+
19+
async function ResultComponent(props: ResultComponentProps) {
20+
return <ResultComponentImplementation {...props} />
21+
}
22+
23+
export default async function PageComponent({ params }: BasePageComponentProps) {
24+
return (
25+
<PageComponentImplementation
26+
routeRoot="default/dynamic/use-cache-data/ttl-5seconds"
27+
params={params}
28+
getData={getData}
29+
ResultComponent={ResultComponent}
30+
/>
31+
)
32+
}
33+
34+
export const dynamic = 'force-dynamic'
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
export type Data = {
2+
data: string
3+
time: string
4+
}
5+
6+
export async function getDataImplementation(route: string): Promise<Data> {
7+
console.log(`[${route}]`, 'Fetch Data')
8+
const res = await fetch(`https://strangerthings-quotes.vercel.app/api/quotes`)
9+
return {
10+
data: (await res.json())[0].quote,
11+
time: new Date().toISOString(),
12+
}
13+
}
14+
15+
export type ResultComponentProps = Data & { route: string }
16+
17+
export async function ResultComponentImplementation({ route, data, time }: ResultComponentProps) {
18+
console.log(`[${route}]`, 'Render ResultComponent')
19+
20+
return (
21+
<>
22+
<dt>Quote (getData)</dt>
23+
<dd data-testid="getData-data">{data}</dd>
24+
<dt>Time (getData)</dt>
25+
<dd data-testid="getData-time">{time}</dd>
26+
<dt>Time (GetDataResult)</dt>
27+
<dd data-testid="ResultComponent-time">{new Date().toISOString()}</dd>
28+
</>
29+
)
30+
}
31+
32+
export type BasePageComponentProps = {
33+
params: Promise<{ slug: string }>
34+
}
35+
36+
export async function PageComponentImplementation({
37+
getData,
38+
ResultComponent,
39+
params,
40+
routeRoot,
41+
}: BasePageComponentProps & {
42+
routeRoot: string
43+
getData: typeof getDataImplementation
44+
ResultComponent: typeof ResultComponentImplementation
45+
}) {
46+
const { slug } = await params
47+
const route = `${routeRoot}/${slug}`
48+
const data = await getData(route)
49+
50+
console.log(`[${route}]`, 'Render PageComponent')
51+
52+
return (
53+
<>
54+
<h1>Hello, use-cache - {route}</h1>
55+
<dl>
56+
<ResultComponent {...data} route={route} />
57+
<dt>Time (PageComponent)</dt>
58+
<dd data-testid="PageComponent-time">{new Date().toISOString()}</dd>
59+
</dl>
60+
</>
61+
)
62+
}

tests/fixtures/use-cache/next.config.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const INFINITE_CACHE = 0xfffffffe
2+
3+
const ONE_YEAR = 365 * 24 * 60 * 60
4+
15
/**
26
* @type {import('next').NextConfig}
37
*/
@@ -8,13 +12,18 @@ const nextConfig = {
812
},
913
experimental: {
1014
useCache: true,
11-
// cacheLife: {
12-
// frequent: {
13-
// stale: 19,
14-
// revalidate: 100,
15-
// expire: 250,
16-
// },
17-
// },
15+
cacheLife: {
16+
'5seconds': {
17+
stale: 5,
18+
revalidate: 5,
19+
expire: INFINITE_CACHE,
20+
},
21+
'1year': {
22+
stale: ONE_YEAR,
23+
revalidate: ONE_YEAR,
24+
expire: INFINITE_CACHE,
25+
},
26+
},
1827
},
1928
}
2029

0 commit comments

Comments
 (0)