ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/lenses/test/card-host.test.ts

test/card-host.test.ts

94 líneas, 5.2 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. import { expect, test } from "bun:test";
  2. import { spawnSync } from "node:child_process";
  3. import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { findChrome } from "../../shtukbase-ui-html/lib/overflow.ts";
  7. import { designFiles } from "../kit/src/build.js";
  8. const chrome = findChrome();
  9. const card = join(import.meta.dir, "../../layout-minimal/pages/design/layout-minimal/card");
  10. test("the selected layout bundles the complete card host before mounting", () => {
  11. const root = mkdtempSync(join(tmpdir(), "sb-layout-bundle-"));
  12. try {
  13. const layout = join(root, "design", "sample");
  14. const assets = join(layout, "card");
  15. mkdirSync(assets, { recursive: true });
  16. writeFileSync(join(root, "design", "design.md"), "---\nlayout: sample\n---\n");
  17. writeFileSync(join(layout, "layout.css"), "/* layout */");
  18. writeFileSync(join(layout, "layout.js"), "// mount");
  19. writeFileSync(join(assets, "card.css"), "/* card */");
  20. expect(designFiles(root).layout).toMatchObject({ css: "/* layout */", js: "// mount" });
  21. writeFileSync(join(assets, "card.js"), "// define host");
  22. expect(designFiles(root).layout).toMatchObject({ css: "/* layout */\n/* card */", js: "// define host\n// mount" });
  23. } finally {
  24. rmSync(root, { recursive: true, force: true });
  25. }
  26. });
  27. test.skipIf(!chrome)("card host preserves the settings panel and returns focus across mode changes", () => {
  28. const dir = mkdtempSync(join(tmpdir(), "sb-layout-card-"));
  29. try {
  30. const file = join(dir, "card.html");
  31. writeFileSync(file, `<!doctype html><style>
  32. .shell { --radius: 10px; --foreground: #222; --background: white; }
  33. ${readFileSync(join(card, "card.css"), "utf8")}
  34. [data-slot="peek"] { width: 100%; height: 180px; }
  35. #source { position: fixed; right: 2px; bottom: 2px; }
  36. .shell-right { position: fixed; right: 12px; top: 12px; bottom: 12px; width: 240px; }
  37. </style>
  38. <div class="shell" data-panel="open">
  39. <aside class="shell-right">Settings</aside>
  40. <button id="source">Source card</button>
  41. <div id="host" data-slot="layout-card-host" data-state="closed" hidden></div>
  42. </div>
  43. <script>${readFileSync(join(card, "card.js"), "utf8")}</script>
  44. <script>
  45. const source = document.querySelector("#source");
  46. const host = document.querySelector("#host");
  47. const shell = document.querySelector(".shell");
  48. const panel = shell.querySelector("aside");
  49. const frame = { shell, cardHost: host, panel: () => panel };
  50. window.LayoutCard.mount(frame);
  51. const peek = document.createElement("section");
  52. peek.dataset.slot = "peek";
  53. peek.innerHTML = '<h2 data-slot="peek-title">Package</h2><button data-slot="peek-close">Close</button><p data-slot="peek-body">Details</p>';
  54. const check = (condition, message) => { if (!condition) throw new Error(message); };
  55. try {
  56. source.focus();
  57. const side = frame.openCard(peek, { mode: "side", anchor: source });
  58. const dialog = host.querySelector("dialog");
  59. check(!host.hidden && host.dataset.mode === "side", "side opened");
  60. check(getComputedStyle(panel).display === "none" && shell.dataset.panel === "open", "side preserves the saved panel");
  61. check(document.activeElement.dataset.slot === "peek-close", "initial focus");
  62. check(!!peek.querySelector('[data-slot="scroll-area-viewport"]'), "body uses the scroll-area viewport");
  63. frame.openCard(peek, { mode: "center" });
  64. side.close();
  65. check(dialog.matches(":modal") && getComputedStyle(panel).display !== "none", "center is modal and leaves the panel visible");
  66. frame.openCard(peek, { mode: "peek", anchor: source });
  67. const r = dialog.getBoundingClientRect();
  68. check(!dialog.matches(":modal") && getComputedStyle(panel).display !== "none", "card leaves the panel visible");
  69. check(r.left >= 12 && r.top >= 12 && r.right <= innerWidth - 12 && r.bottom <= innerHeight - 12, "card fits at the viewport edge");
  70. check(r.right <= panel.getBoundingClientRect().left - 12, "card clears the settings panel when there is room");
  71. document.activeElement.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
  72. check(host.hidden && host.dataset.state === "closed", "Escape closes the host");
  73. check(document.activeElement === source, "focus returns to the source");
  74. check(getComputedStyle(panel).display !== "none", "the open settings panel comes back");
  75. document.body.dataset.result = "pass";
  76. } catch (error) { document.body.dataset.result = error.message; }
  77. </script>`);
  78. const result = spawnSync(chrome!, [
  79. "--headless=new", "--disable-gpu", "--no-first-run", "--no-default-browser-check", `--user-data-dir=${join(dir, "profile")}`,
  80. "--window-size=800,600", "--virtual-time-budget=3000", "--dump-dom", Bun.pathToFileURL(file).href,
  81. ], { encoding: "utf8", timeout: 30_000 });
  82. const outcome = result.stdout.match(/<body[^>]*data-result="([^"]+)"/)?.[1];
  83. expect(outcome, result.stderr.slice(0, 400)).toBe("pass");
  84. } finally {
  85. rmSync(dir, { recursive: true, force: true });
  86. }
  87. }, 40_000);