|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | + |
| 3 | +const dashboard = '/dashboard/hash-navigation.html'; |
| 4 | + |
| 5 | +const heatPane = (page) => page.locator('#heat.dashboard-page'); |
| 6 | +const coolPane = (page) => page.locator('#cool.dashboard-page'); |
| 7 | + |
| 8 | +// https://github.com/quarto-dev/quarto-cli/issues/14818 |
| 9 | +test.describe('a hash that names no page leaves the pages alone', () => { |
| 10 | + test('on load', async ({ page }) => { |
| 11 | + await page.goto(`${dashboard}#no-such-thing`); |
| 12 | + await expect(heatPane(page)).toHaveClass(/active/); |
| 13 | + await expect(coolPane(page)).not.toHaveClass(/active/); |
| 14 | + await expect(page.locator('html')).not.toHaveClass(/hidden/); |
| 15 | + }); |
| 16 | + |
| 17 | + test('on a hash change', async ({ page }) => { |
| 18 | + await page.goto(dashboard); |
| 19 | + await expect(heatPane(page)).toHaveClass(/active/); |
| 20 | + // an in-page anchor that exists but is not a page, such as a footnote |
| 21 | + await page.evaluate(() => { window.location.hash = '#fn1'; }); |
| 22 | + await expect(heatPane(page)).toHaveClass(/active/); |
| 23 | + }); |
| 24 | + |
| 25 | + // isPage used to build a selector from the hash, which throws for a hash |
| 26 | + // that is not a valid CSS selector and left the whole dashboard hidden |
| 27 | + test('when the hash is not a valid CSS selector', async ({ page }) => { |
| 28 | + await page.goto(`${dashboard}#1foo`); |
| 29 | + await expect(page.locator('html')).not.toHaveClass(/hidden/); |
| 30 | + await expect(heatPane(page)).toHaveClass(/active/); |
| 31 | + }); |
| 32 | + |
| 33 | + // decodeURIComponent throws for malformed percent-encoding, which aborted |
| 34 | + // the DOMContentLoaded handler before the hidden class was removed |
| 35 | + test('when the hash is malformed percent-encoding', async ({ page }) => { |
| 36 | + await page.goto(`${dashboard}#%`); |
| 37 | + await expect(page.locator('html')).not.toHaveClass(/hidden/); |
| 38 | + await expect(heatPane(page)).toHaveClass(/active/); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +test('page navigation and history still work', async ({ page }) => { |
| 43 | + await page.goto(dashboard); |
| 44 | + await expect(heatPane(page)).toHaveClass(/active/); |
| 45 | + |
| 46 | + await page.locator('.navbar .nav-link[data-bs-target="#cool"]').click(); |
| 47 | + await expect(coolPane(page)).toHaveClass(/active/); |
| 48 | + await expect(heatPane(page)).not.toHaveClass(/active/); |
| 49 | + |
| 50 | + await page.goBack(); |
| 51 | + await expect(heatPane(page)).toHaveClass(/active/); |
| 52 | + |
| 53 | + await page.goForward(); |
| 54 | + await expect(coolPane(page)).toHaveClass(/active/); |
| 55 | +}); |
| 56 | + |
| 57 | +// https://github.com/quarto-dev/quarto-cli/issues/14819 |
| 58 | +test('landing on a page hash starts tabbing at the top of the document', async ({ page, browserName }) => { |
| 59 | + // WebKit only tabs to links when the Alt modifier is held, which matches |
| 60 | + // Safari's default "Press Tab to highlight each item on a webpage" setting |
| 61 | + const tabKey = browserName === 'webkit' ? 'Alt+Tab' : 'Tab'; |
| 62 | + |
| 63 | + await page.goto(`${dashboard}#cool`); |
| 64 | + await expect(coolPane(page)).toHaveClass(/active/); |
| 65 | + |
| 66 | + // the browser would otherwise start sequential focus inside the target |
| 67 | + // pane, which puts the navbar out of reach of a forward Tab |
| 68 | + await page.keyboard.press(tabKey); |
| 69 | + const firstStop = await page.evaluate(() => ({ |
| 70 | + isBody: document.activeElement === document.body, |
| 71 | + insideAPage: document.activeElement?.closest('.dashboard-page') !== null, |
| 72 | + })); |
| 73 | + expect(firstStop.isBody).toBe(false); |
| 74 | + expect(firstStop.insideAPage).toBe(false); |
| 75 | + |
| 76 | + // the navbar tab for the current page is reachable by tabbing forward. |
| 77 | + // the exact number of stops before it depends on what else the format |
| 78 | + // puts at the top of the body, so tab forward a bounded number of times |
| 79 | + // rather than assuming a fixed position. |
| 80 | + let reachedNavbarTab = false; |
| 81 | + for (let i = 0; i < 5 && !reachedNavbarTab; i++) { |
| 82 | + reachedNavbarTab = await page.evaluate( |
| 83 | + () => document.activeElement?.id === 'tab-cool' |
| 84 | + ); |
| 85 | + if (!reachedNavbarTab) { |
| 86 | + await page.keyboard.press(tabKey); |
| 87 | + } |
| 88 | + } |
| 89 | + expect(reachedNavbarTab).toBe(true); |
| 90 | +}); |
0 commit comments