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