-
Notifications
You must be signed in to change notification settings - Fork 27.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: fix tests to work with new implementation
- Loading branch information
Showing
8 changed files
with
179 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
test/e2e/app-dir/app-static/app/unstable-cache/fetch/route.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Layout({ children }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { unstable_cache } from 'next/cache' | ||
|
||
const getData = unstable_cache( | ||
async () => { | ||
const noStore = await fetch( | ||
process.env.TEST_DATA_SERVER + '?cache=no-store', | ||
{ method: 'GET', cache: 'no-store' } | ||
).then((res) => res.text()) | ||
|
||
const forceCache = await fetch( | ||
process.env.TEST_DATA_SERVER + '?cache=force-cache', | ||
{ method: 'GET', cache: 'force-cache' } | ||
).then((res) => res.text()) | ||
|
||
return JSON.stringify( | ||
{ | ||
random: Math.floor(Math.random() * 1000).toString(), | ||
data: { | ||
forceCache, | ||
noStore, | ||
}, | ||
}, | ||
null, | ||
2 | ||
) | ||
}, | ||
undefined, | ||
{ | ||
tags: ['unstable-cache-fetch'], | ||
} | ||
) | ||
|
||
export default async function Page() { | ||
const data = await getData() | ||
|
||
return <pre id="data">{data}</pre> | ||
} |
6 changes: 6 additions & 0 deletions
6
test/e2e/app-dir/ppr-unstable-cache/app/revalidate-tag/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { revalidateTag } from 'next/cache' | ||
|
||
export const POST = async () => { | ||
revalidateTag('unstable-cache-fetch') | ||
return new Response('OK', { status: 200 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
experimental: { | ||
ppr: true, | ||
}, | ||
} |
103 changes: 103 additions & 0 deletions
103
test/e2e/app-dir/ppr-unstable-cache/ppr-unstable-cache.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { NextInstance, createNext, isNextDeploy, isNextDev } from 'e2e-utils' | ||
import { findPort } from 'next-test-utils' | ||
import http from 'node:http' | ||
|
||
describe('ppr-unstable-cache', () => { | ||
if (isNextDeploy) { | ||
it.skip('should not run in deploy mode', () => {}) | ||
return | ||
} | ||
|
||
if (isNextDev) { | ||
it.skip('should not run in dev mode', () => {}) | ||
return | ||
} | ||
|
||
let next: NextInstance | null = null | ||
let server: http.Server | null = null | ||
afterEach(async () => { | ||
if (next) { | ||
await next.destroy() | ||
next = null | ||
} | ||
|
||
if (server) { | ||
await server.close() | ||
server = null | ||
} | ||
}) | ||
|
||
it('should not cache inner fetch calls', async () => { | ||
let generations: string[] = [] | ||
server = http.createServer(async (req, res) => { | ||
try { | ||
if (!req.url) throw new Error('No URL') | ||
|
||
const cache = new URL(req.url, 'http://n').searchParams.get('cache') | ||
if (!cache) throw new Error('No cache key') | ||
|
||
const random = Math.floor(Math.random() * 1000).toString() | ||
const data = cache + ':' + random | ||
generations.push(data) | ||
res.end(data) | ||
} catch (err) { | ||
res.statusCode = 500 | ||
res.end(err.message) | ||
} | ||
}) | ||
const port = await findPort() | ||
server.listen(port) | ||
|
||
next = await createNext({ | ||
files: __dirname, | ||
env: { TEST_DATA_SERVER: `http://localhost:${port}/` }, | ||
}) | ||
|
||
expect(generations).toHaveLength(3) | ||
|
||
const first = await next | ||
.render$('/') | ||
.then(($) => JSON.parse($('#data').text())) | ||
|
||
expect(generations).toHaveLength(3) | ||
|
||
expect(first.data.forceCache).toBeOneOf(generations) | ||
expect(first.data.noStore).toBeOneOf(generations) | ||
|
||
// Try a few more times, we should always get the same result. | ||
for (let i = 0; i < 3; i++) { | ||
const again = await next | ||
.render$('/') | ||
.then(($) => JSON.parse($('#data').text())) | ||
|
||
expect(generations).toHaveLength(3) | ||
expect(first).toEqual(again) | ||
} | ||
|
||
// Revalidate the tag associated with the `unstable_cache` call. | ||
const revalidate = await next.fetch('/revalidate-tag', { method: 'POST' }) | ||
expect(revalidate.status).toBe(200) | ||
await revalidate.text() | ||
|
||
const revalidated = await next | ||
.render$('/') | ||
.then(($) => JSON.parse($('#data').text())) | ||
|
||
// Expect that the `cache: no-store` value has been updated, but not | ||
// the `cache: force-cache` value. | ||
expect(generations).toHaveLength(5) | ||
|
||
// We know now that the generations have been updated, so let's try to | ||
// validate the value. We don't need to do this within the retry. | ||
expect(revalidated.random).not.toEqual(first.random) | ||
expect(revalidated.data.forceCache).toBeOneOf(generations.slice(0, 3)) | ||
expect(revalidated.data.noStore).toBeOneOf(generations.slice(3)) | ||
expect(revalidated).not.toEqual(first) | ||
|
||
// Ensure that the `force-cache` value has not been updated, and only called | ||
// once. | ||
expect(generations.filter((g) => g.startsWith('force-cache'))).toHaveLength( | ||
1 | ||
) | ||
}) | ||
}) |