Skip to content

Commit

Permalink
tests: added test for inner unstable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Apr 25, 2024
1 parent 4780f21 commit be30092
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 15 deletions.
100 changes: 85 additions & 15 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import globOrig from 'glob'
import http from 'node:http'
import cheerio from 'cheerio'
import { promisify } from 'util'
import { join } from 'path'
import { promisify } from 'node:util'
import { join } from 'node:path'
import { createNextDescribe } from 'e2e-utils'
import {
check,
fetchViaHTTP,
findPort,
normalizeRegEx,
retry,
waitFor,
Expand Down Expand Up @@ -216,24 +218,22 @@ createNextDescribe(
if (isApi) {
prevData = await res.json()
} else {
const initialHtml = await res.text()
const initial$ = isApi ? undefined : cheerio.load(initialHtml)
prevData = JSON.parse(initial$('#props').text())
const $ = cheerio.load(await res.text())
prevData = JSON.parse($('#props').text())
}

expect(prevData.data.random).toBeTruthy()

await check(async () => {
await retry(async () => {
res = await next.fetch(pathname)
expect(res.status).toBe(200)
let curData

let curData
if (isApi) {
curData = await res.json()
} else {
const curHtml = await res.text()
const cur$ = cheerio.load(curHtml)
curData = JSON.parse(cur$('#props').text())
const $ = cheerio.load(await res.text())
curData = JSON.parse($('#props').text())
}

try {
Expand All @@ -242,8 +242,7 @@ createNextDescribe(
} finally {
prevData = curData
}
return 'success'
}, 'success')
})
})

it('should not have cache tags header for non-minimal mode', async () => {
Expand Down Expand Up @@ -2338,7 +2337,7 @@ createNextDescribe(
/partial-gen-params fetch ([\d]{1,})/
)

if (matches[1]) {
if (matches?.[1]) {
langFetchSlug = matches[1]
slugFetchSlug = langFetchSlug
}
Expand Down Expand Up @@ -3047,8 +3046,8 @@ createNextDescribe(
})
expect(res.status).toBe(404)
const html = await res.text()
expect(html).toInclude('"noindex"')
expect(html).toInclude('This page could not be found.')
expect(html).toContain('"noindex"')
expect(html).toContain('This page could not be found.')
})

// TODO-APP: support fetch revalidate case for dynamic rendering
Expand Down Expand Up @@ -3253,6 +3252,77 @@ createNextDescribe(
})

describe('unstable_cache', () => {
describe('fetch', () => {
let server: http.Server | null = null
afterEach(async () => {
if (!server) return

await server.close()
server = null
})

it('should not cache inner fetch calls', async () => {
let generations: string[] = []
server = http.createServer(async (key, res) => {
const random = Math.floor(Math.random() * 100).toString()
generations.push(random)
res.end(random)
})
const port = await findPort()
server.listen(port)
const address = `http://localhost:${port}/`

const first = await next
.fetch('/unstable-cache/fetch', {
headers: {
'X-Test-Data-Server': address,
},
})
.then((res) => res.json())

expect(generations).toHaveLength(1)
expect(first).toEqual(
expect.objectContaining({
data: generations[0],
})
)

const second = await next
.fetch('/unstable-cache/fetch', {
headers: {
'X-Test-Data-Server': address,
},
})
.then((res) => res.json())

expect(generations).toHaveLength(1)
expect(first).toEqual(second)

// Revalidate the cache for the unstable_cache, but explicitly not
// the inner fetch call. We expect it to not cache either.
await next.fetch('/unstable-cache/fetch?tag=unstable-cache-fetch', {
method: 'DELETE',
})

const third = await next
.fetch('/unstable-cache/fetch', {
headers: {
'X-Test-Data-Server': address,
},
})
.then((res) => res.json())

expect(generations).toHaveLength(2)
expect(generations[1]).not.toEqual(generations[0])
expect(third).toEqual(
expect.objectContaining({
data: generations[1],
})
)
expect(third).not.toEqual(first)
})
})

it('should retrieve the same value on second request', async () => {
const res = await next.fetch('/unstable-cache/dynamic')
const html = await res.text()
Expand Down
49 changes: 49 additions & 0 deletions test/e2e/app-dir/app-static/app/unstable-cache/fetch/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { revalidateTag, unstable_cache } from 'next/cache'

export const dynamic = 'force-dynamic'

const getData = unstable_cache(
async (address: string): Promise<string> => {
const res = await fetch(address, {
cache: 'force-cache',
next: { tags: ['unstable-cache-inner-fetch'] },
})

const data = await res.text()

return JSON.stringify({
random: Math.floor(Math.random() * 100).toString(),
data,
})
},
undefined,
{
tags: ['unstable-cache-fetch'],
}
)

export const GET = async (request: Request): Promise<Response> => {
const address = request.headers.get('x-test-data-server')
if (!address) {
return new Response('Missing address', { status: 400 })
}

const data = await getData(address)

return new Response(data, {
headers: {
'Content-Type': 'application/json',
},
})
}

export const DELETE = async (request: Request): Promise<Response> => {
const url = new URL(request.url)
const tags = url.searchParams.getAll('tag')

for (const tag of tags) {
revalidateTag(tag)
}

return new Response('OK', { status: 200 })
}

0 comments on commit be30092

Please sign in to comment.