Skip to content

Commit 1d25312

Browse files
committed
Add test suite
1 parent 42ae07f commit 1d25312

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Root({ children }: { children: React.ReactNode }) {
2+
return (
3+
<html>
4+
<body>{children}</body>
5+
</html>
6+
)
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
async function getCachedRandom(x: number) {
2+
'use cache'
3+
return {
4+
x,
5+
y: Math.random(),
6+
}
7+
}
8+
9+
export default async function Page({
10+
searchParams,
11+
}: {
12+
searchParams: Promise<{ n: string }>
13+
}) {
14+
const n = +(await searchParams).n
15+
const values = await getCachedRandom(n)
16+
return (
17+
<>
18+
<p id="x">{values.x}</p>
19+
<p id="y">{values.y}</p>
20+
</>
21+
)
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {
5+
experimental: {
6+
dynamicIO: true,
7+
},
8+
}
9+
10+
module.exports = nextConfig
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @ts-check
2+
import { nextTestSetup } from 'e2e-utils'
3+
4+
describe('use-cache', () => {
5+
const { next } = nextTestSetup({
6+
files: __dirname,
7+
})
8+
9+
it('should cache results', async () => {
10+
const browser = await next.browser('/?n=1')
11+
expect(await browser.waitForElementByCss('#x').text()).toBe('1')
12+
const random1a = await browser.waitForElementByCss('#y').text()
13+
14+
await browser.loadPage(new URL('/?n=2', next.url).toString())
15+
expect(await browser.waitForElementByCss('#x').text()).toBe('2')
16+
const random2 = await browser.waitForElementByCss('#y').text()
17+
18+
await browser.loadPage(new URL('/?n=1&unrelated', next.url).toString())
19+
expect(await browser.waitForElementByCss('#x').text()).toBe('1')
20+
const random1b = await browser.waitForElementByCss('#y').text()
21+
22+
// The two navigations to n=1 should use a cached value.
23+
expect(random1a).toBe(random1b)
24+
25+
// The navigation to n=2 should be some other random value.
26+
expect(random1a).not.toBe(random2)
27+
})
28+
})

0 commit comments

Comments
 (0)