|
| 1 | +import { test, expect, Page } from "@playwright/test"; |
| 2 | + |
| 3 | +const BASE = './html/search-tabsets/_site/index.html'; |
| 4 | + |
| 5 | +// Helper: count marks visible (not inside an inactive tab pane) |
| 6 | +async function visibleMarkCount(page: Page): Promise<number> { |
| 7 | + return page.evaluate(() => { |
| 8 | + return Array.from(document.querySelectorAll('mark')).filter(m => { |
| 9 | + let el: Element | null = m; |
| 10 | + while (el) { |
| 11 | + if (el.classList?.contains('tab-pane') && !el.classList.contains('active')) { |
| 12 | + return false; |
| 13 | + } |
| 14 | + el = el.parentElement; |
| 15 | + } |
| 16 | + return true; |
| 17 | + }).length; |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +test('Search activates inactive tab containing match', async ({ page }) => { |
| 22 | + await page.goto(`${BASE}?q=beta-unique-search-term`); |
| 23 | + |
| 24 | + // Mark should be visible (tab activation deferred to pageshow) |
| 25 | + const marks = page.locator('mark'); |
| 26 | + await expect(marks.first()).toBeVisible({ timeout: 5000 }); |
| 27 | + |
| 28 | + // Tab Beta should be active in the ungrouped tabset |
| 29 | + await expect(page.getByRole('tab', { name: 'Tab Beta', exact: true })).toHaveClass(/active/); |
| 30 | + |
| 31 | + await expect(marks).toHaveCount(1); |
| 32 | + expect(await visibleMarkCount(page)).toBe(1); |
| 33 | +}); |
| 34 | + |
| 35 | +test('Search keeps active tab when it already has a match', async ({ page }) => { |
| 36 | + await page.goto(`${BASE}?q=gamma-both-tabs`); |
| 37 | + |
| 38 | + const marks = page.locator('mark'); |
| 39 | + await expect(marks.first()).toBeVisible({ timeout: 5000 }); |
| 40 | + |
| 41 | + // R tab should stay active — it already has a match |
| 42 | + const section = page.locator('#both-tabs-match'); |
| 43 | + await expect(section.getByRole('tab', { name: 'R', exact: true })).toHaveClass(/active/); |
| 44 | + |
| 45 | + // 2 marks total (one in each tab), only 1 visible (in active tab) |
| 46 | + await expect(marks).toHaveCount(2); |
| 47 | + expect(await visibleMarkCount(page)).toBe(1); |
| 48 | +}); |
| 49 | + |
| 50 | +test('Search highlights outside tabs without changing tab state', async ({ page }) => { |
| 51 | + await page.goto(`${BASE}?q=epsilon-no-tabs`); |
| 52 | + |
| 53 | + const marks = page.locator('mark'); |
| 54 | + await expect(marks.first()).toBeVisible({ timeout: 5000 }); |
| 55 | + |
| 56 | + // All tabs should remain at their defaults (first tab active) |
| 57 | + await expect(page.getByRole('tab', { name: 'Tab Alpha', exact: true })).toHaveClass(/active/); |
| 58 | + const bothSection = page.locator('#both-tabs-match'); |
| 59 | + await expect(bothSection.getByRole('tab', { name: 'R', exact: true })).toHaveClass(/active/); |
| 60 | + |
| 61 | + await expect(marks).toHaveCount(1); |
| 62 | + expect(await visibleMarkCount(page)).toBe(1); |
| 63 | +}); |
| 64 | + |
| 65 | +test('Search activates both outer and inner tabs for nested match', async ({ page }) => { |
| 66 | + await page.goto(`${BASE}?q=nested-inner-only-term`); |
| 67 | + |
| 68 | + const marks = page.locator('mark'); |
| 69 | + await expect(marks.first()).toBeVisible({ timeout: 5000 }); |
| 70 | + |
| 71 | + // Both outer and inner tabs should activate for the nested match |
| 72 | + await expect(page.getByRole('tab', { name: 'Outer Tab B', exact: true })).toHaveClass(/active/); |
| 73 | + await expect(page.getByRole('tab', { name: 'Inner Tab Y', exact: true })).toHaveClass(/active/); |
| 74 | + |
| 75 | + await expect(marks).toHaveCount(1); |
| 76 | + expect(await visibleMarkCount(page)).toBe(1); |
| 77 | +}); |
| 78 | + |
| 79 | +test('Search activation overrides localStorage tab preference', async ({ page }) => { |
| 80 | + // Pre-set localStorage to prefer "R" for the "language" group |
| 81 | + await page.goto(`${BASE}`); |
| 82 | + await page.evaluate(() => { |
| 83 | + localStorage.setItem( |
| 84 | + 'quarto-persistent-tabsets-data', |
| 85 | + JSON.stringify({ language: 'R' }) |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + // Navigate with search query that matches only in the Python tab |
| 90 | + await page.goto(`${BASE}?q=python-only-content`); |
| 91 | + |
| 92 | + const marks = page.locator('mark'); |
| 93 | + await expect(marks.first()).toBeVisible({ timeout: 5000 }); |
| 94 | + |
| 95 | + // Python tab should be active despite localStorage saying "R" |
| 96 | + const groupedSection = page.locator('#grouped-tabset'); |
| 97 | + await expect(groupedSection.getByRole('tab', { name: 'Python', exact: true })).toHaveClass(/active/); |
| 98 | + |
| 99 | + // Second grouped tabset should remain on R (no search match there) |
| 100 | + const secondGrouped = page.locator('#second-grouped-tabset'); |
| 101 | + await expect(secondGrouped.getByRole('tab', { name: 'R', exact: true })).toHaveClass(/active/); |
| 102 | + |
| 103 | + await expect(marks).toHaveCount(1); |
| 104 | + expect(await visibleMarkCount(page)).toBe(1); |
| 105 | +}); |
| 106 | + |
| 107 | +test('Search scrolls to first visible match', async ({ page }) => { |
| 108 | + // Use small viewport so the nested tabset at the bottom is below the fold, |
| 109 | + // ensuring the test actually exercises scrollIntoView (not trivially passing). |
| 110 | + await page.setViewportSize({ width: 800, height: 400 }); |
| 111 | + await page.goto(`${BASE}?q=nested-inner-only-term`); |
| 112 | + |
| 113 | + const mark = page.locator('mark').first(); |
| 114 | + await expect(mark).toBeVisible({ timeout: 5000 }); |
| 115 | + await expect(mark).toBeInViewport(); |
| 116 | +}); |
0 commit comments