|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | + |
| 3 | +const BASE = './html/search-highlight/_site/index.html'; |
| 4 | + |
| 5 | +test('Search highlights persist after scrolling', async ({ page }) => { |
| 6 | + await page.goto(`${BASE}?q=special`); |
| 7 | + const marks = page.locator('mark'); |
| 8 | + |
| 9 | + await expect(marks.first()).toBeVisible({ timeout: 5000 }); |
| 10 | + const initialCount = await marks.count(); |
| 11 | + expect(initialCount).toBeGreaterThanOrEqual(2); |
| 12 | + |
| 13 | + // Scroll the page — marks should not be cleared |
| 14 | + await page.evaluate(() => window.scrollBy(0, 300)); |
| 15 | + await page.waitForTimeout(500); |
| 16 | + await expect(marks).toHaveCount(initialCount); |
| 17 | +}); |
| 18 | + |
| 19 | +test('Search highlights cleared when query changes', async ({ page }) => { |
| 20 | + await page.goto(`${BASE}?q=special`); |
| 21 | + const marks = page.locator('mark'); |
| 22 | + |
| 23 | + await expect(marks.first()).toBeVisible({ timeout: 5000 }); |
| 24 | + |
| 25 | + // Open the search overlay and type a different query |
| 26 | + await page.locator('#quarto-search').getByRole('button').click(); |
| 27 | + const input = page.getByRole('searchbox'); |
| 28 | + await expect(input).toBeVisible({ timeout: 2000 }); |
| 29 | + |
| 30 | + // Typing a different query triggers onStateChange which clears marks |
| 31 | + await input.fill('different'); |
| 32 | + await expect(page.locator('main mark')).toHaveCount(0, { timeout: 2000 }); |
| 33 | +}); |
| 34 | + |
| 35 | +test('No highlights without search query', async ({ page }) => { |
| 36 | + await page.goto(BASE); |
| 37 | + |
| 38 | + // Wait for page to fully load |
| 39 | + await expect(page.locator('main')).toBeVisible(); |
| 40 | + |
| 41 | + // No marks should exist without ?q= parameter |
| 42 | + await expect(page.locator('mark')).toHaveCount(0); |
| 43 | +}); |
0 commit comments