Skip to content

Commit

Permalink
Add test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Sep 25, 2024
1 parent 42ae07f commit 1d25312
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/e2e/app-dir/use-cache/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
22 changes: 22 additions & 0 deletions test/e2e/app-dir/use-cache/app/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<p id="x">{values.x}</p>
<p id="y">{values.y}</p>
</>
)
}
10 changes: 10 additions & 0 deletions test/e2e/app-dir/use-cache/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
dynamicIO: true,
},
}

module.exports = nextConfig
28 changes: 28 additions & 0 deletions test/e2e/app-dir/use-cache/use-cache.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit 1d25312

Please sign in to comment.