Skip to content

Commit

Permalink
Add tests for router.push(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
keyz committed May 12, 2023
1 parent 8478c12 commit 8c45a52
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'

import { useRouter } from 'next/navigation'

export function RouterPushButton() {
const router = useRouter()

return (
<h3 id="h3">
<button
id="button-to-h3-hash-only"
onClick={() => {
router.push('#h3')
}}
>
To #h3, hash only
</button>
</h3>
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Link from 'next/link'
import { RouterPushButton } from './client-component'

export default function Page() {
return (
Expand All @@ -20,6 +21,8 @@ export default function Page() {
To #h2, with both relative hash and query
</Link>
</h2>

<RouterPushButton />
</>
)
}
14 changes: 14 additions & 0 deletions test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ createNextDescribe(
await check(() => browser.url(), next.url + pathname + '#h1')
})

it('should work with a hash-only `router.push(...)`', async () => {
const browser = await next.browser(pathname)
await browser.elementByCss('#button-to-h3-hash-only').click()

await check(() => browser.url(), next.url + pathname + '#h3')
})

it('should work with a query-only href', async () => {
const browser = await next.browser(pathname)
await browser.elementByCss('#link-to-dummy-query').click()
Expand Down Expand Up @@ -96,6 +103,13 @@ createNextDescribe(
() => browser.url(),
next.url + pathname + '?foo=1&bar=2#h1'
)

// Update hash again via `router.push(...)`
await browser.elementByCss('#button-to-h3-hash-only').click()
await check(
() => browser.url(),
next.url + pathname + '?foo=1&bar=2#h3'
)
})
})

Expand Down

0 comments on commit 8c45a52

Please sign in to comment.