| 1 |
import { test, expect } from '@playwright/test'; |
| 2 |
|
| 3 |
import { expectScrollPosition, expectToBeAt } from '../support/commands.js'; |
| 4 |
import { waitForSwup } from '../support/swup.js'; |
| 5 |
|
| 6 |
test.describe('scrolling', () => { |
| 7 |
test.beforeEach(async ({ page }) => { |
| 8 |
await page.goto('/scrolling-1.html'); |
| 9 |
await waitForSwup(page); |
| 10 |
}); |
| 11 |
|
| 12 |
test('scrolls to anchor and back to top', async ({ page }) => { |
| 13 |
await page.getByTestId('link-to-anchor').click(); |
| 14 |
await expect(page.getByTestId('anchor')).toBeInViewport(); |
| 15 |
await page.getByTestId('link-to-page').click(); |
| 16 |
await expectScrollPosition(page, 0); |
| 17 |
}); |
| 18 |
|
| 19 |
test('scrolls to anchor with path', async ({ page }) => { |
| 20 |
await page.getByTestId('link-to-self-anchor').click(); |
| 21 |
await expect(page.getByTestId('anchor')).toBeInViewport(); |
| 22 |
}); |
| 23 |
|
| 24 |
test('scrolls to top', async ({ page }) => { |
| 25 |
await page.getByTestId('link-to-self-anchor').click(); |
| 26 |
await expect(page.getByTestId('anchor')).toBeInViewport(); |
| 27 |
await page.getByTestId('link-to-top').click(); |
| 28 |
await expectScrollPosition(page, 0); |
| 29 |
}); |
| 30 |
|
| 31 |
test('scrolls to id-based anchor', async ({ page }) => { |
| 32 |
await page.getByTestId('link-to-anchor-by-id').click(); |
| 33 |
await expect(page.getByTestId('anchor-by-id')).toBeInViewport(); |
| 34 |
}); |
| 35 |
|
| 36 |
test('scrolls to name-based anchor', async ({ page }) => { |
| 37 |
await page.getByTestId('link-to-anchor-by-name').click(); |
| 38 |
await expect(page.getByTestId('anchor-by-name')).toBeInViewport(); |
| 39 |
}); |
| 40 |
|
| 41 |
test('prefers undecoded id attributes', async ({ page }) => { |
| 42 |
await page.getByTestId('link-to-anchor-encoded').click(); |
| 43 |
await expect(page.getByTestId('anchor-encoded')).toBeInViewport(); |
| 44 |
}); |
| 45 |
|
| 46 |
test('accepts unencoded anchor links', async ({ page }) => { |
| 47 |
await page.getByTestId('link-to-anchor-unencoded').click(); |
| 48 |
await expect(page.getByTestId('anchor-unencoded')).toBeInViewport(); |
| 49 |
}); |
| 50 |
|
| 51 |
test('scrolls to anchor with special characters', async ({ page }) => { |
| 52 |
await page.getByTestId('link-to-anchor-with-colon').click(); |
| 53 |
await expect(page.getByTestId('anchor-with-colon')).toBeInViewport(); |
| 54 |
await page.getByTestId('link-to-anchor-with-unicode').click(); |
| 55 |
await expect(page.getByTestId('anchor-with-unicode')).toBeInViewport(); |
| 56 |
}); |
| 57 |
|
| 58 |
test('scrolls to requested hash after navigation', async ({ page }) => { |
| 59 |
await page.getByTestId('link-to-page-anchor').click(); |
| 60 |
await expectToBeAt(page, '/scrolling-2.html#anchor', 'Scrolling 2'); |
| 61 |
await expect(page.getByTestId('anchor')).toBeInViewport(); |
| 62 |
}); |
| 63 |
|
| 64 |
test('appends the hash if changing visit.to.hash on the fly', async ({ page }) => { |
| 65 |
await page.evaluate(() => { |
| 66 |
window._swup.hooks.once('visit:start', (visit) => (visit.to.hash = '#anchor')); |
| 67 |
}); |
| 68 |
await page.getByTestId('link-to-page').click(); |
| 69 |
await expectToBeAt(page, '/scrolling-2.html#anchor', 'Scrolling 2'); |
| 70 |
await expect(page.getByTestId('anchor')).toBeInViewport(); |
| 71 |
}); |
| 72 |
|
| 73 |
test('does not append the hash if changing visit.scroll.target on the fly', async ({ |
| 74 |
page |
| 75 |
}) => { |
| 76 |
await page.evaluate(() => { |
| 77 |
window._swup.hooks.once('visit:start', (visit) => (visit.scroll.target = '#anchor')); |
| 78 |
}); |
| 79 |
await page.getByTestId('link-to-page').click(); |
| 80 |
await expectToBeAt(page, '/scrolling-2.html', 'Scrolling 2'); |
| 81 |
await expect(page.getByTestId('anchor')).toBeInViewport(); |
| 82 |
}); |
| 83 |
}); |