Files
novalon-website/e2e/nav-keyboard.spec.ts
T
zhangxiang e9bd965411 fix(nav): 「查看全部」不再靠中文标签反推路由,目的地改由导航项自身 href 提供
原实现(mega-dropdown.tsx:168):
  href={`/${label === '产品' ? 'products' : label === '解决方案' ? 'solutions' : 'services'}`}
第三个及以后任何标签都**静默落到 /services**。

为什么必须在 IA 改名之前修:改名前这个缺陷表现为一个刺眼的不合理跳转,很容易被发现;
改名后 /services 这条路由依然存在(本轮刻意不做任何 rename),于是它退化成一个
「看起来完全合理的错误目的地」—— 那类最难查的 bug。

- props 新增 viewAllHref;缺失时**整块不渲染**该链接(宁可少一个入口,不给错目的地)
- header.tsx 把 item.href 传下去(非下拉分支 :145 本来就用同一个值,两分支现已同源)
- 顺带把两处裸时长收进令牌:duration-200 → duration-hover、framer transition 补 ease-ink

单测(mega-dropdown.test.tsx,5 条新用例)已实测能失败:
修复前 4 failed / 5 passed,其中「产品→/products」那条通过正是
「防修一个坏一个」的基准 —— 它证明旧实现在前两个标签上确实是对的。

e2e(nav-keyboard.spec.ts,2 条 @a11y)是**浏览器层**负对照:单测只能证明组件契约,
「header 有没有真的把 href 接上」只有真实渲染看得见。
已实测该用例能失败:摘除 viewAllHref 传递 ⇒ 立刻判红「下拉「产品」没有渲染「查看全部」链接」,
恢复 ⇒ 2 passed。另含两条不变量:分母必须等于已登记的下拉数(新增导航项忘登记即判红,
不会"少测一个"悄悄放行)、各下拉目的地必须互不相同(原缺陷的表现形式正是多个标签塌到同一个
/services)。

首跑踩到本文件 :21-29 已记录的 dev/Fast Refresh hydration 竞态 —— click() 打在
hydration 后被整体替换的旧节点上、aria-expanded 恒 false。按仓库既有配方改为
把**交互本身**放进 toPass 重发,而非只重读断言。

门禁:type-check EXIT=0 · eslint --quiet 0 error · jest src/components/layout 全过 ·
e2e nav-keyboard@chromium 2 passed
2026-10-16 20:14:19 +08:00

162 lines
8.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test, expect, type Page } from '@playwright/test';
import { expectHydrated } from './hydrated';
// 契约来源(逐行核对):src/components/layout/mega-dropdown.tsx:35-46(Escape/blur 关闭)、
// :62-76(触发器是 <button onClick>,带 aria-haspopup/aria-expanded)、:94-178(面板仅 isOpen 时挂载)。
// hover 路径已由 e2e/nav-dropdown.spec.ts 覆盖;本 spec 只补「无指针设备」路径(验收 B-10 的浏览器层)。
// jsdom 单测 mega-dropdown.test.tsx:55-74 用 fireEvent 测过 Escape,但 relatedTarget 与真实焦点顺序
// 在 jsdom 里不成立,不能替代浏览器验证。
const NAV_SELECTOR = 'nav[aria-label="主导航"]';
function productTrigger(page: Page) {
return page.getByRole('navigation', { name: '主导航' }).getByRole('button', { name: '产品', exact: true });
}
function viewAllLink(page: Page) {
return page.getByRole('link', { name: '查看全部产品' });
}
async function openWithKeyboard(page: Page) {
const trigger = productTrigger(page);
await page.goto('/');
await expect(trigger).toBeVisible();
await expectHydrated(trigger);
// dev 目标下该按钮的 DOM 节点会在 hydration 后被整体替换(实测:失败样本里 click/keydown
// 计数为 1 但 aria-expanded 恒 false、焦点不在按钮上;通过样本反过来是 click=0 而 aria=true,
// 说明监听挂在已被换掉的旧节点上)。生产产物不复现,属 dev/ Fast Refresh 竞态,
// 与 e2e/primary-nav.ts:11-14 记录的是同一类窗口 ⇒ 按本仓既有写法重发**交互**而不只是重读断言。
await expect(async () => {
await trigger.press('Enter');
await expect(trigger).toHaveAttribute('aria-expanded', 'true', { timeout: 2000 });
}).toPass({ timeout: 30000 });
await expect(viewAllLink(page)).toBeVisible();
return trigger;
}
test.describe('主导航下拉的键盘可操作性', { tag: '@a11y' }, () => {
// 桌面 mega dropdown 在移动断点整体不渲染(chromium-mobile 实测 element(s) not found);
// 移动导航是另一个组件(抽屉),它的键盘契约另立 spec,不在本文件假装覆盖。
test.beforeEach(async ({ viewport }) => {
test.skip(!!viewport && viewport.width < 768, '移动断点不渲染桌面 mega dropdown');
});
// 依赖真实 Tab 遍历的两例在 webkit 引擎上跳过:本机实测(:3120 生产产物)连续 4 次 Tab 后
// document.activeElement 仍是 BODY,而元素 .focus() 生效且 matches(':focus') 为 true。
// 与 e2e/p4-performance-a11y.spec.ts:240 记录的 N-27 同源(macOS WebKit 未开「全键盘访问」),
// 属引擎/系统默认值差异而非本仓缺陷;主配置的 chromium-mobile 同为 webkit 引擎,一并覆盖。
test('Tab 到达触发器后可用 Enter 展开', async ({ page, browserName }) => {
test.skip(browserName === 'webkit', 'WebKit 本机不响应真实 Tab 键(N-27,见上方注释)');
const trigger = productTrigger(page);
await page.goto('/');
await expect(trigger).toBeVisible();
await expectHydrated(trigger);
await expect(trigger).toHaveAttribute('aria-haspopup', 'true');
await expect(trigger).toHaveAttribute('aria-expanded', 'false');
// 焦点顺序实测(chromium + firefox,:3120 生产产物与 dev 目标读数一致):
// 第 1 站「跳转到主要内容」跳转链接,第 2 站 logo「返回首页」,第 3 站才是本触发器。
await page.keyboard.press('Tab');
await page.keyboard.press('Tab');
await page.keyboard.press('Tab');
await expect(trigger).toBeFocused();
await page.keyboard.press('Enter');
await expect(trigger).toHaveAttribute('aria-expanded', 'true');
await expect(viewAllLink(page)).toBeVisible();
});
test('Tab 焦点进入面板链接而不是跳过面板', async ({ page, browserName }) => {
test.skip(browserName === 'webkit', 'WebKit 本机不响应真实 Tab 键(N-27,见上方注释)');
await openWithKeyboard(page);
await page.keyboard.press('Tab');
await expect(page.locator(`${NAV_SELECTOR} a[href^="/products/"]:focus`)).toHaveCount(1);
});
test('Escape 关闭面板并把焦点还给触发器', async ({ page }) => {
const trigger = await openWithKeyboard(page);
await page.keyboard.press('Escape');
await expect(trigger).toHaveAttribute('aria-expanded', 'false');
await expect(trigger).toBeFocused();
// 卸载断言而非隐藏断言:抓源码注释担心的「看不见但仍在 DOM 里」滞留态
await expect(viewAllLink(page)).toHaveCount(0);
});
test('焦点移出下拉容器即关闭', async ({ page }) => {
const trigger = await openWithKeyboard(page);
await viewAllLink(page).press('Tab');
await expect(trigger).toHaveAttribute('aria-expanded', 'false');
await expect(viewAllLink(page)).toHaveCount(0);
});
});
// ── 「查看全部」目的地不变量 ──────────────────────────────────────────────
// 缺陷来源:mega-dropdown.tsx 原先用
// `/${label === '产品' ? 'products' : label === '解决方案' ? 'solutions' : 'services'}`
// 靠中文标签反推路由 ⇒ 第三个及以后任何标签都静默落到 /services。
// 修法是把导航项自身 href 传进去(props.viewAllHref)。
// 下面这条是它的**浏览器层负对照**:单测已锁住组件契约,但「header 有没有真的把 href 传下来」
// 只有在真实渲染里才看得见 —— 少传一个 prop,单测全绿而线上静默退化。
const VIEW_ALL_EXPECTED: Record<string, string> = {
产品: '/products',
解决方案: '/solutions',
};
test.describe('主导航「查看全部」的目的地', { tag: '@a11y' }, () => {
test.beforeEach(async ({ viewport }) => {
test.skip(!!viewport && viewport.width < 768, '移动断点不渲染桌面 mega dropdown');
});
test('每个下拉都必须有「查看全部」,且落点是该导航项自身路由(不得静默兜底)', async ({ page }) => {
await page.goto('/');
await expectHydrated(page.getByRole('navigation', { name: '主导航' }));
const nav = page.getByRole('navigation', { name: '主导航' });
const triggers = nav.locator('button[aria-haspopup="true"]');
const count = await triggers.count();
// 分母守卫:桌面下拉数量必须与预期表一致。
// 新增一个导航项却忘了登记预期 ⇒ 这里判红,而不是"少测一个"悄悄放行。
expect(count).toBe(Object.keys(VIEW_ALL_EXPECTED).length);
const destinations: string[] = [];
for (let i = 0; i < count; i++) {
const trigger = triggers.nth(i);
const label = (await trigger.innerText()).trim();
expect(VIEW_ALL_EXPECTED[label], `未登记的下拉标签「${label}」`).toBeTruthy();
// 重发**交互**而不是只重读断言:dev 目标下 hydration 会整体替换按钮 DOM 节点,
// 一次性 click() 会打在旧节点上、aria-expanded 恒 false(本文件 :21-29 已记同类竞态,
// openWithKeyboard() 用的是同一个配方)。生产产物不复现。
await expect(async () => {
await trigger.click();
await expect(trigger).toHaveAttribute('aria-expanded', 'true', { timeout: 2000 });
}).toPass({ timeout: 30000 });
const viewAll = nav.getByRole('link', { name: `查看全部${label}` });
// 原实现即便标签不认识也会渲染出一个链接(只是目的地错);
// 新实现在拿不到 href 时**整块不渲染** ⇒ 这条断言就是那个缺陷的正面哨兵。
await expect(viewAll, `下拉「${label}」没有渲染「查看全部」链接`).toBeVisible();
const href = await viewAll.getAttribute('href');
expect(href, `下拉「${label}」的查看全部无 href`).toBe(VIEW_ALL_EXPECTED[label]);
destinations.push(href ?? '(null)');
await page.keyboard.press('Escape');
}
// 独立目的地:原缺陷的表现形式正是多个标签塌到同一个 /services。
expect(new Set(destinations).size).toBe(destinations.length);
});
test('「查看全部」指向的路由必须真实可达(不是 404 的合理外观)', async ({ page }) => {
await page.goto('/');
await expectHydrated(page.getByRole('navigation', { name: '主导航' }));
for (const href of Object.values(VIEW_ALL_EXPECTED)) {
const resp = await page.request.get(href);
expect(resp.status(), `${href} 应可达`).toBe(200);
}
});
});