Files
novalon-website/e2e/reduced-motion-hydration.spec.ts
zhangxiang 2f86c4419a fix(motion): 站内跳转白屏——路由内容不再走 AnimatePresence wait 退场
现象:生产环境所有站内跳转白屏,地址与标题已切换,刷新才恢复。
根因:AnimatePresence mode="wait" 要求新页等旧页退场完成才挂载;App Router
导航期间的重复渲染会重置退场完成标记,切换 state 的窗口被错过,
motion.div 永久停在 exit 变体(opacity:0; translateY(-8px))。
修法:路由内容只保留 key={pathname} 的 280ms 入场;首屏仍由 mounted 渲染
裸 children;reduce 变体改为显式可见终值,避免切到空变体时冻结在半透明。
守卫:新增 client-navigation-visibility(导航后落定再读祖先链乘积);
首屏用例改逐帧采样;视觉用例改逐屏滚动触发 whileInView。
验证:E2E 20/20(四浏览器)、视觉 125/125、单测 1815、type-check/lint 0 error。
2026-10-20 14:00:41 +08:00

93 lines
4.0 KiB
TypeScript
Raw Permalink 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 } from './fixtures';
/**
* 减少动效(prefers-reduced-motion: reduce)下的 hydration 与可见性回归。
*
* 缺陷原型(2026-10-20 从生产排查出来):`useReducedMotion` 用 `window.matchMedia` 作 useState 初值,
* 于是 SSR 恒 false、客户端首帧 true;组件再用它决定 `initial`/`variants`,
* 导致 ①React #418(hydration 不匹配)②减少动效用户看到大片元素停在 opacity:0。
*
* 断言两条,都不依赖像素:
* ① 页面加载与滚动过程中**不得出现 hydration 相关错误**(console.error 或 pageerror);
* ② 滚到底部后,末段标题的 opacity 必须为 1(内容真的可见,而不是停在初始隐藏态)。
*
* 敏感性:把 hooks/use-reduced-motion.ts 的 `useState(false)` 改回 matchMedia 初值,
* 或把 scroll-reveal 的 `initial={…}` 改回 `shouldReduceMotion ? {} : …` ⇒ 本例应转红。
*/
// 用 contextOptions(BrowserContextOptions 的正式字段)而不是 test.use 的便捷项:
// 后者不在本仓自定义 fixtures 的 TestOptions 推导里(type-check:e2e 会拦),且等价。
test.use({ contextOptions: { reducedMotion: 'reduce' } });
const HYDRATION_HINTS = [/hydrat/i, /did not match/i, /#418/, /Minified React error/];
function collectErrors(page: import('@playwright/test').Page) {
const hits: string[] = [];
page.on('pageerror', (e) => {
const s = String(e);
if (HYDRATION_HINTS.some((r) => r.test(s))) hits.push(`pageerror: ${s.slice(0, 240)}`);
});
page.on('console', (m) => {
if (m.type() !== 'error') return;
const s = m.text();
if (HYDRATION_HINTS.some((r) => r.test(s))) hits.push(`console: ${s.slice(0, 240)}`);
});
return hits;
}
const CASES = [
{ path: '/about', lateHeading: /准备好开始了吗/ },
{ path: '/', lateHeading: /现在有什么卡住你了/ },
];
for (const { path, lateHeading } of CASES) {
test(`${path} 在 reduce 下无 hydration 错误且末段可见`, async ({ page }) => {
const errors = collectErrors(page);
await page.goto(path, { waitUntil: 'networkidle', timeout: 60000 });
// 自证:确实处在 reduce 偏好下,否则本用例什么都没测到
expect(
await page.evaluate(() => matchMedia('(prefers-reduced-motion: reduce)').matches),
'reducedMotion 未生效',
).toBe(true);
const h1 = page.locator('h1').first();
await expect(h1).toBeVisible({ timeout: 15000 });
// 2026-10-20:只看元素自身的 opacity 会漏掉 PageTransition 祖先停在 0.15–0.35 的那类白屏。
// 有效可见度必须是祖先链的乘积累积,而不是单节点读数。
const h1Opacity = await h1.evaluate((el) => {
let op = 1;
let n: Element | null = el;
while (n) {
op *= Number(getComputedStyle(n).opacity);
n = n.parentElement;
}
return op;
});
expect(h1Opacity, '首屏 h1 的祖先链不得停在半透明/0').toBeGreaterThan(0.99);
// 把目标滚进视口,让 whileInView 触发;reduce 下应「一步到位」变可见。
// 不能 `window.scrollTo(0, scrollHeight)` 直接跳底:reduce 会禁用平滑滚动(瞬时跳转),
// 目标从视口外被越过、IntersectionObserver 永不回调 ⇒ 探针自设条件造成假红。
const late = page.getByRole('heading', { name: lateHeading }).first();
await expect(late, '末段标题应存在(页面完整)').toBeVisible({ timeout: 10000 });
await late.scrollIntoViewIfNeeded();
await page.waitForTimeout(1000);
await expect
.poll(
async () =>
late.evaluate((el) => {
let op = 1;
let n: Element | null = el;
while (n) {
op *= Number(getComputedStyle(n).opacity);
n = n.parentElement;
}
return op;
}),
{ timeout: 5000 },
)
.toBeGreaterThan(0.99);
expect(errors, `出现 hydration 错误:\n${errors.join('\n')}`).toEqual([]);
});
}