Skip to content

Commit

Permalink
test: reorganize the default test
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Nov 15, 2024
1 parent 1e720ff commit 4d8260d
Show file tree
Hide file tree
Showing 20 changed files with 172 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { nextTestSetup } from 'e2e-utils'

describe('app dir - forbidden - basic', () => {
describe('app dir - forbidden with customized boundary', () => {
const { next } = nextTestSetup({
files: __dirname,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { unauthorized } from 'next/navigation'

export default async function Page(props) {
const params = await props.params
if (params.id === '401') {
unauthorized()
}

return <p id="page">{`group-dynamic [id]`}</p>
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/unauthorized/default/app/(group)/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function RootLayout({ children }) {
return <div className="group-root-layout">{children}</div>
}
28 changes: 28 additions & 0 deletions test/e2e/app-dir/unauthorized/default/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use client'

import { useState, Suspense } from 'react'
import { unauthorized } from 'next/navigation'
import ForbiddenTrigger from './unauthorized-trigger'

export default function Root({ children }) {
const [clicked, setClicked] = useState(false)
if (clicked) {
unauthorized()
}

return (
<html className="root-layout-html">
<body>
<Suspense fallback={<div>Loading...</div>}>
<ForbiddenTrigger />
</Suspense>
<button id="trigger-unauthorized" onClick={() => setClicked(true)}>
Click to unauthorized
</button>
{children}
</body>
</html>
)
}

export const dynamic = 'force-dynamic'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { unauthorized } from 'next/navigation'

export async function generateMetadata() {
unauthorized()
}

export default function layout({ children }) {
return children
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function page() {
return 'not found metadata page'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { unauthorized } from 'next/navigation'

export default function page() {
unauthorized()
}
22 changes: 22 additions & 0 deletions test/e2e/app-dir/unauthorized/default/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Link from 'next/link'

export default function Page() {
return (
<div>
<p>hello world</p>
<div>
<Link id="navigate-unauthorized" href="/navigate-unauthorized">
navigate to page calling unauthorized()
</Link>
</div>
<div>
<Link
id="metadata-layout-unauthorized"
href="/metadata-layout-unauthorized"
>
navigate to layout with metadata API calling unauthorized()
</Link>
</div>
</div>
)
}
12 changes: 12 additions & 0 deletions test/e2e/app-dir/unauthorized/default/app/unauthorized-trigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use client'

import { useSearchParams, unauthorized } from 'next/navigation'

export default function ForbiddenTrigger() {
const searchParams = useSearchParams()

if (searchParams.get('root-unauthorized')) {
unauthorized()
}
return null
}
79 changes: 79 additions & 0 deletions test/e2e/app-dir/unauthorized/default/unauthorized-default.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { nextTestSetup } from 'e2e-utils'
import {
assertHasRedbox,
assertNoRedbox,
getRedboxDescription,
} from 'next-test-utils'

describe('app dir - unauthorized with default unauthorized boundary', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})

if (skipped) {
return
}

// TODO: error unauthorized usage in root layout
it.skip('should error on client unauthorized from root layout in browser', async () => {
const browser = await next.browser('/')

await browser.elementByCss('#trigger-unauthorized').click()

if (isNextDev) {
await assertHasRedbox(browser)
expect(await getRedboxDescription(browser)).toMatch(
/unauthorized\(\) is not allowed to use in root layout/
)
}
})

// TODO: error unauthorized usage in root layout
it.skip('should error on server unauthorized from root layout on server-side', async () => {
const browser = await next.browser('/?root-unauthorized=1')

if (isNextDev) {
await assertHasRedbox(browser)
expect(await getRedboxDescription(browser)).toBe(
'Error: unauthorized() is not allowed to use in root layout'
)
}
})

it('should be able to navigate to page calling unauthorized', async () => {
const browser = await next.browser('/')

await browser.elementByCss('#navigate-unauthorized').click()
await browser.waitForElementByCss('.next-error-h1')

expect(await browser.elementByCss('h1').text()).toBe('401')
expect(await browser.elementByCss('h2').text()).toBe(
`You're not authorized to access this page.`
)
})

it('should be able to navigate to page with calling unauthorized in metadata', async () => {
const browser = await next.browser('/')

await browser.elementByCss('#metadata-layout-unauthorized').click()
await browser.waitForElementByCss('.next-error-h1')

expect(await browser.elementByCss('h1').text()).toBe('401')
expect(await browser.elementByCss('h2').text()).toBe(
`You're not authorized to access this page.`
)
})

it('should render default unauthorized for group routes if unauthorized is not defined', async () => {
const browser = await next.browser('/group-dynamic/123')
expect(await browser.elementByCss('#page').text()).toBe(
'group-dynamic [id]'
)

await browser.loadPage(next.url + '/group-dynamic/401')
await assertNoRedbox(browser)
await browser.waitForElementByCss('.group-root-layout')
expect(await browser.elementByCss('.next-error-h1').text()).toBe('401')
})
})

0 comments on commit 4d8260d

Please sign in to comment.