From 9cc156c3ae0d9879b23a4192517a98b39c727473 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 23 Aug 2022 20:29:29 +0100 Subject: [PATCH] Added tests for next/router in app directory (#39867) Added tests to ensure that the `next/router` returns null for `useRouter` and `withRouter` when used with the app directory. --- test/e2e/app-dir/app/app/old-router/IsNull.js | 7 +++++++ .../app/app/old-router/Router.client.js | 15 +++++++++++++++ test/e2e/app-dir/app/app/old-router/Router.js | 19 +++++++++++++++++++ .../app/app/old-router/Router.server.js | 15 +++++++++++++++ .../app-dir/app/app/old-router/page.server.js | 9 +++++++++ test/e2e/app-dir/index.test.ts | 18 ++++++++++++++++++ 6 files changed, 83 insertions(+) create mode 100644 test/e2e/app-dir/app/app/old-router/IsNull.js create mode 100644 test/e2e/app-dir/app/app/old-router/Router.client.js create mode 100644 test/e2e/app-dir/app/app/old-router/Router.js create mode 100644 test/e2e/app-dir/app/app/old-router/Router.server.js create mode 100644 test/e2e/app-dir/app/app/old-router/page.server.js diff --git a/test/e2e/app-dir/app/app/old-router/IsNull.js b/test/e2e/app-dir/app/app/old-router/IsNull.js new file mode 100644 index 000000000000000..3876fbe873654bb --- /dev/null +++ b/test/e2e/app-dir/app/app/old-router/IsNull.js @@ -0,0 +1,7 @@ +export default function IsNull({ value }) { + if (value === null) { + return
Value Was Null
+ } + + return
Value Was Not Null
+} diff --git a/test/e2e/app-dir/app/app/old-router/Router.client.js b/test/e2e/app-dir/app/app/old-router/Router.client.js new file mode 100644 index 000000000000000..aaec667cb52d575 --- /dev/null +++ b/test/e2e/app-dir/app/app/old-router/Router.client.js @@ -0,0 +1,15 @@ +import { useRouter, withRouter } from 'next/router' +import IsNull from './IsNull' + +function ClientRouter({ router: withRouter }) { + const router = useRouter() + + return ( + <> + + + + ) +} + +export default withRouter(ClientRouter) diff --git a/test/e2e/app-dir/app/app/old-router/Router.js b/test/e2e/app-dir/app/app/old-router/Router.js new file mode 100644 index 000000000000000..930b9ddac284c44 --- /dev/null +++ b/test/e2e/app-dir/app/app/old-router/Router.js @@ -0,0 +1,19 @@ +import { useRouter, withRouter } from 'next/router' +import IsNull from './IsNull' +import ServerRouter from './Router.server' +import ClientRouter from './Router.client' + +function SharedRouter({ router: withRouter }) { + const router = useRouter() + + return ( + <> + + + + + + ) +} + +export default withRouter(SharedRouter) diff --git a/test/e2e/app-dir/app/app/old-router/Router.server.js b/test/e2e/app-dir/app/app/old-router/Router.server.js new file mode 100644 index 000000000000000..ca08c6668f252ba --- /dev/null +++ b/test/e2e/app-dir/app/app/old-router/Router.server.js @@ -0,0 +1,15 @@ +import { useRouter, withRouter } from 'next/router' +import IsNull from './IsNull' + +function ServerRouter({ router: withRouter }) { + const router = useRouter() + + return ( + <> + + + + ) +} + +export default withRouter(ServerRouter) diff --git a/test/e2e/app-dir/app/app/old-router/page.server.js b/test/e2e/app-dir/app/app/old-router/page.server.js new file mode 100644 index 000000000000000..d74b7650d38c6bd --- /dev/null +++ b/test/e2e/app-dir/app/app/old-router/page.server.js @@ -0,0 +1,9 @@ +import Router from './Router' + +export default function Page() { + return ( +
+ +
+ ) +} diff --git a/test/e2e/app-dir/index.test.ts b/test/e2e/app-dir/index.test.ts index e9984ce8fa0ff74..920a8800bd84bda 100644 --- a/test/e2e/app-dir/index.test.ts +++ b/test/e2e/app-dir/index.test.ts @@ -720,6 +720,24 @@ describe('app dir', () => { }) }) + describe('next/router', () => { + it('should always return null when accessed from /app', async () => { + const browser = await webdriver(next.url, '/old-router') + + try { + await browser.waitForElementByCss('#old-router') + + const notNull = await browser.elementsByCss('.was-not-null') + expect(notNull.length).toBe(0) + + const wasNull = await browser.elementsByCss('.was-null') + expect(wasNull.length).toBe(6) + } finally { + await browser.close() + } + }) + }) + describe('hooks', () => { describe('useCookies', () => { it('should retrive cookies in a server component', async () => {