|
| 1 | +import { test, expect } from "@playwright/test" |
| 2 | +import { nextEventNamed } from "../helpers/page" |
| 3 | + |
| 4 | +test("it resets the scroll position when the turbo-visit-scroll meta tag is 'reset'", async ({ page }) => { |
| 5 | + await page.goto("/src/tests/fixtures/scroll_restoration.html") |
| 6 | + |
| 7 | + const reset = page.locator("#reset") |
| 8 | + await reset.scrollIntoViewIfNeeded() |
| 9 | + |
| 10 | + await clickWithoutScrolling(reset) |
| 11 | + await nextEventNamed(page, "turbo:render") |
| 12 | + |
| 13 | + expect.poll(async () => await scrollPosition(page)).toEqual([ 0, 0 ]) |
| 14 | +}) |
| 15 | + |
| 16 | +test("it preserves the scroll position during 'restore' visits when the turbo-visit-scroll meta tag is 'reset'", async ({ page }) => { |
| 17 | + await page.goto("/src/tests/fixtures/scroll_restoration/reset.html") |
| 18 | + |
| 19 | + const link = page.locator("#default") |
| 20 | + await link.scrollIntoViewIfNeeded() |
| 21 | + await clickWithoutScrolling(link) |
| 22 | + |
| 23 | + await nextEventNamed(page, "turbo:render") |
| 24 | + await page.goBack() |
| 25 | + await nextEventNamed(page, "turbo:render") |
| 26 | + |
| 27 | + expect.poll(async () => await scrollPosition(page)).not.toEqual([ 0, 0 ]) |
| 28 | +}) |
| 29 | + |
| 30 | +test("it preserves the scroll position when the turbo-visit-scroll meta tag is 'preserve'", async ({ page }) => { |
| 31 | + await page.goto("/src/tests/fixtures/scroll_restoration.html") |
| 32 | + |
| 33 | + const preserve = page.locator("#preserve") |
| 34 | + await preserve.scrollIntoViewIfNeeded() |
| 35 | + |
| 36 | + const [ scrollTop, scrollLeft ] = await page.evaluate(() => [window.scrollY, window.scrollX]) |
| 37 | + |
| 38 | + await clickWithoutScrolling(preserve) |
| 39 | + await nextEventNamed(page, "turbo:render") |
| 40 | + |
| 41 | + expect.poll(async () => await scrollPosition(page)).toEqual([ scrollTop, scrollLeft ]) |
| 42 | +}) |
| 43 | + |
| 44 | +test("it does not preserve the scroll position during 'restore' visits when coming from a pack with turbo-visit-scroll meta tag of 'reset'", async ({ page }) => { |
| 45 | + await page.goto("/src/tests/fixtures/scroll_restoration/preserve.html") |
| 46 | + |
| 47 | + const reset = page.locator("#reset") |
| 48 | + await reset.scrollIntoViewIfNeeded() |
| 49 | + await clickWithoutScrolling(reset) |
| 50 | + |
| 51 | + await nextEventNamed(page, "turbo:render") |
| 52 | + await page.goBack() |
| 53 | + await nextEventNamed(page, "turbo:render") |
| 54 | + |
| 55 | + expect.poll(async () => await scrollPosition(page)).not.toEqual([ 0, 0 ]) |
| 56 | +}) |
| 57 | + |
| 58 | +function clickWithoutScrolling(locator) { |
| 59 | + // not using locator.click() because it can reset the scroll position |
| 60 | + return locator.evaluate((element) => element.click()) |
| 61 | +} |
| 62 | + |
| 63 | +function scrollPosition(page) { |
| 64 | + return page.evaluate(() => [ |
| 65 | + document.documentElement.scrollTop || document.body.scrollTop, |
| 66 | + document.documentElement.scrollLeft || document.body.scrollLeft |
| 67 | + ]) |
| 68 | +} |
0 commit comments