SPB Git

Repository Public

renderer.full.test.tsx 7.5 KB 108 lines tsx

1import { describe, expect, it } from "vitest";2import { renderToStaticMarkup } from "react-dom/server";3import { BLOCK_TYPES, FORMATS, FORMAT_SIZES, THEMES, allBlocks, type Block } from "@/schema";4import { BlockView, REGISTRY, SheetView, buildCtx } from "@/renderer/SheetView";5import { ICON_GLYPHS, IconGlyph } from "@/renderer/icons";6import { ICONS } from "@/schema";7import { fullDocument } from "./renderer.fixture";89const doc = fullDocument();1011describe("SheetView — full document", () => {12  it("the fixture contains every block type, and the registry covers them all", () => {13    const used = new Set(allBlocks(doc).map((b) => b.type));14    for (const type of BLOCK_TYPES) { expect(used.has(type), type).toBe(true); expect(typeof REGISTRY[type], type).toBe("function"); }15    expect(Object.keys(REGISTRY).sort()).toEqual([...BLOCK_TYPES].sort());16  });1718  for (const mode of ["public", "editor", "print"] as const) {19    it(`renders in ${mode} mode`, () => {20      const html = renderToStaticMarkup(<SheetView doc={doc} mode={mode} assetUrl={(id) => `/assets/${id}`} />);21      expect(html).toContain(`<article class="sheet" data-theme="scientific-light" data-accent="blue" data-format="article" data-mode="${mode}" lang="fr">`);22      expect(html.replace(/<div class="sh-simulation">[\s\S]*?<div id="dv"/, "")).not.toMatch(/undefined|NaN|\[object Object\]/);23      // Every top-level block has its wrapper, in document order.24      const ids = Array.from(html.matchAll(/<div id="([a-z0-9-]+)" class="sh-block sh-span-\d+(?: sh-qs-wide)?" data-block-id="\1" data-block-type="([a-z]+)"/g)).map((m) => m[1]);25      expect(ids).toEqual(doc.blocks.map((b) => b.id));26      // One h1; the level-1 heading inside a tab is demoted.27      expect(html.match(/<h1\b/g)).toHaveLength(1);28      expect(html).toContain('<h2 class="sh-h sh-h1">Titre dans un onglet</h2>');29      expect(html).toContain("Exemple de démonstration — non révisé par l&#x27;auteur");30      // Inline subset, citations, references.31      expect(html).toContain('<sup class="sh-cite"><a href="#src-s1" aria-label="Source 1">1</a></sup>');32      expect(html).toContain('id="src-s1"'); expect(html).toContain('href="https://doi.org/10.1162/neco.1991.3.1.79"');33      expect(html).toContain("non vérifiée"); expect(html).toContain("non vérifiée — source injoignable");34      expect(html).toContain('<p class="sh-sourceline">');35      // Math through KaTeX with MathML; equation flag and variables.36      expect(html).toContain("katex-display"); expect(html).toContain("<math"); expect(html).toContain("Résultat dérivé dans cette fiche"); expect(html).toContain('class="sh-vars"');37      // Visual blocks carry a text alternative.38      expect(html).toContain('role="img" aria-label="Schéma du pipeline"'); expect(html).toContain('role="img" aria-label="Perte par époque"');39      expect(html).toContain('aria-label="Matrice de corrélation"'); expect(html).toContain('aria-label="Un cercle"');40      expect(html).toContain('<img src="/assets/3f2b8c1e-4d5a-4e6f-8a9b-0c1d2e3f4a5b" alt="Schéma d&#x27;une figure"');41      expect(html).toContain(mode === "print" ? 'loading="eager"' : 'loading="lazy"');42      // Static (server) output of the interactive blocks: all steps, all answers, all tab panels.43      expect(html).toContain("sh-diagram--static"); expect(html).toContain('class="sh-diagram-steps"'); expect(html).toContain("Routage");44      expect(html).toContain("sh-quiz--static"); expect(html).toContain("Voir la réponse");45      expect(html).toContain("sh-tabs--static"); expect(html).toContain("Contenu du premier onglet."); expect(html).toContain('id="tq"');46      expect(html).not.toMatch(/class="sh-(btn|quiz-choice|tab)[" ]/); expect(html).not.toContain('role="tab"');47      // Data table of charts, data-kind badges, scroll containers.48      expect(html).toContain("Voir les données"); expect(html).toContain("DONNÉES SYNTHÉTIQUES"); expect(html).toContain("DONNÉES EMPIRIQUES"); expect(html).toContain("DONNÉES ILLUSTRATIVES");49      expect(html).toContain('class="sh-scroll sh-table-scroll" tabindex="0" role="region"'); expect(html).toContain("<caption");50      expect(html).toContain('class="sh-code-line"'); expect(html).toContain('class="sh-tok-keyword">def</span>');51      expect(html).toContain("<details"); expect(html).toContain('<div class="sh-simulation">');52      // The hostile bits of the fixture never reach the page.53      expect(html).not.toContain("position:fixed"); expect(html).toContain("Légende &lt;b&gt;");54      expect(html).not.toMatch(/\sstyle="[^"]*order\s*:/);55      if (mode === "print") expect(html).toMatch(/<details class="sh-accordion-item" open="">/);56    });57  }5859  it("renders every theme, every fixed format and both locales", () => {60    for (const preset of THEMES) for (const format of FORMATS) {61      const d = fullDocument({ theme: { preset, accent: "violet" }, layout: { format } });62      const html = renderToStaticMarkup(<SheetView doc={d} locale="en" mode="print" />);63      expect(html).toContain(`data-theme="${preset}" data-accent="violet" data-format="${format}"`);64      expect(html).toContain('lang="en"'); expect(html).toContain("Demonstration example — not reviewed by the author");65      const size = FORMAT_SIZES[format];66      if (format === "article") expect(html).toContain('<div class="sh-frame" style="max-width:1120px">');67      else expect(html).toContain(`<div class="sh-frame sh-frame--fixed" style="width:${size.width}px;min-height:${size.height}px;padding:${size.safeMargin}px">`);68    }69  });7071  it("shows no banner unless the document is an example, and lets the editor wrap blocks", () => {72    const d = fullDocument({ metadata: { title: "T", locale: "fr" } });73    const html = renderToStaticMarkup(<SheetView doc={d} mode="editor" wrapBlock={(block, node) => <div className="chrome" data-wrap={block.id}>{node}</div>} />);74    expect(html).not.toContain("sh-example-banner");75    expect(html.match(/class="chrome"/g)).toHaveLength(d.blocks.length);76    expect(html).toContain('data-block-type="heading"><div class="chrome" data-wrap="h1">');77  });7879  it("BlockView renders a single block and ignores unknown types", () => {80    const ctx = buildCtx(doc, "fr", "public");81    for (const block of allBlocks(doc)) expect(() => renderToStaticMarkup(<BlockView block={block} ctx={ctx} />)).not.toThrow();82    expect(renderToStaticMarkup(<BlockView block={{ id: "x", type: "nope", span: 12 } as unknown as Block} ctx={ctx} />)).toBe("");83    expect(ctx.assetUrl("a b")).toBe("/media/a%20b");84  });8586  it("survives an image whose asset is missing", () => {87    const d = { ...doc, assets: [] };88    expect(renderToStaticMarkup(<SheetView doc={d} />)).toContain("Image indisponible");89  });90});9192describe("icons", () => {93  it("draws all 50 icons", () => {94    expect(ICONS).toHaveLength(50);95    expect(Object.keys(ICON_GLYPHS).sort()).toEqual([...ICONS].sort());96    const seen = new Set<string>();97    for (const name of ICONS) {98      const out = renderToStaticMarkup(<IconGlyph name={name} size={24} />);99      expect(out).toContain('viewBox="0 0 24 24"'); expect(out).toContain('stroke-width="1.75"'); expect(out).toContain('stroke-linecap="round"'); expect(out).toContain('aria-hidden="true"');100      expect(out).toMatch(/<(path|circle|rect|ellipse)\b/);101      expect(seen.has(out.replace(/data-icon="[a-z]+"/, "")), `${name} duplicates another glyph`).toBe(false);102      seen.add(out.replace(/data-icon="[a-z]+"/, ""));103      // Path data stays on the 24×24 grid.104      for (const d of out.matchAll(/ d="([^"]+)"/g)) expect(d[1]).toMatch(/^[Mm][\d\s.,a-zA-Z-]+$/);105    }106  });107});108
Files main
Menu

Theme