Files
novalon-website/e2e/reduced-motion-hydration.spec.ts
T
zhangxiang acb6b67357 feat(brand): 首页影像版式与名+号字标上线
- 首页按咨询机构影像版式重排,保留原站文案与“洞察→行业→能力”段序
- 导航/页脚/品牌页换用上下锁定字标,alt 维持法定主体名
- 修复 reduce 下 PageTransition 祖先 opacity 停留导致的白屏
- 4 浏览器 × 9 宽度多端复核,axe 0 violation
- 视觉基线 106 张更新并只读复跑 125/125
2026-10-20 11:09:32 +08:00

91 lines
3.8 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 } 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 下应「一步到位」变可见
await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight));
await page.waitForTimeout(1000);
const late = page.getByRole('heading', { name: lateHeading }).first();
await expect(late, '末段标题应存在(页面完整)').toBeVisible({ timeout: 10000 });
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([]);
});
}