markdown-table-motion.cases.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import assert from "node:assert/strict";
  2. import { test } from "node:test";
  3. import { join } from "node:path";
  4. export function registerMarkdownTableMotionCases(context) {
  5. for (const width of [390, 1200])
  6. for (const theme of ["light", "dark"])
  7. test(`STF06 table motion ${width}/${theme}`, async () => {
  8. const { browser, origin, artifacts } = context();
  9. const page = await browser.newPage({ viewport: { width, height: 850 } });
  10. const errors = [];
  11. page.on("pageerror", (error) => errors.push(String(error)));
  12. try {
  13. await page.goto(`${origin}fixture?table-motion=true&theme=${theme}`);
  14. await page
  15. .getByRole("button", { name: "Expand table scroll area", exact: true })
  16. .first()
  17. .waitFor();
  18. const sample = (action) =>
  19. page.evaluate(async (action) => {
  20. const frame = document.querySelector("[data-markdown-table-frame]");
  21. const content = frame.firstElementChild;
  22. const root = document.querySelector('[data-testid="table-layout-root"]');
  23. const widths = [];
  24. const transitions = new Set();
  25. const label =
  26. action === "expand" ? "Expand table scroll area" : "Collapse table scroll area";
  27. if (action === "resize")
  28. root.style.width = root.style.width === "80%" ? "100%" : "80%";
  29. else frame.parentElement.querySelector(`button[aria-label="${label}"]`).click();
  30. const start = performance.now();
  31. await new Promise((resolve) => {
  32. const tick = (now) => {
  33. widths.push(content.getBoundingClientRect().width);
  34. for (const animation of frame.getAnimations({ subtree: true })) {
  35. if (
  36. animation instanceof CSSTransition &&
  37. /^(width|max-width)$/.test(animation.transitionProperty)
  38. )
  39. transitions.add(animation);
  40. }
  41. if (now - start >= 500) resolve();
  42. else requestAnimationFrame(tick);
  43. };
  44. requestAnimationFrame(tick);
  45. });
  46. return {
  47. widths: new Set(widths.map((w) => Math.round(w))).size,
  48. transitions: transitions.size,
  49. contentWidth: content.getBoundingClientRect().width,
  50. frameWidth: frame.getBoundingClientRect().width,
  51. inlineTransition: content.style.transitionProperty,
  52. modes: [...document.querySelectorAll("[data-markdown-table-toolbar]")].map(
  53. (toolbar) => !!toolbar.querySelector('[aria-label="Collapse table scroll area"]'),
  54. ),
  55. };
  56. }, action);
  57. // 普通模式由 CSS 跟随;增强模式也不能叠加第二轮宽度动画。
  58. for (const mode of ["normal", "expanded"]) {
  59. if (mode === "expanded") {
  60. const expanded = await sample("expand");
  61. assert(expanded.transitions > 0 && expanded.widths > 2, JSON.stringify(expanded));
  62. assert.deepEqual(expanded.modes, [true, false]);
  63. assert.equal(expanded.inlineTransition, "");
  64. }
  65. const resized = await sample("resize");
  66. assert.equal(resized.transitions, 0, JSON.stringify(resized));
  67. assert(resized.widths > 2, JSON.stringify(resized));
  68. }
  69. const collapsed = await sample("collapse");
  70. assert(collapsed.transitions > 0 && collapsed.widths > 2, JSON.stringify(collapsed));
  71. assert(Math.abs(collapsed.contentWidth - collapsed.frameWidth) <= 1);
  72. assert.deepEqual(collapsed.modes, [false, false]);
  73. const interrupted = await page.evaluate(async () => {
  74. const frame = document.querySelector("[data-markdown-table-frame]");
  75. const content = frame.firstElementChild;
  76. frame.parentElement.querySelector('[aria-label="Expand table scroll area"]').click();
  77. await new Promise((resolve) =>
  78. requestAnimationFrame(() => requestAnimationFrame(resolve)),
  79. );
  80. const before = frame
  81. .getAnimations({ subtree: true })
  82. .filter(
  83. (animation) =>
  84. animation instanceof CSSTransition &&
  85. /^(width|max-width)$/.test(animation.transitionProperty),
  86. ).length;
  87. frame
  88. .querySelector("table")
  89. .parentElement.dispatchEvent(
  90. new WheelEvent("wheel", { deltaX: 50, bubbles: true, cancelable: true }),
  91. );
  92. await new Promise(requestAnimationFrame);
  93. return {
  94. before,
  95. transition: content.style.transitionProperty,
  96. offset: new DOMMatrix(getComputedStyle(content).transform).m41,
  97. };
  98. });
  99. assert(interrupted.before > 0, JSON.stringify(interrupted));
  100. assert.equal(interrupted.transition, "");
  101. assert(interrupted.offset < 0, JSON.stringify(interrupted));
  102. await sample("collapse");
  103. await page.emulateMedia({ reducedMotion: "reduce" });
  104. const reduced = await sample("expand");
  105. assert.equal(reduced.transitions, 0);
  106. assert.deepEqual(reduced.modes, [true, false]);
  107. await page.screenshot({ path: join(artifacts, `table-motion-${width}-${theme}.png`) });
  108. assert.deepEqual(errors, []);
  109. } finally {
  110. await page.close();
  111. }
  112. });
  113. }