| 1 | import { test, expect } from './_fixtures'; |
| 2 | |
| 3 | // Wait for SW to claim the page |
| 4 | async function waitForSw(page: any) { |
| 5 | await page.evaluate(async () => { |
| 6 | if (!('serviceWorker' in navigator)) return; |
| 7 | await navigator.serviceWorker.ready; |
| 8 | const deadline = Date.now() + 10_000; |
| 9 | while (!navigator.serviceWorker.controller && Date.now() < deadline) |
| 10 | await new Promise(r => setTimeout(r, 50)); |
| 11 | }); |
| 12 | } |
| 13 | |
| 14 | // Wait for demo content to appear (supports both #demo-content and .demo-container) |
| 15 | async function waitForDemo(page: any) { |
| 16 | await expect(page.locator('#demo-content > *, .demo-container > *').first()).toBeVisible({ timeout: 15_000 }); |
| 17 | } |
| 18 | |
| 19 | // Inject a visible boosted link and click it to trigger morph navigation |
| 20 | async function morphViaLink(page: any, url: string) { |
| 21 | const id = 'test-morph-link-' + Math.random().toString(36).slice(2, 8); |
| 22 | await page.evaluate(({ url, id }) => { |
| 23 | const a = document.createElement('a'); |
| 24 | a.href = url; |
| 25 | a.textContent = 'Test nav'; |
| 26 | a.id = id; |
| 27 | a.style.cssText = 'position:fixed;top:0;left:0;z-index:9999;padding:10px;background:red;color:white'; |
| 28 | document.body.appendChild(a); |
| 29 | if ((window as any).htmx) (window as any).htmx.process(a); |
| 30 | }, { url, id }); |
| 31 | await page.click(`#${id}`); |
| 32 | await expect(page).toHaveURL(url, { timeout: 10_000 }); |
| 33 | } |
| 34 | |
| 35 | const DEMO_A = '/patterns/loading/click-to-load'; |
| 36 | const DEMO_B = '/patterns/loading/lazy-load'; |
| 37 | const STUB = '/patterns/real-time/polling'; |
| 38 | |
| 39 | // Serial: SW state leaks between parallel workers |
| 40 | test.describe.serial('Pattern demos', () => { |
| 41 | test.setTimeout(30_000); |
| 42 | |
| 43 | test('renders on direct navigation', async ({ page }) => { |
| 44 | const errors: string[] = []; |
| 45 | page.on('pageerror', (err) => errors.push(err.message)); |
| 46 | |
| 47 | await page.goto(DEMO_A); |
| 48 | await waitForSw(page); |
| 49 | await waitForDemo(page); |
| 50 | |
| 51 | expect(errors).toEqual([]); |
| 52 | }); |
| 53 | |
| 54 | test('renders after reload', async ({ page }) => { |
| 55 | const errors: string[] = []; |
| 56 | page.on('pageerror', (err) => errors.push(err.message)); |
| 57 | |
| 58 | await page.goto(DEMO_A); |
| 59 | await waitForSw(page); |
| 60 | await waitForDemo(page); |
| 61 | |
| 62 | await page.reload(); |
| 63 | await waitForSw(page); |
| 64 | await waitForDemo(page); |
| 65 | |
| 66 | expect(errors).toEqual([]); |
| 67 | }); |
| 68 | |
| 69 | test('renders after morph from non-demo page', async ({ page }) => { |
| 70 | await page.goto('/about'); |
| 71 | await expect(page.locator('.prose')).toBeVisible({ timeout: 10_000 }); |
| 72 | |
| 73 | await morphViaLink(page, DEMO_A); |
| 74 | await waitForSw(page); |
| 75 | |
| 76 | // Check for pattern-specific content, not just any demo content |
| 77 | await expect(page.locator('#demo-content button', { hasText: /show more/i })) |
| 78 | .toBeVisible({ timeout: 15_000 }); |
| 79 | }); |
| 80 | |
| 81 | test('renders after morph between same-group demos', async ({ page }) => { |
| 82 | await page.goto('/patterns/loading/infinite-scroll'); |
| 83 | await waitForSw(page); |
| 84 | await waitForDemo(page); |
| 85 | |
| 86 | // Morph to click-to-load via sidebar link |
| 87 | await page.click('a[href="/patterns/loading/click-to-load"]'); |
| 88 | await expect(page).toHaveURL('/patterns/loading/click-to-load', { timeout: 10_000 }); |
| 89 | |
| 90 | // Verify click-to-load specific content appears (not stale infinite-scroll) |
| 91 | await expect(page.locator('#demo-content button', { hasText: /show more/i })) |
| 92 | .toBeVisible({ timeout: 15_000 }); |
| 93 | }); |
| 94 | |
| 95 | test('renders after morph between cross-group demos', async ({ page }) => { |
| 96 | await page.goto(DEMO_A); |
| 97 | await waitForSw(page); |
| 98 | await waitForDemo(page); |
| 99 | |
| 100 | // Cross-group navigation (Loading → Forms) via injected link |
| 101 | await morphViaLink(page, '/patterns/forms/active-search'); |
| 102 | |
| 103 | await expect(page.locator('#demo-content input[type="search"]')) |
| 104 | .toBeVisible({ timeout: 15_000 }); |
| 105 | }); |
| 106 | |
| 107 | test('Load More works after morph navigation', async ({ page }) => { |
| 108 | const errors: string[] = []; |
| 109 | page.on('pageerror', (err) => errors.push(err.message)); |
| 110 | |
| 111 | await page.goto('/patterns/loading/infinite-scroll'); |
| 112 | await waitForSw(page); |
| 113 | await waitForDemo(page); |
| 114 | |
| 115 | // Morph to click-to-load |
| 116 | await page.click('a[href="/patterns/loading/click-to-load"]'); |
| 117 | await expect(page).toHaveURL('/patterns/loading/click-to-load', { timeout: 10_000 }); |
| 118 | |
| 119 | const loadMore = page.locator('#demo-content button', { hasText: /show more/i }); |
| 120 | await expect(loadMore).toBeVisible({ timeout: 15_000 }); |
| 121 | |
| 122 | const initialCount = await page.locator('#demo-content #comments > div').count(); |
| 123 | await loadMore.click(); |
| 124 | await page.waitForTimeout(1500); |
| 125 | const newCount = await page.locator('#demo-content #comments > div').count(); |
| 126 | expect(newCount).toBeGreaterThan(initialCount); |
| 127 | expect(errors).toEqual([]); |
| 128 | }); |
| 129 | |
| 130 | test('renders after morph away then browser back', async ({ page }) => { |
| 131 | await page.goto(DEMO_A); |
| 132 | await waitForSw(page); |
| 133 | await waitForDemo(page); |
| 134 | |
| 135 | await page.click('nav a[href="/about"]'); |
| 136 | await expect(page).toHaveURL('/about', { timeout: 10_000 }); |
| 137 | |
| 138 | await page.goBack(); |
| 139 | await expect(page).toHaveURL(DEMO_A, { timeout: 10_000 }); |
| 140 | await waitForDemo(page); |
| 141 | }); |
| 142 | |
| 143 | test('renders after morph between demo pages and history nav', async ({ page }) => { |
| 144 | await page.goto(DEMO_A); |
| 145 | await waitForSw(page); |
| 146 | await waitForDemo(page); |
| 147 | |
| 148 | // Morph to demo B |
| 149 | await page.click(`a[href="${DEMO_B}"]`); |
| 150 | await expect(page).toHaveURL(DEMO_B, { timeout: 10_000 }); |
| 151 | await waitForDemo(page); |
| 152 | |
| 153 | // Back to A |
| 154 | await page.goBack(); |
| 155 | await expect(page).toHaveURL(DEMO_A, { timeout: 10_000 }); |
| 156 | await waitForDemo(page); |
| 157 | |
| 158 | // Forward to B |
| 159 | await page.goForward(); |
| 160 | await expect(page).toHaveURL(DEMO_B, { timeout: 10_000 }); |
| 161 | await waitForDemo(page); |
| 162 | }); |
| 163 | |
| 164 | test('no demo container on stub pages', async ({ page }) => { |
| 165 | await page.goto(STUB); |
| 166 | await expect(page.locator('.prose')).toBeVisible(); |
| 167 | |
| 168 | // Stub pages don't have a demo container (it's author-placed, not layout-injected) |
| 169 | const demoCount = await page.locator('#demo-content, .demo-container').count(); |
| 170 | expect(demoCount).toBe(0); |
| 171 | }); |
| 172 | |
| 173 | test('no errors on pages without demos', async ({ page }) => { |
| 174 | const errors: string[] = []; |
| 175 | page.on('pageerror', (err) => errors.push(err.message)); |
| 176 | |
| 177 | await page.goto(STUB); |
| 178 | await expect(page.locator('.prose')).toBeVisible(); |
| 179 | |
| 180 | expect(errors).toEqual([]); |
| 181 | }); |
| 182 | }); |