From e9bd9654119a16f1829612a898662eb597d8f2b0 Mon Sep 17 00:00:00 2001 From: zhangxiang Date: Fri, 16 Oct 2026 20:14:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(nav):=20=E3=80=8C=E6=9F=A5=E7=9C=8B?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E3=80=8D=E4=B8=8D=E5=86=8D=E9=9D=A0=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E6=A0=87=E7=AD=BE=E5=8F=8D=E6=8E=A8=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=EF=BC=8C=E7=9B=AE=E7=9A=84=E5=9C=B0=E6=94=B9=E7=94=B1=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E9=A1=B9=E8=87=AA=E8=BA=AB=20href=20=E6=8F=90?= =?UTF-8?q?=E4=BE=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原实现(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 --- e2e/nav-keyboard.spec.ts | 69 ++++++++++++++++++++ src/components/layout/header.tsx | 2 + src/components/layout/mega-dropdown.test.tsx | 35 ++++++++++ src/components/layout/mega-dropdown.tsx | 38 +++++++---- 4 files changed, 132 insertions(+), 12 deletions(-) diff --git a/e2e/nav-keyboard.spec.ts b/e2e/nav-keyboard.spec.ts index 025874d..48fb1da 100644 --- a/e2e/nav-keyboard.spec.ts +++ b/e2e/nav-keyboard.spec.ts @@ -90,3 +90,72 @@ test.describe('主导航下拉的键盘可操作性', { tag: '@a11y' }, () => { 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 = { + 产品: '/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); + } + }); +}); diff --git a/src/components/layout/header.tsx b/src/components/layout/header.tsx index 79cfe00..f49ab3a 100644 --- a/src/components/layout/header.tsx +++ b/src/components/layout/header.tsx @@ -128,6 +128,8 @@ function HeaderContent() { key={item.id} label={item.label} groups={config.megaDropdown[item.dropdownKey!] ?? []} + /* 「查看全部」落点 = 导航项自身 href,不靠标签反推(原缺陷见 mega-dropdown.tsx props 注释) */ + viewAllHref={item.href} isOpen={openDropdown === item.id} isActive={isActive(item)} onToggle={() => { diff --git a/src/components/layout/mega-dropdown.test.tsx b/src/components/layout/mega-dropdown.test.tsx index 4cc5df3..72a9507 100644 --- a/src/components/layout/mega-dropdown.test.tsx +++ b/src/components/layout/mega-dropdown.test.tsx @@ -47,6 +47,41 @@ function renderDropdown(overrides: Partial[0]> = return props; } +describe('「查看全部」目的地必须来自导航项自身 href(回归)', () => { + // 缺陷:原实现写的是 + // href={`/${label === '产品' ? 'products' : label === '解决方案' ? 'solutions' : 'services'}`} + // 靠**中文标签字符串匹配**推断目标,第三个及以后任何标签都静默落到 /services。 + // 危害随 IA 改名而升级:改名前它表现为 404 般的不合理,改名后 /services 仍存在, + // 于是它会变成一个「看起来完全合理的错误目的地」—— 这才是最贵的那类 bug。 + it('标签为「洞察」时指向 /insights,而不是静默兜底到 /services', () => { + renderDropdown({ label: '洞察', viewAllHref: '/insights' }); + const link = screen.getByRole('link', { name: /查看全部洞察/ }); + expect(link).toHaveAttribute('href', '/insights'); + }); + + it('标签为「能力」时指向 /capabilities', () => { + renderDropdown({ label: '能力', viewAllHref: '/capabilities' }); + expect(screen.getByRole('link', { name: /查看全部能力/ })).toHaveAttribute('href', '/capabilities'); + }); + + it('既有两个标签仍各自正确(防修一个坏一个)', () => { + renderDropdown({ label: '产品', viewAllHref: '/products' }); + expect(screen.getByRole('link', { name: /查看全部产品/ })).toHaveAttribute('href', '/products'); + }); + + it('标签文案随 viewAllHref 一起走,不靠标签反推路由', () => { + renderDropdown({ label: '行业', viewAllHref: '/solutions' }); + const link = screen.getByRole('link', { name: /查看全部行业/ }); + // 行业轴的落地路径就是 /solutions(沿用既有 model 与已索引 URL,不做 rename) + expect(link).toHaveAttribute('href', '/solutions'); + }); + + it('未提供 viewAllHref ⇒ 不渲染该链接(宁可少一个入口,不给错目的地)', () => { + renderDropdown({ label: '洞察' }); + expect(screen.queryByRole('link', { name: /查看全部/ })).not.toBeInTheDocument(); + }); +}); + describe('MegaDropdown 键盘可达性(验收 B-10)', () => { beforeEach(() => { jest.clearAllMocks(); diff --git a/src/components/layout/mega-dropdown.tsx b/src/components/layout/mega-dropdown.tsx index 490ee21..bb146d1 100644 --- a/src/components/layout/mega-dropdown.tsx +++ b/src/components/layout/mega-dropdown.tsx @@ -11,12 +11,22 @@ interface MegaDropdownProps { groups: MegaDropdownGroup[]; isOpen: boolean; isActive?: boolean; + /** + * 「查看全部」的落点 —— **必须**由调用方(导航项自身 href)传入。 + * + * 原实现是 `href={`/${label === '产品' ? 'products' : label === '解决方案' ? 'solutions' : 'services'}`}`, + * 靠中文标签字符串匹配反推路由,第三个及以后任何标签都静默落到 /services。 + * 这类缺陷在 IA 改名时尤其危险:改名前它表现为一个刺眼的不合理跳转,改名后目标路由仍在, + * 于是它退化成一个「看起来完全合理的错误目的地」。 + * 现在不给目的地 ⇒ 干脆不渲染这个链接(宁可少一个入口,不给错目的地)。 + */ + viewAllHref?: string; onToggle: () => void; onOpen: () => void; onClose: () => void; } -export function MegaDropdown({ label, groups, isOpen, isActive, onToggle, onOpen, onClose }: MegaDropdownProps) { +export function MegaDropdown({ label, groups, isOpen, isActive, viewAllHref, onToggle, onOpen, onClose }: MegaDropdownProps) { const dropdownRef = useRef(null); const triggerRef = useRef(null); const timeoutRef = useRef>(undefined); @@ -76,7 +86,7 @@ export function MegaDropdown({ label, groups, isOpen, isActive, onToggle, onOpen > {label}
@@ -163,15 +174,18 @@ export function MegaDropdown({ label, groups, isOpen, isActive, onToggle, onOpen
))} -
- - 查看全部{label} - - -
+ {/* 目的地缺失 ⇒ 整块不渲染。见 props 里 viewAllHref 的缺陷说明 */} + {viewAllHref && ( +
+ + 查看全部{label} + + +
+ )} )}