Skip to content

Commit

Permalink
Add test for next-found and redirect for metadata (#46248)
Browse files Browse the repository at this point in the history
Add tests for using `redirect` and `notFound` in `generateMetadata`
  • Loading branch information
huozhi authored Feb 22, 2023
1 parent aac3ecb commit ab123c1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions test/e2e/app-dir/metadata/app/async/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export async function generateMetadata() {
keywords: 'parent',
}
}

export const revalidate = 0
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata/app/async/not-found/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { notFound } from 'next/navigation'

export default function page() {
return 'not found'
return 'not-found-text'
}

export async function generateMetadata() {
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/app-dir/metadata/app/async/redirect/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { redirect } from 'next/navigation'

export default function page() {
return 'redirect to basic'
}

export async function generateMetadata() {
redirect('/basic')
}
11 changes: 11 additions & 0 deletions test/e2e/app-dir/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,17 @@ createNextDescribe(
)
})

it('should support notFound and redirect in generateMetadata', async () => {
const resNotFound = await next.fetch('/async/not-found')
expect(resNotFound.status).toBe(404)
const notFoundHtml = await resNotFound.text()
expect(notFoundHtml).not.toBe('not-found-text')
expect(notFoundHtml).toContain('This page could not be found.')

const resRedirect = await next.fetch('/async/redirect')
expect(resRedirect.status).toBe(307)
})

if (isNextDev) {
it('should freeze parent resolved metadata to avoid mutating in generateMetadata', async () => {
const pagePath = 'app/mutate/page.tsx'
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/navigation/app/layout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const revalidate = 0
export const dynamic = 'force-dynamic'

export default function Layout({ children }) {
return (
Expand Down

0 comments on commit ab123c1

Please sign in to comment.