Skip to content

Commit

Permalink
Added tests for next/router in app directory (#39867)
Browse files Browse the repository at this point in the history
<!--
Thanks for opening a PR! Your contribution is much appreciated.
In order to make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

Added tests to ensure that the `next/router` returns null for `useRouter` and `withRouter` when used with the app directory.
  • Loading branch information
wyattjoh committed Aug 23, 2022
1 parent 64a3697 commit 9cc156c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/e2e/app-dir/app/app/old-router/IsNull.js
@@ -0,0 +1,7 @@
export default function IsNull({ value }) {
if (value === null) {
return <div className="was-null">Value Was Null</div>
}

return <div className="was-not-null">Value Was Not Null</div>
}
15 changes: 15 additions & 0 deletions 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 (
<>
<IsNull value={withRouter} />
<IsNull value={router} />
</>
)
}

export default withRouter(ClientRouter)
19 changes: 19 additions & 0 deletions 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 (
<>
<IsNull value={withRouter} />
<IsNull value={router} />
<ServerRouter />
<ClientRouter />
</>
)
}

export default withRouter(SharedRouter)
15 changes: 15 additions & 0 deletions 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 (
<>
<IsNull value={withRouter} />
<IsNull value={router} />
</>
)
}

export default withRouter(ServerRouter)
9 changes: 9 additions & 0 deletions test/e2e/app-dir/app/app/old-router/page.server.js
@@ -0,0 +1,9 @@
import Router from './Router'

export default function Page() {
return (
<div id="old-router">
<Router />
</div>
)
}
18 changes: 18 additions & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 9cc156c

Please sign in to comment.