diff --git a/test/e2e/app-dir/use-cache/app/layout.tsx b/test/e2e/app-dir/use-cache/app/layout.tsx new file mode 100644 index 0000000000000..e7077399c03ce --- /dev/null +++ b/test/e2e/app-dir/use-cache/app/layout.tsx @@ -0,0 +1,7 @@ +export default function Root({ children }: { children: React.ReactNode }) { + return ( + + {children} + + ) +} diff --git a/test/e2e/app-dir/use-cache/app/page.tsx b/test/e2e/app-dir/use-cache/app/page.tsx new file mode 100644 index 0000000000000..09ba77896e320 --- /dev/null +++ b/test/e2e/app-dir/use-cache/app/page.tsx @@ -0,0 +1,22 @@ +async function getCachedRandom(x: number) { + 'use cache' + return { + x, + y: Math.random(), + } +} + +export default async function Page({ + searchParams, +}: { + searchParams: Promise<{ n: string }> +}) { + const n = +(await searchParams).n + const values = await getCachedRandom(n) + return ( + <> +

{values.x}

+

{values.y}

+ + ) +} diff --git a/test/e2e/app-dir/use-cache/next.config.js b/test/e2e/app-dir/use-cache/next.config.js new file mode 100644 index 0000000000000..ac4afcf432196 --- /dev/null +++ b/test/e2e/app-dir/use-cache/next.config.js @@ -0,0 +1,10 @@ +/** + * @type {import('next').NextConfig} + */ +const nextConfig = { + experimental: { + dynamicIO: true, + }, +} + +module.exports = nextConfig diff --git a/test/e2e/app-dir/use-cache/use-cache.test.ts b/test/e2e/app-dir/use-cache/use-cache.test.ts new file mode 100644 index 0000000000000..922838f628b6d --- /dev/null +++ b/test/e2e/app-dir/use-cache/use-cache.test.ts @@ -0,0 +1,28 @@ +// @ts-check +import { nextTestSetup } from 'e2e-utils' + +describe('use-cache', () => { + const { next } = nextTestSetup({ + files: __dirname, + }) + + it('should cache results', async () => { + const browser = await next.browser('/?n=1') + expect(await browser.waitForElementByCss('#x').text()).toBe('1') + const random1a = await browser.waitForElementByCss('#y').text() + + await browser.loadPage(new URL('/?n=2', next.url).toString()) + expect(await browser.waitForElementByCss('#x').text()).toBe('2') + const random2 = await browser.waitForElementByCss('#y').text() + + await browser.loadPage(new URL('/?n=1&unrelated', next.url).toString()) + expect(await browser.waitForElementByCss('#x').text()).toBe('1') + const random1b = await browser.waitForElementByCss('#y').text() + + // The two navigations to n=1 should use a cached value. + expect(random1a).toBe(random1b) + + // The navigation to n=2 should be some other random value. + expect(random1a).not.toBe(random2) + }) +})