| 1 | import { test, expect } from './_fixtures'; |
| 2 | |
| 3 | async function openSearch(page: any) { |
| 4 | await page.evaluate(() => { |
| 5 | const dialog = document.querySelector('dialog#search-modal') as HTMLDialogElement; |
| 6 | dialog?.showModal(); |
| 7 | }); |
| 8 | await expect(page.locator('dialog#search-modal')).toBeVisible(); |
| 9 | } |
| 10 | |
| 11 | async function searchFor(page: any, query: string) { |
| 12 | await openSearch(page); |
| 13 | |
| 14 | // Wait for custom element upgrade, then load the index |
| 15 | await page.evaluate(() => customElements.whenDefined('search-index')); |
| 16 | await page.evaluate(() => |
| 17 | (document.querySelector('search-index') as any)?.load() |
| 18 | ); |
| 19 | |
| 20 | const input = page.locator('#search-input'); |
| 21 | await input.fill(query); |
| 22 | |
| 23 | await expect(page.locator('.result').first()).toBeAttached({ timeout: 5000 }); |
| 24 | } |
| 25 | |
| 26 | test.describe('Search interaction', () => { |
| 27 | test('results are <a> links with valid href', async ({ page }) => { |
| 28 | await page.goto('/', { waitUntil: 'networkidle' }); |
| 29 | await searchFor(page, 'hx-get'); |
| 30 | |
| 31 | const firstResult = page.locator('.result').first(); |
| 32 | const tagName = await firstResult.evaluate((el: Element) => el.tagName); |
| 33 | expect(tagName).toBe('A'); |
| 34 | |
| 35 | const href = await firstResult.getAttribute('href'); |
| 36 | expect(href).toBeTruthy(); |
| 37 | expect(href).toMatch(/^\//); // internal link |
| 38 | }); |
| 39 | |
| 40 | test('click result navigates to the page', async ({ page }) => { |
| 41 | await page.goto('/', { waitUntil: 'networkidle' }); |
| 42 | await searchFor(page, 'hx-get'); |
| 43 | |
| 44 | const firstResult = page.locator('.result').first(); |
| 45 | const href = await firstResult.getAttribute('href'); |
| 46 | |
| 47 | await firstResult.click(); |
| 48 | await expect(page).toHaveURL(href!); |
| 49 | }); |
| 50 | |
| 51 | test('Enter navigates to the selected result', async ({ page }) => { |
| 52 | await page.goto('/', { waitUntil: 'networkidle' }); |
| 53 | await searchFor(page, 'hx-get'); |
| 54 | |
| 55 | const firstResult = page.locator('.result').first(); |
| 56 | const href = await firstResult.getAttribute('href'); |
| 57 | |
| 58 | await page.keyboard.press('Enter'); |
| 59 | await expect(page).toHaveURL(href!); |
| 60 | }); |
| 61 | |
| 62 | test('arrow keys move selection', async ({ page }) => { |
| 63 | await page.goto('/', { waitUntil: 'networkidle' }); |
| 64 | await searchFor(page, 'hx-'); |
| 65 | |
| 66 | // First result should be selected initially |
| 67 | const first = page.locator('.result').first(); |
| 68 | await expect(first).toHaveAttribute('aria-selected', 'true'); |
| 69 | |
| 70 | // Press ArrowDown — selection moves to second |
| 71 | await page.keyboard.press('ArrowDown'); |
| 72 | const second = page.locator('.result').nth(1); |
| 73 | await expect(second).toHaveAttribute('aria-selected', 'true'); |
| 74 | await expect(first).not.toHaveAttribute('aria-selected'); |
| 75 | |
| 76 | // Press ArrowUp — selection moves back to first |
| 77 | await page.keyboard.press('ArrowUp'); |
| 78 | await expect(first).toHaveAttribute('aria-selected', 'true'); |
| 79 | await expect(second).not.toHaveAttribute('aria-selected'); |
| 80 | }); |
| 81 | |
| 82 | test('modal closes after clicking a result', async ({ page }) => { |
| 83 | await page.goto('/', { waitUntil: 'networkidle' }); |
| 84 | await searchFor(page, 'hx-get'); |
| 85 | |
| 86 | await page.locator('.result').first().click(); |
| 87 | |
| 88 | // Wait for navigation then check modal is gone |
| 89 | await page.waitForURL(/hx-get/); |
| 90 | await expect(page.locator('dialog#search-modal')).not.toBeVisible(); |
| 91 | }); |
| 92 | |
| 93 | test('modal closes after pressing Enter', async ({ page }) => { |
| 94 | await page.goto('/', { waitUntil: 'networkidle' }); |
| 95 | await searchFor(page, 'hx-get'); |
| 96 | |
| 97 | await page.keyboard.press('Enter'); |
| 98 | |
| 99 | await page.waitForURL(/hx-get/); |
| 100 | await expect(page.locator('dialog#search-modal')).not.toBeVisible(); |
| 101 | }); |
| 102 | |
| 103 | test('Escape closes modal and clears input', async ({ page }) => { |
| 104 | await page.goto('/', { waitUntil: 'networkidle' }); |
| 105 | await searchFor(page, 'hx-get'); |
| 106 | |
| 107 | await page.keyboard.press('Escape'); |
| 108 | await expect(page.locator('dialog#search-modal')).not.toBeVisible(); |
| 109 | |
| 110 | // Re-open — input should be cleared |
| 111 | await openSearch(page); |
| 112 | await expect(page.locator('#search-input')).toHaveValue(''); |
| 113 | }); |
| 114 | }); |