Skip to content

Commit

Permalink
test(fix: Prevent page to scroll to top during data HMR) (#35744)
Browse files Browse the repository at this point in the history
* fix: Prevent page to scroll to top during data HMR

* Add test for scroll position with gssp refreshing
  • Loading branch information
ijjk committed Mar 30, 2022
1 parent 600e0b3 commit 050936c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Expand Up @@ -9,6 +9,8 @@ export default function Gsp(props) {
<>
<p id="change">change me</p>
<p id="props">{JSON.stringify(props)}</p>
<div style={{ height: 1000, width: 50, background: 'cyan' }}></div>
<p id="scroll-target">bottom</p>
</>
)
}
Expand Down
Expand Up @@ -163,7 +163,44 @@ describe('GS(S)P Server-Side Change Reloading', () => {
originalContent.replace('count = 1', 'count = 2')
)

await check(
async () =>
JSON.parse(await browser.elementByCss('#props').text()).count + '',
'2'
)
expect(await browser.eval('window.beforeChange')).toBe('hi')
await next.patchFile(page, originalContent)
})

it('should keep scroll position when updating from change in getStaticProps', async () => {
const browser = await webdriver(next.url, '/another')
await browser.eval(
'document.getElementById("scroll-target").scrollIntoView()'
)
const scrollPosition = await browser.eval(
'document.documentElement.scrollTop'
)
await browser.eval(`window.beforeChange = 'hi'`)

const props = JSON.parse(await browser.elementByCss('#props').text())
expect(props.count).toBe(1)

const page = 'pages/another/index.js'
const originalContent = await next.readFile(page)
await next.patchFile(
page,
originalContent.replace('count = 1', 'count = 2')
)

await check(
async () =>
JSON.parse(await browser.elementByCss('#props').text()).count + '',
'2'
)
expect(await browser.eval('window.beforeChange')).toBe('hi')
expect(await browser.eval('document.documentElement.scrollTop')).toBe(
scrollPosition
)
await next.patchFile(page, originalContent)
})

Expand Down

0 comments on commit 050936c

Please sign in to comment.