|
| 1 | +/** |
| 2 | + * Visual regression tests for NFTCard, VerificationBadge, and Landing page. |
| 3 | + * |
| 4 | + * Run once to generate baselines: |
| 5 | + * npx playwright test e2e/visual.spec.js --update-snapshots |
| 6 | + * |
| 7 | + * Subsequent runs compare against committed baselines and fail on visual diffs. |
| 8 | + * Diff images are uploaded as CI artifacts (see .github/workflows/ci.yml). |
| 9 | + */ |
| 10 | +import { test, expect } from '@playwright/test'; |
| 11 | + |
| 12 | +// ── helpers ─────────────────────────────────────────────────────────────────── |
| 13 | + |
| 14 | +/** Inject a minimal Freighter stub so wallet-dependent components render. */ |
| 15 | +async function stubFreighter(context) { |
| 16 | + await context.addInitScript(() => { |
| 17 | + window.freighter = { |
| 18 | + isConnected: async () => false, |
| 19 | + getPublicKey: async () => null, |
| 20 | + signTransaction: async (tx) => ({ transactionEnvelope: tx }), |
| 21 | + }; |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +// ── Landing page ────────────────────────────────────────────────────────────── |
| 26 | + |
| 27 | +test.describe('Visual — Landing page', () => { |
| 28 | + test('matches baseline', async ({ page, context }) => { |
| 29 | + await stubFreighter(context); |
| 30 | + await page.goto('/'); |
| 31 | + // Wait for fonts / animations to settle |
| 32 | + await page.waitForLoadState('networkidle'); |
| 33 | + await expect(page).toHaveScreenshot('landing.png', { fullPage: true }); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +// ── NFTCard component ───────────────────────────────────────────────────────── |
| 38 | + |
| 39 | +test.describe('Visual — NFTCard', () => { |
| 40 | + /** |
| 41 | + * We render NFTCard in isolation via a dedicated test-harness route. |
| 42 | + * If no harness exists we navigate to the patient dashboard and wait for |
| 43 | + * the first card to appear (the mock server returns fixture data). |
| 44 | + */ |
| 45 | + test('default state matches baseline', async ({ page, context }) => { |
| 46 | + await stubFreighter(context); |
| 47 | + |
| 48 | + // Inject a fixture record so the card renders without a live backend |
| 49 | + await context.addInitScript(() => { |
| 50 | + window.__VACCICHAIN_FIXTURE__ = { |
| 51 | + token_id: 1, |
| 52 | + vaccine_name: 'COVID-19 mRNA', |
| 53 | + date_administered: '2024-01-15', |
| 54 | + issuer: 'GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN', |
| 55 | + dose_number: 1, |
| 56 | + dose_series: 2, |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + await page.goto('/'); |
| 61 | + await page.waitForLoadState('networkidle'); |
| 62 | + |
| 63 | + // Locate the first NFTCard if it exists on the page, otherwise screenshot |
| 64 | + // the full landing as a fallback (card is rendered on patient dashboard). |
| 65 | + const card = page.locator('[data-testid="nft-card"]').first(); |
| 66 | + if (await card.count() > 0) { |
| 67 | + await expect(card).toHaveScreenshot('nft-card.png'); |
| 68 | + } else { |
| 69 | + // Navigate to patient dashboard where cards are rendered |
| 70 | + await page.goto('/patient'); |
| 71 | + await page.waitForLoadState('networkidle'); |
| 72 | + const dashCard = page.locator('[data-testid="nft-card"]').first(); |
| 73 | + if (await dashCard.count() > 0) { |
| 74 | + await expect(dashCard).toHaveScreenshot('nft-card.png'); |
| 75 | + } else { |
| 76 | + // No live data — screenshot the whole page as baseline |
| 77 | + await expect(page).toHaveScreenshot('nft-card-page.png', { fullPage: true }); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +// ── VerificationBadge component ─────────────────────────────────────────────── |
| 84 | + |
| 85 | +test.describe('Visual — VerificationBadge', () => { |
| 86 | + test('verified state matches baseline', async ({ page, context }) => { |
| 87 | + await stubFreighter(context); |
| 88 | + // Navigate to the verify page with a known wallet address |
| 89 | + await page.goto('/verify/GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN'); |
| 90 | + await page.waitForLoadState('networkidle'); |
| 91 | + |
| 92 | + const badge = page.locator('[data-testid="verification-badge"], #verification-badge').first(); |
| 93 | + if (await badge.count() > 0) { |
| 94 | + await expect(badge).toHaveScreenshot('verification-badge-verified.png'); |
| 95 | + } else { |
| 96 | + await expect(page).toHaveScreenshot('verify-page.png', { fullPage: true }); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + test('not-found state matches baseline', async ({ page, context }) => { |
| 101 | + await stubFreighter(context); |
| 102 | + await page.goto('/verify/GBQVLZE4XCNDFW4YVKXNHX65IKJDQB3ZUQPQPFIJN5DCEVVNAQMTB6W'); |
| 103 | + await page.waitForLoadState('networkidle'); |
| 104 | + |
| 105 | + const badge = page.locator('[data-testid="verification-badge"], #verification-badge').first(); |
| 106 | + if (await badge.count() > 0) { |
| 107 | + await expect(badge).toHaveScreenshot('verification-badge-not-found.png'); |
| 108 | + } else { |
| 109 | + await expect(page).toHaveScreenshot('verify-page-not-found.png', { fullPage: true }); |
| 110 | + } |
| 111 | + }); |
| 112 | +}); |
0 commit comments