Unverified Commit 3430ac15 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

New Router Tests (#38390)

* test: added some test cases for new client side router

* tests: added additional test cases

* test: improved test support

* feat: added support for `<Link soft />`

* tests: improved test reliability

* tests: added more cases for catch-all routes

* tests: added app -> pages -> app tests

* tests: add tests for useCookies + useHeaders

* tests: added tests for usePreviewData

* tests: added more tests for hooks and client components

* tests: added tests for query/params handling

* tests: fix tests for param/query to use props

* tests: added some skipped tests for unimplemented features

* tests: linting

* refactor: updated TODO -> TODO-APP

* tests: added some more test cases

* tests: skipped failing test
parent c1ce6bfe
...@@ -8,7 +8,10 @@ import { ...@@ -8,7 +8,10 @@ import {
} from '../shared/lib/router/router' } from '../shared/lib/router/router'
import { addLocale } from './add-locale' import { addLocale } from './add-locale'
import { RouterContext } from '../shared/lib/router-context' import { RouterContext } from '../shared/lib/router-context'
import { AppRouterContext } from '../shared/lib/app-router-context' import {
AppRouterContext,
AppRouterInstance,
} from '../shared/lib/app-router-context'
import { useIntersection } from './use-intersection' import { useIntersection } from './use-intersection'
import { getDomainLocale } from './get-domain-locale' import { getDomainLocale } from './get-domain-locale'
import { addBasePath } from './add-base-path' import { addBasePath } from './add-base-path'
...@@ -28,6 +31,11 @@ type InternalLinkProps = { ...@@ -28,6 +31,11 @@ type InternalLinkProps = {
href: Url href: Url
as?: Url as?: Url
replace?: boolean replace?: boolean
/**
* TODO-APP
*/
soft?: boolean
scroll?: boolean scroll?: boolean
shallow?: boolean shallow?: boolean
passHref?: boolean passHref?: boolean
...@@ -95,10 +103,11 @@ function isModifiedEvent(event: React.MouseEvent): boolean { ...@@ -95,10 +103,11 @@ function isModifiedEvent(event: React.MouseEvent): boolean {
function linkClicked( function linkClicked(
e: React.MouseEvent, e: React.MouseEvent,
router: NextRouter, router: NextRouter | AppRouterInstance,
href: string, href: string,
as: string, as: string,
replace?: boolean, replace?: boolean,
soft?: boolean,
shallow?: boolean, shallow?: boolean,
scroll?: boolean, scroll?: boolean,
locale?: string | false, locale?: string | false,
...@@ -117,12 +126,27 @@ function linkClicked( ...@@ -117,12 +126,27 @@ function linkClicked(
e.preventDefault() e.preventDefault()
const navigate = () => { const navigate = () => {
// replace state instead of push if prop is present // If the router is an AppRouterInstance, then it'll have `softPush` and
router[replace ? 'replace' : 'push'](href, as, { // `softReplace`.
shallow, if ('softPush' in router && 'softReplace' in router) {
locale, // If we're doing a soft navigation, use the soft variants of
scroll, // replace/push.
}) const method: keyof AppRouterInstance = soft
? replace
? 'softReplace'
: 'softPush'
: replace
? 'replace'
: 'push'
router[method](href)
} else {
router[replace ? 'replace' : 'push'](href, as, {
shallow,
locale,
scroll,
})
}
} }
if (startTransition) { if (startTransition) {
...@@ -183,6 +207,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -183,6 +207,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
const optionalPropsGuard: Record<LinkPropsOptional, true> = { const optionalPropsGuard: Record<LinkPropsOptional, true> = {
as: true, as: true,
replace: true, replace: true,
soft: true,
scroll: true, scroll: true,
shallow: true, shallow: true,
passHref: true, passHref: true,
...@@ -224,6 +249,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -224,6 +249,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
} }
} else if ( } else if (
key === 'replace' || key === 'replace' ||
key === 'soft' ||
key === 'scroll' || key === 'scroll' ||
key === 'shallow' || key === 'shallow' ||
key === 'passHref' || key === 'passHref' ||
...@@ -264,6 +290,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -264,6 +290,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
prefetch: prefetchProp, prefetch: prefetchProp,
passHref, passHref,
replace, replace,
soft,
shallow, shallow,
scroll, scroll,
locale, locale,
...@@ -416,6 +443,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>( ...@@ -416,6 +443,7 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
href, href,
as, as,
replace, replace,
soft,
shallow, shallow,
scroll, scroll,
locale, locale,
......
export function getServerSideProps({ params }) {
return { props: { params } }
}
export default function Page({ params }) {
return (
<h1 id="text" data-params={params.slug.join('/') ?? ''}>
hello from /catch-all/{params.slug.join('/')}
</h1>
)
}
export function getServerSideProps() {
return { props: {} }
}
export default function Page() {
return null
}
export function getStaticProps() {
return { props: {} }
}
export default function Page() {
return null
}
...@@ -2,7 +2,9 @@ import ClientComp from './client-comp.client' ...@@ -2,7 +2,9 @@ import ClientComp from './client-comp.client'
export default function DashboardPage(props) { export default function DashboardPage(props) {
return ( return (
<> <>
<p className="p">hello from app/dashboard</p> <p id="from-dashboard" className="p">
hello from app/dashboard
</p>
<p className="green">this is green</p> <p className="green">this is green</p>
<ClientComp /> <ClientComp />
</> </>
......
import { useCookies } from 'next/dist/client/components/hooks-server'
export default function Page() {
// This should throw an error.
useCookies()
return null
}
import { useCookies } from 'next/dist/client/components/hooks-server'
export default function Page() {
const cookies = useCookies()
const hasCookie =
'use-cookies' in cookies && cookies['use-cookies'] === 'value'
return (
<>
<h1 id="text">hello from /hooks/use-cookies</h1>
{hasCookie ? (
<h2 id="has-cookie">Has use-cookies cookie</h2>
) : (
<h2 id="does-not-have-cookie">Does not have use-cookies cookie</h2>
)}
</>
)
}
import { useHeaders } from 'next/dist/client/components/hooks-server'
export default function Page() {
// This should throw an error.
useHeaders()
return null
}
import { useHeaders } from 'next/dist/client/components/hooks-server'
export default function Page() {
const headers = useHeaders()
const hasHeader =
'x-use-headers' in headers && headers['x-use-headers'] === 'value'
return (
<>
<h1 id="text">hello from /hooks/use-headers</h1>
{hasHeader ? (
<h2 id="has-header">Has x-use-headers header</h2>
) : (
<h2 id="does-not-have-header">Does not have x-use-headers header</h2>
)}
{'referer' in headers && headers['referer'] && (
<h3 id="has-referer">Has referer header</h3>
)}
</>
)
}
import { useLayoutSegments } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
useLayoutSegments()
return null
}
import { useParams } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
useParams()
return null
}
import { usePathname } from 'next/dist/client/components/hooks-client'
export default function Page() {
const pathname = usePathname()
return (
<>
<h1 id="pathname" data-pathname={pathname}>
hello from /hooks/use-pathname
</h1>
</>
)
}
import { usePathname } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
usePathname()
return null
}
import { usePreviewData } from 'next/dist/client/components/hooks-server'
export default function Page() {
// This should throw an error.
usePreviewData()
return null
}
import { usePreviewData } from 'next/dist/client/components/hooks-server'
export default function Page() {
const data = usePreviewData()
const hasData = !!data && data.key === 'value'
return (
<>
<h1>hello from /hooks/use-preview-data</h1>
{hasData ? (
<h2 id="has-preview-data">Has preview data</h2>
) : (
<h2 id="does-not-have-preview-data">Does not have preview data</h2>
)}
</>
)
}
import { useRouter } from 'next/dist/client/components/hooks-client'
export default function Page() {
const router = useRouter()
return (
<>
<h1 id="router">hello from /hooks/use-router</h1>
<button
id="button-push"
onClick={() => router.push('/hooks/use-router/sub-page')}
>
Router Push
</button>
</>
)
}
import { useRouter } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
useRouter()
return null
}
export default function Page() {
return <h1 id="router-sub-page">hello from /hooks/use-router/sub-page</h1>
}
import { useSearchParams } from 'next/dist/client/components/hooks-client'
export default function Page() {
const params = useSearchParams()
return (
<>
<h1
id="params"
data-param-first={params.first ?? 'N/A'}
data-param-second={params.second ?? 'N/A'}
data-param-third={params.third ?? 'N/A'}
data-param-not-real={params.notReal ?? 'N/A'}
>
hello from /hooks/use-search-params
</h1>
</>
)
}
import { useSearchParams } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
useSearchParams()
return null
}
import { useSelectedLayoutSegment } from 'next/dist/client/components/hooks-client'
export default function Page() {
// This should throw an error.
useSelectedLayoutSegment()
return null
}
import Link from 'next/link'
export default function Page() {
return (
<Link href="/with-id">
<a id="link">With ID</a>
</Link>
)
}
import { nanoid } from 'nanoid'
import Link from 'next/link'
export default function Page() {
return (
<>
<h1 id="render-id">{nanoid()}</h1>
<Link href="/link-hard-replace" replace>
<a id="self-link">Self Link</a>
</Link>
<Link href="/link-hard-replace/subpage" replace>
<a id="subpage-link">Subpage</a>
</Link>
</>
)
}
import Link from 'next/link'
export default function Page() {
return (
<Link href="/link-hard-replace" replace>
<a id="back-link">Self Link</a>
</Link>
)
}
import Link from 'next/link'
export default function Page() {
return (
<Link href="/with-id" soft>
<a id="link">With ID</a>
</Link>
)
}
import { nanoid } from 'nanoid'
import Link from 'next/link'
export default function Page() {
return (
<>
<h1 id="render-id">{nanoid()}</h1>
<Link href="/link-soft-replace" soft replace>
<a id="self-link">Self Link</a>
</Link>
<Link href="/link-soft-replace/subpage" soft replace>
<a id="subpage-link">Subpage</a>
</Link>
</>
)
}
import Link from 'next/link'
export default function Page() {
return (
<Link href="/link-soft-replace" soft replace>
<a id="back-link">Self Link</a>
</Link>
)
}
import { nanoid } from 'nanoid'
import Link from 'next/link'
export default function Page() {
return (
<>
<h1 id="render-id">{nanoid()}</h1>
<h2 id="from-navigation">hello from /navigation</h2>
<Link href="/hooks/use-cookies">
<a id="use-cookies">useCookies</a>
</Link>
<Link href="/hooks/use-headers">
<a id="use-headers">useHeaders</a>
</Link>
</>
)
}
export function getServerSideProps({ params }) {
return { props: { params } }
}
export default function Page({ params }) {
return (
<h1 id="text" data-params={params.slug?.join('/') ?? ''}>
hello from /optional-catch-all/{params.slug?.join('/')}
</h1>
)
}
import Link from 'next/link'
export default function Page(props) {
return (
<Link href="/app-linking">
<a id="app-link">To Pages Page</a>
</Link>
)
}
export default function Page({ params, query }) {
return (
<h1 id="params-and-query" data-params={params.slug} data-query={query.slug}>
hello from /param-and-query/{params.slug}?slug={query.slug}
</h1>
)
}
import Link from 'next/link'
export default function Page() {
return (
<Link href="/rewritten-to-dashboard">
<a id="link">To Dashboard Rewritten</a>
</Link>
)
}
import Link from 'next/link'
export default function Page() {
return (
<>
<h1 id="first-page">hello from same-layout/first</h1>
<Link href="/same-layout/second">
<a id="link">To Second</a>
</Link>
</>
)
}
import { nanoid } from 'nanoid'
export default function Layout({ children }) {
return (
<>
<h1 id="render-id">{nanoid()}</h1>
<div>{children}</div>
</>
)
}
import Link from 'next/link'
export default function Page() {
return (
<>
<h1 id="second-page">hello from same-layout/second</h1>
<Link href="/same-layout/first">
<a id="link">To First</a>
</Link>
</>
)
}
import Link from 'next/link'
import { nanoid } from 'nanoid'
export default function Page() {
return (
<>
<h1 id="render-id">{nanoid()}</h1>
<Link href="/navigation">
<a id="link">To Navigation</a>
</Link>
</>
)
}
export default function handler(req, res) {
res.setPreviewData({ key: 'value' })
res.send(200).end()
}
import Link from 'next/link'
export default function Page(props) {
return (
<Link href="/pages-linking">
<a id="pages-link">To App Page</a>
</Link>
)
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment