Open History to see the last commit
1import { expect } from '@playwright/test'
2β
3// These exercise browser lifecycle handlers, not actual BFCache navigation.
4export function scrollRestorationLifecycleTests(
5 test: typeof import('./e2e-utils/src/fixture').test,
6) {
7 test.beforeEach(async ({ page }) => {
8 await page.goto('/normal-page')
9 await expect
10 .poll(() => page.evaluate(() => history.scrollRestoration))
11 .toBe('manual')
12 await page.waitForLoadState('networkidle')
13 })
14β
15 for (const persisted of [false, true]) {
16 test(`pagehide hands control back with persisted=${persisted}`, async ({
17 page,
18 }) => {
19 const mode = await page.evaluate((persisted) => {
20 dispatchEvent(new PageTransitionEvent('pagehide', { persisted }))
21 return history.scrollRestoration
22 }, persisted)
23 expect(mode).toBe('auto')
24 })
25β
26 test(`pageshow reclaims control only with persisted=${persisted}`, async ({
27 page,
28 }) => {
29 const mode = await page.evaluate((persisted) => {
30 // Model the state left by pagehide before the document resumes.
31 history.scrollRestoration = 'auto'
32 dispatchEvent(new PageTransitionEvent('pageshow', { persisted }))
33 return history.scrollRestoration
34 }, persisted)
35 expect(mode).toBe(persisted ? 'manual' : 'auto')
36 })
37 }
38β
39 test('repeats the handoff when a cached document resumes more than once', async ({
40 page,
41 }) => {
42 const modes = await page.evaluate(() => {
43 const modes: Array<ScrollRestoration> = []
44 for (let i = 0; i < 3; i++) {
45 dispatchEvent(new PageTransitionEvent('pagehide', { persisted: true }))
46 modes.push(history.scrollRestoration)
47 dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true }))
48 modes.push(history.scrollRestoration)
49 }
50 return modes
51 })
52 expect(modes).toEqual([
53 'auto',
54 'manual',
55 'auto',
56 'manual',
57 'auto',
58 'manual',
59 ])
60 })
61}