From 050936c040f588a87c93afbe07a89db762b7b396 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 30 Mar 2022 11:20:50 -0500 Subject: [PATCH] test(fix: Prevent page to scroll to top during data HMR) (#35744) * fix: Prevent page to scroll to top during data HMR * Add test for scroll position with gssp refreshing --- .../pages/another/index.js | 2 + .../test/index.test.ts | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/test/development/basic/gssp-ssr-change-reloading/pages/another/index.js b/test/development/basic/gssp-ssr-change-reloading/pages/another/index.js index 679405ddf80ada2..2e9e154507dccbd 100644 --- a/test/development/basic/gssp-ssr-change-reloading/pages/another/index.js +++ b/test/development/basic/gssp-ssr-change-reloading/pages/another/index.js @@ -9,6 +9,8 @@ export default function Gsp(props) { <>

change me

{JSON.stringify(props)}

+
+

bottom

) } diff --git a/test/development/basic/gssp-ssr-change-reloading/test/index.test.ts b/test/development/basic/gssp-ssr-change-reloading/test/index.test.ts index 828dc2adb2fe4e9..8d8cf6d6364319e 100644 --- a/test/development/basic/gssp-ssr-change-reloading/test/index.test.ts +++ b/test/development/basic/gssp-ssr-change-reloading/test/index.test.ts @@ -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) })