From 2408322af4db96fc3b146233811f21c15a2ce581 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 30 Nov 2022 11:43:44 -0700 Subject: [PATCH] tests: added tests to cover usecases --- packages/next/client/router.ts | 2 +- .../app-dir/app/app/compat-hooks/app/page.js | 6 ++ test/e2e/app-dir/app/app/router/page.js | 10 +++ .../app/components/router-hooks-fixtures.js | 19 ++-- .../{adapter-hooks => compat-hooks}/[id].js | 0 .../[id]/account.js | 0 .../{adapter-hooks => compat-hooks}/pushed.js | 0 .../{adapter-hooks => compat-hooks}/static.js | 0 test/e2e/app-dir/index.test.ts | 90 +++++++++++-------- 9 files changed, 81 insertions(+), 46 deletions(-) create mode 100644 test/e2e/app-dir/app/app/compat-hooks/app/page.js create mode 100644 test/e2e/app-dir/app/app/router/page.js rename test/e2e/app-dir/app/pages/{adapter-hooks => compat-hooks}/[id].js (100%) rename test/e2e/app-dir/app/pages/{adapter-hooks => compat-hooks}/[id]/account.js (100%) rename test/e2e/app-dir/app/pages/{adapter-hooks => compat-hooks}/pushed.js (100%) rename test/e2e/app-dir/app/pages/{adapter-hooks => compat-hooks}/static.js (100%) diff --git a/packages/next/client/router.ts b/packages/next/client/router.ts index 7f4acbd51a60c..930535fb1de3b 100644 --- a/packages/next/client/router.ts +++ b/packages/next/client/router.ts @@ -133,7 +133,7 @@ export function useRouter(): NextRouter { const router = React.useContext(RouterContext) if (!router) { throw new Error( - 'Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted' + 'NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted' ) } diff --git a/test/e2e/app-dir/app/app/compat-hooks/app/page.js b/test/e2e/app-dir/app/app/compat-hooks/app/page.js new file mode 100644 index 0000000000000..a72c90c528f94 --- /dev/null +++ b/test/e2e/app-dir/app/app/compat-hooks/app/page.js @@ -0,0 +1,6 @@ +'use client' +import { RouterHooksFixtures } from '../../../components/router-hooks-fixtures' + +export default function Page() { + return +} diff --git a/test/e2e/app-dir/app/app/router/page.js b/test/e2e/app-dir/app/app/router/page.js new file mode 100644 index 0000000000000..e8a3dbcd7ecc9 --- /dev/null +++ b/test/e2e/app-dir/app/app/router/page.js @@ -0,0 +1,10 @@ +'use client' + +import { useRouter } from 'next/router' + +export default function RouterPage() { + // eslint-disable-next-line no-unused-vars + const router = useRouter() + + return
You shouldn't see this
+} diff --git a/test/e2e/app-dir/app/components/router-hooks-fixtures.js b/test/e2e/app-dir/app/components/router-hooks-fixtures.js index 6c5772188b663..90c224f5d11a4 100644 --- a/test/e2e/app-dir/app/components/router-hooks-fixtures.js +++ b/test/e2e/app-dir/app/components/router-hooks-fixtures.js @@ -1,10 +1,10 @@ -import { useRouter as usePagesRouter } from 'next/router' +import { useRouter as usePagesRouter } from 'next/compat/router' import { usePathname, useRouter as useAppRouter, useSearchParams, } from 'next/compat/navigation' -import { useState, useEffect } from 'react' +import { useState, useEffect, useMemo } from 'react' export const RouterHooksFixtures = () => { const pagesRouter = usePagesRouter() @@ -12,21 +12,26 @@ export const RouterHooksFixtures = () => { const searchParams = useSearchParams() const pathname = usePathname() + const isReady = useMemo( + () => !pagesRouter || pagesRouter.isReady, + [pagesRouter] + ) + const [value, setValue] = useState(null) useEffect(() => { - if (!pagesRouter.isReady) { + if (!isReady) { return } - setValue(searchParams.get('key')) - }, [pagesRouter.isReady, searchParams]) + setValue(searchParams?.get('key') ?? null) + }, [isReady, searchParams]) const onClick = () => { - appRouter.push('/adapter-hooks/pushed') + appRouter.push('/compat-hooks/pushed') } return ( -
+
{value}
{pathname}