Files
anidev-design-system/tests/panel-navigation.mjs
T

180 lines
6.6 KiB
JavaScript

import { chromium } from "playwright";
import assert from "node:assert/strict";
import { mkdir, writeFile } from "node:fs/promises";
const base = process.env.BASE_URL || "http://127.0.0.1:4173";
const browser = await chromium.launch({
executablePath:
process.env.PLAYWRIGHT_EXECUTABLE_PATH ||
"C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe",
headless: true,
});
const page = await browser.newPage();
const errors = [];
page.on("pageerror", (error) => errors.push(error.message));
let passed = 0;
await mkdir("references/panel-navigation", { recursive: true });
async function seed(status = "authenticated", subject = "demo-anna") {
await page.goto(base + "/?page=dashboard");
await page.evaluate(
async ({ status, subject }) => {
const { createDemoIdentityAdapter } =
await import("/patterns/demo-identity.js");
const adapter = createDemoIdentityAdapter();
adapter.setScenario(status);
const key = "anidev.identity.demo.v2",
data = JSON.parse(sessionStorage.getItem(key));
data.subject = subject;
sessionStorage.setItem(key, JSON.stringify(data));
},
{ status, subject },
);
}
try {
for (const theme of ["light", "dark"])
for (const width of [1440, 768, 390]) {
await page.setViewportSize({ width, height: 900 });
await seed();
await page.goto(`${base}/?page=profile&theme=${theme}&locale=ru`);
assert.equal(
await page.locator("body").getAttribute("data-panel"),
"user",
);
assert.equal(
await page.locator('.nav-links [data-page="security"]').count(),
0,
);
await page.locator(".dock-brand").click();
await page.locator(".dock-account").click();
await page.locator(".account-parent-link").hover();
await page.locator(".account-submenu:popover-open").waitFor();
await page.waitForTimeout(200);
const parent = await page.locator("#account-menu").boundingBox(),
sub = await page.locator(".account-submenu").boundingBox();
assert.ok(
sub.x >= 0 &&
sub.x + sub.width <= width &&
sub.y >= 0 &&
sub.y + sub.height <= 901,
);
assert.ok(
sub.x >= parent.x + parent.width + 4 ||
sub.x + sub.width <= parent.x - 4,
);
await page.screenshot({
path: `references/panel-navigation/flyout-${theme}-${width}.png`,
});
await page.locator('.account-submenu [data-page="security"]').hover();
await page.waitForTimeout(220);
assert.equal(await page.locator("#account-menu").isVisible(), true);
await page.locator('.account-submenu [data-page="security"]').click();
assert.equal(new URL(page.url()).searchParams.get("page"), "security");
assert.equal(
await page.locator(".dock-brand").getAttribute("aria-expanded"),
"true",
);
await page.keyboard.press("Escape");
await page.locator(".dock-account").click();
await page.locator(".account-parent-link").click();
assert.equal(new URL(page.url()).searchParams.get("page"), "profile");
passed++;
}
await seed();
await page.goto(base + "/?page=profile&theme=dark&locale=ru");
await page.locator(".dock-account").click();
await page.locator(".account-parent-link").focus();
await page.keyboard.press("ArrowRight");
assert.equal(
await page
.locator('.account-submenu [data-page="profile"]')
.evaluate((e) => e === document.activeElement),
true,
);
await page.keyboard.press("ArrowDown");
await page.keyboard.press("ArrowLeft");
assert.equal(
await page
.locator(".account-parent-link")
.evaluate((e) => e === document.activeElement),
true,
);
assert.equal(await page.locator(".account-submenu").isVisible(), false);
await page.keyboard.press("ArrowRight");
await page.keyboard.press("Escape");
assert.equal(await page.locator("#account-menu").isVisible(), true);
await page.keyboard.press("Escape");
assert.equal(await page.locator("#account-menu").isVisible(), false);
passed++;
await page.locator(".dock-account").click();
await page.locator('[data-panel="admin"]').click();
assert.equal(await page.locator("body").getAttribute("data-panel"), "admin");
assert.equal(await page.locator('.nav-links [data-page="users"]').count(), 1);
await page.goto(base + "/?page=dashboard&panel=user");
await page.locator(".dock-account").click();
await page.locator('#account-menu [data-panel="admin"]').click();
assert.equal(await page.locator("body").getAttribute("data-panel"), "admin");
assert.equal(await page.locator('.nav-links [data-page="users"]').count(), 1);
passed++;
await seed("authenticated", "demo-mikhail");
await page.goto(base + "/?page=dashboard&panel=admin");
assert.equal(await page.locator("body").getAttribute("data-panel"), "user");
assert.equal(await page.locator('.nav-links [data-page="users"]').count(), 0);
assert.equal(
await page.locator('#account-menu [data-panel="admin"]').count(),
0,
);
passed++;
for (const status of [
"signed_out",
"loading",
"unavailable",
"factor_required",
]) {
await seed(status);
await page.goto(base + "/?page=profile&theme=dark");
await page.locator(".identity-gate").waitFor();
assert.equal(
await page.locator(".navigation,.dock-scrim,.app-header").count(),
0,
);
await page.keyboard.press("Control+k");
assert.equal(await page.locator("dialog[open]").count(), 0);
if (status === "signed_out")
await page.screenshot({
path: "references/panel-navigation/signed-out.png",
});
passed++;
}
const touch = await browser.newContext({
viewport: { width: 390, height: 844 },
hasTouch: true,
isMobile: true,
});
const tp = await touch.newPage();
await tp.goto(base + "/?page=profile&theme=dark&locale=ru");
await tp.locator(".dock-account").tap();
await tp.locator(".account-submenu-toggle").tap();
await tp.locator(".account-submenu:popover-open").waitFor();
await tp.waitForTimeout(300);
await tp.locator('.account-submenu [data-page="sessions"]').tap();
assert.equal(new URL(tp.url()).searchParams.get("page"), "sessions");
await touch.close();
passed++;
assert.deepEqual(errors, []);
await writeFile(
"references/panel-navigation/qa.json",
JSON.stringify(
{
passed,
errors,
checks:
"Separate user/admin Dock; account side flyout in both themes/three widths; pointer travel, direct management click, keys, touch, no Dock for guest/loading/error/factor states",
},
null,
2,
) + "\n",
);
console.log(`Panel navigation: ${passed} checks passed, no runtime errors`);
} finally {
await browser.close();
}