Skip to content

Commit

Permalink
test: break down metadata test suite into smaller ones (vercel#67018)
Browse files Browse the repository at this point in the history
### What

app-dir `metadata.test.ts` is pretty big and includes a lot of erroring
tests and navigation tests. Breaking them into smaller suites to avoid
the erroring on effect on others.

- metadata
- metadata-navigation
- metadata-thrown

Moved the metadata testing utils into `next-test-utils` for sharing
purpose.
Moved the hmr test to the bottom to avoid flakyness.
  • Loading branch information
huozhi authored Jun 19, 2024
1 parent 9ff5c44 commit 54f5442
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 246 deletions.
14 changes: 14 additions & 0 deletions test/e2e/app-dir/metadata-navigation/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function Layout({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}

export const metadata = {
title: 'this is the layout title',
description: 'this is the layout description',
keywords: ['nextjs', 'react'],
}
File renamed without changes.
76 changes: 76 additions & 0 deletions test/e2e/app-dir/metadata-navigation/metadata-navigation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { nextTestSetup } from 'e2e-utils'
import {
createMultiDomMatcher,
createMultiHtmlMatcher,
getTitle,
} from 'next-test-utils'

describe('app dir - metadata navigation', () => {
const { next } = nextTestSetup({
files: __dirname,
})

describe('navigation', () => {
it('should render root not-found with default metadata', async () => {
const $ = await next.render$('/does-not-exist')

// Should contain default metadata and noindex tag
const matchHtml = createMultiHtmlMatcher($)
expect($('meta[charset="utf-8"]').length).toBe(1)
matchHtml('meta', 'name', 'content', {
viewport: 'width=device-width, initial-scale=1',
robots: 'noindex',
// not found metadata
description: 'Root not found description',
})
expect(await $('title').text()).toBe('Root not found')
})

it('should support notFound in generateMetadata', async () => {
const res = await next.fetch('/async/not-found')
expect(res.status).toBe(404)
const $ = await next.render$('/async/not-found')

// TODO-APP: support render custom not-found in SSR for generateMetadata.
// Check contains root not-found payload in flight response for now.
let hasRootNotFoundFlight = false
for (const el of $('script').toArray()) {
const text = $(el).text()
if (text.includes('Local found boundary')) {
hasRootNotFoundFlight = true
}
}
expect(hasRootNotFoundFlight).toBe(true)

// Should contain default metadata and noindex tag
const matchHtml = createMultiHtmlMatcher($)
expect($('meta[charset="utf-8"]').length).toBe(1)
matchHtml('meta', 'name', 'content', {
viewport: 'width=device-width, initial-scale=1',
robots: 'noindex',
})

const browser = await next.browser('/async/not-found')
expect(await browser.elementByCss('h2').text()).toBe(
'Local found boundary'
)

const matchMultiDom = createMultiDomMatcher(browser)
await matchMultiDom('meta', 'name', 'content', {
viewport: 'width=device-width, initial-scale=1',
keywords: 'parent',
robots: 'noindex',
// not found metadata
description: 'Local not found description',
})
expect(await getTitle(browser)).toBe('Local not found')
})

it('should support redirect in generateMetadata', async () => {
const res = await next.fetch('/async/redirect', {
redirect: 'manual',
})
expect(res.status).toBe(307)
})
})
})
12 changes: 12 additions & 0 deletions test/e2e/app-dir/metadata-thrown/metadata-thrown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { nextTestSetup } from 'e2e-utils'

describe('app dir - metadata thrown', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should not crash from error thrown during preloading nested generateMetadata', async () => {
const res = await next.fetch('/dynamic-meta')
expect(res.status).toBe(404)
})
})
19 changes: 19 additions & 0 deletions test/e2e/app-dir/metadata/app/dynamic/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function format({ params, searchParams }) {
const { slug } = params
const { q } = searchParams
return `params - ${slug}${q ? ` query - ${q}` : ''}`
}

export default function page(props) {
return <p>{format(props)}</p>
}

export async function generateMetadata(props, parent) {
const parentMetadata = await parent
/* mutating */
return {
...parentMetadata,
title: format(props),
keywords: parentMetadata.keywords.concat(['child']),
}
}
11 changes: 11 additions & 0 deletions test/e2e/app-dir/metadata/app/dynamic/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function layout({ children }) {
return children
}

export async function generateMetadata() {
return {
keywords: 'parent',
}
}

export const revalidate = 0
Loading

0 comments on commit 54f5442

Please sign in to comment.