|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | + |
| 3 | +test('smooth scrolls to index 1000', async ({ page }) => { |
| 4 | + await page.goto('/smooth-scroll/') |
| 5 | + await page.click('#scroll-to-1000') |
| 6 | + |
| 7 | + // Smooth scroll animation is 500ms + reconciliation time |
| 8 | + await page.waitForTimeout(2000) |
| 9 | + |
| 10 | + await expect(page.locator('[data-testid="item-1000"]')).toBeVisible() |
| 11 | + |
| 12 | + const delta = await page.evaluate(() => { |
| 13 | + const item = document.querySelector('[data-testid="item-1000"]') |
| 14 | + const container = document.querySelector('#scroll-container') |
| 15 | + if (!item || !container) throw new Error('Elements not found') |
| 16 | + |
| 17 | + const itemRect = item.getBoundingClientRect() |
| 18 | + const containerRect = container.getBoundingClientRect() |
| 19 | + const scrollTop = container.scrollTop |
| 20 | + const top = itemRect.top + scrollTop - containerRect.top |
| 21 | + const bottom = top + itemRect.height |
| 22 | + const containerBottom = scrollTop + container.clientHeight |
| 23 | + return Math.abs(bottom - containerBottom) |
| 24 | + }) |
| 25 | + expect(delta).toBeLessThan(1.01) |
| 26 | +}) |
| 27 | + |
| 28 | +test('smooth scrolls to index 100', async ({ page }) => { |
| 29 | + await page.goto('/smooth-scroll/') |
| 30 | + await page.click('#scroll-to-100') |
| 31 | + |
| 32 | + await page.waitForTimeout(2000) |
| 33 | + |
| 34 | + await expect(page.locator('[data-testid="item-100"]')).toBeVisible() |
| 35 | +}) |
| 36 | + |
| 37 | +test('smooth scrolls to index 0 after scrolling away', async ({ page }) => { |
| 38 | + await page.goto('/smooth-scroll/') |
| 39 | + |
| 40 | + // First scroll down |
| 41 | + await page.click('#scroll-to-500') |
| 42 | + await page.waitForTimeout(2000) |
| 43 | + await expect(page.locator('[data-testid="item-500"]')).toBeVisible() |
| 44 | + |
| 45 | + // Then smooth scroll back to top |
| 46 | + await page.click('#scroll-to-0') |
| 47 | + await page.waitForTimeout(2000) |
| 48 | + |
| 49 | + await expect(page.locator('[data-testid="item-0"]')).toBeVisible() |
| 50 | + |
| 51 | + const scrollTop = await page.evaluate(() => { |
| 52 | + const container = document.querySelector('#scroll-container') |
| 53 | + return container?.scrollTop ?? -1 |
| 54 | + }) |
| 55 | + expect(scrollTop).toBeLessThan(1.01) |
| 56 | +}) |
| 57 | + |
| 58 | +test('smooth scrolls to index 500 with start alignment', async ({ page }) => { |
| 59 | + await page.goto('/smooth-scroll/') |
| 60 | + await page.click('#scroll-to-500-start') |
| 61 | + |
| 62 | + await page.waitForTimeout(2000) |
| 63 | + |
| 64 | + await expect(page.locator('[data-testid="item-500"]')).toBeVisible() |
| 65 | + |
| 66 | + const delta = await page.evaluate( |
| 67 | + ([idx, align]) => { |
| 68 | + const item = document.querySelector(`[data-testid="item-${idx}"]`) |
| 69 | + const container = document.querySelector('#scroll-container') |
| 70 | + if (!item || !container) throw new Error('Elements not found') |
| 71 | + const itemRect = item.getBoundingClientRect() |
| 72 | + const containerRect = container.getBoundingClientRect() |
| 73 | + if (align === 'start') { |
| 74 | + return Math.abs(itemRect.top - containerRect.top) |
| 75 | + } |
| 76 | + return 0 |
| 77 | + }, |
| 78 | + [500, 'start'] as const, |
| 79 | + ) |
| 80 | + expect(delta).toBeLessThan(1.01) |
| 81 | +}) |
| 82 | + |
| 83 | +test('smooth scrolls to index 500 with center alignment', async ({ page }) => { |
| 84 | + await page.goto('/smooth-scroll/') |
| 85 | + await page.click('#scroll-to-500-center') |
| 86 | + |
| 87 | + await page.waitForTimeout(2000) |
| 88 | + |
| 89 | + await expect(page.locator('[data-testid="item-500"]')).toBeVisible() |
| 90 | + |
| 91 | + const delta = await page.evaluate( |
| 92 | + ([idx]) => { |
| 93 | + const item = document.querySelector(`[data-testid="item-${idx}"]`) |
| 94 | + const container = document.querySelector('#scroll-container') |
| 95 | + if (!item || !container) throw new Error('Elements not found') |
| 96 | + const itemRect = item.getBoundingClientRect() |
| 97 | + const containerRect = container.getBoundingClientRect() |
| 98 | + const containerCenter = containerRect.top + containerRect.height / 2 |
| 99 | + const itemCenter = itemRect.top + itemRect.height / 2 |
| 100 | + return Math.abs(itemCenter - containerCenter) |
| 101 | + }, |
| 102 | + [500] as const, |
| 103 | + ) |
| 104 | + // Center alignment has slightly more tolerance due to rounding |
| 105 | + expect(delta).toBeLessThan(50) |
| 106 | +}) |
| 107 | + |
| 108 | +test('smooth scrolls sequentially to multiple targets', async ({ page }) => { |
| 109 | + await page.goto('/smooth-scroll/') |
| 110 | + |
| 111 | + // Scroll to 100 first |
| 112 | + await page.click('#scroll-to-100') |
| 113 | + await page.waitForTimeout(2000) |
| 114 | + await expect(page.locator('[data-testid="item-100"]')).toBeVisible() |
| 115 | + |
| 116 | + // Then scroll to 500 |
| 117 | + await page.click('#scroll-to-500') |
| 118 | + await page.waitForTimeout(2000) |
| 119 | + await expect(page.locator('[data-testid="item-500"]')).toBeVisible() |
| 120 | + |
| 121 | + // Then scroll to 1000 |
| 122 | + await page.click('#scroll-to-1000') |
| 123 | + await page.waitForTimeout(2000) |
| 124 | + await expect(page.locator('[data-testid="item-1000"]')).toBeVisible() |
| 125 | +}) |
| 126 | + |
| 127 | +test('interrupting smooth scroll with another smooth scroll', async ({ |
| 128 | + page, |
| 129 | +}) => { |
| 130 | + await page.goto('/smooth-scroll/') |
| 131 | + |
| 132 | + // Start scrolling to 1000 |
| 133 | + await page.click('#scroll-to-1000') |
| 134 | + // Interrupt mid-animation (before the 500ms animation completes) |
| 135 | + await page.waitForTimeout(200) |
| 136 | + await page.click('#scroll-to-100') |
| 137 | + |
| 138 | + // Wait for the second scroll to complete |
| 139 | + await page.waitForTimeout(2000) |
| 140 | + |
| 141 | + // Should have ended at 100, not 1000 |
| 142 | + await expect(page.locator('[data-testid="item-100"]')).toBeVisible() |
| 143 | +}) |
0 commit comments