Skip to content

Commit

Permalink
Merge branch 'canary' into chore/decrease-stale-no-reproduction
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Jan 3, 2022
2 parents a8e6c19 + be1d308 commit 79f31de
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ export default class Router implements BaseRouter {
}

scrollToHash(as: string): void {
const [, hash] = as.split('#')
const [, hash = ''] = as.split('#')
// Scroll to top if the hash is just `#` with no value or `#top`
// To mirror browsers
if (hash === '' || hash === 'top') {
Expand Down
16 changes: 16 additions & 0 deletions test/integration/router-hash-navigation/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Link from 'next/link'

export default function Page() {
return (
<div>
<div style={{ height: '100vh' }} />
<Link href="/">
<a id="top-link">top</a>
</Link>
<Link href="#section">
<a id="section-link">section link</a>
</Link>
<section id="section">section content</section>
</div>
)
}
51 changes: 51 additions & 0 deletions test/integration/router-hash-navigation/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-env jest */

import { join } from 'path'
import webdriver from 'next-webdriver'
import {
findPort,
launchApp,
killApp,
nextStart,
nextBuild,
} from 'next-test-utils'

let app
let appPort
const appDir = join(__dirname, '../')

function runTests() {
it('scrolls to top when href="/" and url already contains a hash', async () => {
const browser = await webdriver(appPort, '/#section')
expect(await browser.eval(() => window.scrollY)).not.toBe(0)
await browser.elementByCss('#top-link').click()
expect(await browser.eval(() => window.scrollY)).toBe(0)
await browser.close()
})
}

describe('router.isReady', () => {
describe('dev mode', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})

runTests()
})

describe('production mode', () => {
beforeAll(async () => {
await nextBuild(appDir)

appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})
})

0 comments on commit 79f31de

Please sign in to comment.