|
| 1 | +/** |
| 2 | + * Icon component regression tests |
| 3 | + * Ensures our gallery page mirrors the local svg set and that each icon remains accessible. |
| 4 | + */ |
| 5 | + |
| 6 | +import { BasePage, expect, test } from '@test/e2e/helpers' |
| 7 | +import { colorClasses } from '@components/Icon/constants' |
| 8 | +import type { Page } from '@playwright/test' |
| 9 | +import { promises as fs } from 'node:fs' |
| 10 | +import path from 'node:path' |
| 11 | + |
| 12 | +const iconVariants = Object.keys(colorClasses) as Array<keyof typeof colorClasses> |
| 13 | +const firstVariant = (iconVariants[0] ?? 'default') as keyof typeof colorClasses |
| 14 | +const selectors = { |
| 15 | + variantSection: (variant: string) => `section[aria-labelledby="icon-variant-${variant}"]`, |
| 16 | + iconSvg: 'svg[data-icon]' |
| 17 | +} |
| 18 | + |
| 19 | +async function readLocalIconNames(): Promise<string[]> { |
| 20 | + const iconsDir = path.resolve(process.cwd(), 'src/icons') |
| 21 | + const entries = await fs.readdir(iconsDir, { withFileTypes: true }) |
| 22 | + |
| 23 | + return entries |
| 24 | + .filter(entry => entry.isFile() && entry.name.endsWith('.svg')) |
| 25 | + .map(entry => entry.name.replace(/\.svg$/i, '')) |
| 26 | + .sort((a, b) => a.localeCompare(b)) |
| 27 | +} |
| 28 | + |
| 29 | +async function setupIconGallery(playwrightPage: Page): Promise<BasePage> { |
| 30 | + const page = await BasePage.init(playwrightPage) |
| 31 | + await page.goto('/testing/icons') |
| 32 | + await page.waitForSelector(selectors.variantSection(firstVariant)) |
| 33 | + await page.waitForSelector(`${selectors.variantSection(firstVariant)} ${selectors.iconSvg}`) |
| 34 | + return page |
| 35 | +} |
| 36 | + |
| 37 | +test.describe('Icon Component', () => { |
| 38 | + test('default variant renders every local icon exactly once', async ({ page: playwrightPage }) => { |
| 39 | + const page = await setupIconGallery(playwrightPage) |
| 40 | + const localIconNames = await readLocalIconNames() |
| 41 | + const defaultSection = page.locator(selectors.variantSection(firstVariant)) |
| 42 | + const defaultVariantSvgs = defaultSection.locator(selectors.iconSvg) |
| 43 | + |
| 44 | + await expect(defaultVariantSvgs).toHaveCount(localIconNames.length) |
| 45 | + |
| 46 | + const renderedNames = (await defaultSection.locator('span.font-mono').allTextContents()) |
| 47 | + .map(name => name.trim()) |
| 48 | + .filter(Boolean) |
| 49 | + .sort((a, b) => a.localeCompare(b)) |
| 50 | + |
| 51 | + expect(renderedNames).toEqual(localIconNames) |
| 52 | + }) |
| 53 | + |
| 54 | + test('each icon exposes a title element and valid SVG markup', async ({ page: playwrightPage }) => { |
| 55 | + const page = await setupIconGallery(playwrightPage) |
| 56 | + |
| 57 | + const auditResults = await page.evaluate((sectionSelector) => { |
| 58 | + const section = document.querySelector(sectionSelector) |
| 59 | + const missingTitles: string[] = [] |
| 60 | + const invalidSvg: string[] = [] |
| 61 | + |
| 62 | + if (!section) { |
| 63 | + return { missingTitles: ['icon-gallery-missing'], invalidSvg: ['icon-gallery-missing'] } |
| 64 | + } |
| 65 | + |
| 66 | + const svgNodes = Array.from(section.querySelectorAll<SVGSVGElement>('svg[data-icon]')) |
| 67 | + |
| 68 | + for (const svg of svgNodes) { |
| 69 | + const iconName = svg.getAttribute('data-icon') ?? 'unknown' |
| 70 | + const titleText = svg.querySelector('title')?.textContent?.trim() |
| 71 | + if (!titleText) { |
| 72 | + missingTitles.push(iconName) |
| 73 | + } |
| 74 | + |
| 75 | + const useElement = svg.querySelector('use') |
| 76 | + const href = useElement?.getAttribute('href') ?? '' |
| 77 | + const symbolId = href.startsWith('#') ? href.slice(1) : href |
| 78 | + const symbolElement = symbolId.length ? document.getElementById(symbolId) : null |
| 79 | + const isSvgElement = svg.namespaceURI === 'http://www.w3.org/2000/svg' |
| 80 | + const hasValidSymbol = Boolean(symbolElement && symbolElement.tagName.toLowerCase() === 'symbol') |
| 81 | + |
| 82 | + if (!isSvgElement || !hasValidSymbol) { |
| 83 | + invalidSvg.push(iconName) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return { missingTitles, invalidSvg } |
| 88 | + }, selectors.variantSection(firstVariant)) |
| 89 | + |
| 90 | + expect(auditResults.missingTitles, 'icons missing <title> text').toEqual([]) |
| 91 | + expect(auditResults.invalidSvg, 'icons missing valid <symbol> references').toEqual([]) |
| 92 | + }) |
| 93 | + |
| 94 | + test('every color variant renders the complete icon set', async ({ page: playwrightPage }) => { |
| 95 | + const page = await setupIconGallery(playwrightPage) |
| 96 | + const expectedCount = await page.locator(selectors.variantSection(firstVariant)).locator(selectors.iconSvg).count() |
| 97 | + |
| 98 | + for (const variant of iconVariants) { |
| 99 | + const section = page.locator(selectors.variantSection(variant)) |
| 100 | + await expect(section.locator(selectors.iconSvg)).toHaveCount(expectedCount) |
| 101 | + } |
| 102 | + }) |
| 103 | +}) |
0 commit comments