Add interactive component catalog and browser regression tests
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import AxeBuilder from "@axe-core/playwright";
|
||||
import { expect, test } from "@playwright/test";
|
||||
|
||||
const openExample = async (page) => {
|
||||
await page.goto("/examples/index.html");
|
||||
await page.addStyleTag({ content: "html { scroll-behavior: auto !important; }" });
|
||||
await page.evaluate(() => document.fonts.ready);
|
||||
};
|
||||
|
||||
const formatViolations = (violations) => violations.map(({ help, helpUrl, id, impact, nodes }) => ({
|
||||
help,
|
||||
helpUrl,
|
||||
id,
|
||||
impact,
|
||||
targets: nodes.map((node) => node.target),
|
||||
}));
|
||||
|
||||
test.beforeEach(async ({ page }) => {
|
||||
await openExample(page);
|
||||
});
|
||||
|
||||
const visualCases = [
|
||||
{ id: "buttons", name: "buttons" },
|
||||
{ id: "forms", name: "form controls" },
|
||||
{ id: "surfaces", name: "surfaces and badges" },
|
||||
{ id: "feedback", name: "feedback" },
|
||||
{ id: "menus", name: "menus and tooltips", prepare: async (page) => {
|
||||
if (await page.locator("#example-menu").isHidden()) await page.locator("#menu-trigger").click();
|
||||
} },
|
||||
{ id: "navigation", name: "navigation" },
|
||||
{ id: "disclosure", name: "tabs and accordion" },
|
||||
{ id: "data", name: "data and status" },
|
||||
{ id: "modal", name: "modal", target: "#example-dialog .frost-modal__content", prepare: async (page) => {
|
||||
if (!await page.locator("#example-dialog").evaluate((dialog) => dialog.open)) await page.locator("#modal-trigger").click();
|
||||
} },
|
||||
];
|
||||
|
||||
for (const visualCase of visualCases) {
|
||||
test(`${visualCase.name} matches its component baselines`, async ({ page }) => {
|
||||
await page.addStyleTag({ content: ".frost-skip-link { display: none !important; }" });
|
||||
for (const theme of ["light", "dark"]) {
|
||||
await page.locator("html").evaluate((element, selectedTheme) => {
|
||||
element.dataset.theme = selectedTheme;
|
||||
}, theme);
|
||||
await visualCase.prepare?.(page);
|
||||
const target = page.locator(visualCase.target ?? `#${visualCase.id}`);
|
||||
await expect(target).toHaveScreenshot(`component-${visualCase.id}-${theme}.png`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (const theme of ["light", "dark"]) {
|
||||
test(`has no detectable WCAG A or AA violations in the ${theme} theme`, async ({ page }) => {
|
||||
await page.locator("html").evaluate((element, selectedTheme) => {
|
||||
element.dataset.theme = selectedTheme;
|
||||
}, theme);
|
||||
|
||||
const { violations } = await new AxeBuilder({ page })
|
||||
.withTags(["wcag2a", "wcag2aa", "wcag21a", "wcag21aa", "wcag22aa"])
|
||||
.analyze();
|
||||
|
||||
expect(formatViolations(violations), "Axe accessibility violations").toEqual([]);
|
||||
});
|
||||
}
|
||||
|
||||
test("dropdown exposes state and closes with Escape", async ({ page }) => {
|
||||
const trigger = page.getByRole("button", { name: "Menü öffnen" });
|
||||
const menu = page.locator("#example-menu");
|
||||
|
||||
await trigger.click();
|
||||
await expect(trigger).toHaveAttribute("aria-expanded", "true");
|
||||
await expect(menu).toBeVisible();
|
||||
|
||||
await page.keyboard.press("Escape");
|
||||
await expect(trigger).toHaveAttribute("aria-expanded", "false");
|
||||
await expect(menu).toBeHidden();
|
||||
await expect(trigger).toBeFocused();
|
||||
});
|
||||
|
||||
test("tabs support click and arrow-key navigation", async ({ page }) => {
|
||||
const overview = page.getByRole("tab", { name: "Übersicht" });
|
||||
const details = page.getByRole("tab", { name: "Details" });
|
||||
|
||||
await details.click();
|
||||
await expect(details).toHaveAttribute("aria-selected", "true");
|
||||
await expect(page.locator("#tab-panel-two")).toBeVisible();
|
||||
await expect(page.locator("#tab-panel-one")).toBeHidden();
|
||||
|
||||
await page.keyboard.press("ArrowLeft");
|
||||
await expect(overview).toBeFocused();
|
||||
await expect(overview).toHaveAttribute("aria-selected", "true");
|
||||
await expect(page.locator("#tab-panel-one")).toBeVisible();
|
||||
});
|
||||
|
||||
test("accordion keeps its control and panel state synchronized", async ({ page }) => {
|
||||
const trigger = page.getByRole("button", { name: "Was macht Frost?" });
|
||||
const panel = page.locator("#accordion-panel");
|
||||
|
||||
await trigger.click();
|
||||
await expect(trigger).toHaveAttribute("aria-expanded", "true");
|
||||
await expect(panel).toBeVisible();
|
||||
|
||||
await trigger.click();
|
||||
await expect(trigger).toHaveAttribute("aria-expanded", "false");
|
||||
await expect(panel).toBeHidden();
|
||||
});
|
||||
|
||||
test("catalog search filters components and navigation", async ({ page }) => {
|
||||
const search = page.locator("#component-search");
|
||||
|
||||
await search.fill("modal");
|
||||
await expect(page.locator("#modal")).toBeVisible();
|
||||
await expect(page.locator("#buttons")).toBeHidden();
|
||||
await expect(page.locator("#visible-count")).toHaveText("1 von 9 Gruppen");
|
||||
|
||||
await search.fill("unbekannt");
|
||||
await expect(page.locator("#catalog-empty")).toBeVisible();
|
||||
});
|
||||
|
||||
test("copy buttons expose the live HTML snippet", async ({ page }) => {
|
||||
const component = page.locator("#buttons");
|
||||
const code = component.locator("[data-snippet-code]");
|
||||
const copy = component.locator("[data-copy-html]");
|
||||
|
||||
await expect(code).toContainText("frost-button--primary");
|
||||
await copy.click();
|
||||
await expect(copy).toHaveText("Kopiert");
|
||||
await expect(page.locator("#copy-status")).toContainText("Buttons: HTML kopiert");
|
||||
});
|
||||
|
||||
test("modal restores focus to its trigger after closing", async ({ page }) => {
|
||||
const trigger = page.locator("#modal-trigger");
|
||||
const dialog = page.locator("#example-dialog");
|
||||
|
||||
await trigger.click();
|
||||
await expect(dialog).toBeVisible();
|
||||
await dialog.getByRole("button", { name: "Modal schließen" }).click();
|
||||
await expect(dialog).toBeHidden();
|
||||
await expect(trigger).toBeFocused();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user