import { expect, test, type Page } from "../support/browser/strict-browser-test.ts"; const scrollY = (page: Page) => page.evaluate(() => Math.round(window.scrollY)); // Template sync (af3d8e6). Upstream scrolls its long example page; this product // deleted it, and a TechLog Case (about 6,000px at 1280x720) is the long page. const LONG_PAGE = "/cases/collection-fetch-join-pagination"; const LONG_PAGE_TITLE = "컬렉션 Fetch Join과 페이징은 왜 충돌하는가"; // 08-08. Back restores where the user was; a new route starts at the top. // The Case body fills in after the title, so the test waits for it before it // scrolls. test("restores the scroll position when navigating back", async ({ page }) => { const heading = page.getByRole("heading", { level: 1 }); await page.goto(LONG_PAGE); await expect(heading).toHaveText(LONG_PAGE_TITLE); await expect(page.locator("main [aria-busy='true']")).toHaveCount(0); await expect(page.locator("#remaining-cost")).toBeAttached(); await page.evaluate(() => window.scrollTo(0, 900)); await expect.poll(() => scrollY(page)).toBe(900); // TechLog's header is not sticky, so a real click first scrolls up to the // link and the reader leaves from the top. The click is dispatched where the // reader is, so the page is left at 900. await page .getByRole("navigation", { name: "주요 탐색" }) .getByRole("link", { name: "프로젝트" }) .dispatchEvent("click"); await expect(heading).toHaveText("프로젝트"); await expect.poll(() => scrollY(page)).toBe(0); await page.goBack(); await expect(heading).toHaveText(LONG_PAGE_TITLE); await expect.poll(() => scrollY(page)).toBe(900); }); // A section well below the first screen. The header is the TechLog site header. test("opens a hash link at its target below the sticky header", async ({ page, }) => { await page.goto(`${LONG_PAGE}#separate-loading`); const target = page.locator("#separate-loading"); await expect(target).toBeInViewport(); const top = await target.evaluate((element) => Math.round(element.getBoundingClientRect().top), ); const headerBottom = await page .locator(".site-header") .evaluate((element) => Math.round(element.getBoundingClientRect().bottom)); expect(top).toBeGreaterThanOrEqual(headerBottom); expect(await scrollY(page)).toBeGreaterThan(0); });