69 lines
2.2 KiB
JavaScript
69 lines
2.2 KiB
JavaScript
"use strict";
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const integration = require("../static/js/mermaid-racket-wiki.js");
|
|
|
|
function fixture(sourceText) {
|
|
let replacement = null;
|
|
const document = {
|
|
createElement() {
|
|
return {
|
|
attributes: {},
|
|
className: "",
|
|
innerHTML: "",
|
|
setAttribute(name, value) { this.attributes[name] = value; }
|
|
};
|
|
}
|
|
};
|
|
const source = {
|
|
classList: { values: [], add(value) { this.values.push(value); } },
|
|
dataset: {},
|
|
ownerDocument: document,
|
|
replaceWith(value) { replacement = value; },
|
|
title: ""
|
|
};
|
|
const code = { parentElement: source, textContent: sourceText };
|
|
const container = { querySelectorAll: () => [code] };
|
|
return { container, replacement: () => replacement, source };
|
|
}
|
|
|
|
test("initializes Mermaid with automatic rendering disabled and strict security", () => {
|
|
let config = null;
|
|
integration.initialize({ initialize(value) { config = value; } });
|
|
assert.deepEqual(config, {
|
|
startOnLoad: false,
|
|
securityLevel: "strict",
|
|
suppressErrorRendering: true
|
|
});
|
|
});
|
|
|
|
test("replaces a Mermaid code block with the rendered SVG", async () => {
|
|
const value = fixture("flowchart LR\n A --> B");
|
|
const mermaid = {
|
|
initialize() {},
|
|
async render(id, source) {
|
|
assert.match(id, /^racket-wiki-mermaid-/);
|
|
assert.equal(source, "flowchart LR\n A --> B");
|
|
return { svg: "<svg>diagram</svg>" };
|
|
}
|
|
};
|
|
await integration.hydrate(value.container, mermaid);
|
|
assert.equal(value.replacement().className, "rw-mermaid");
|
|
assert.equal(value.replacement().innerHTML, "<svg>diagram</svg>");
|
|
assert.equal(value.replacement().attributes.role, "img");
|
|
});
|
|
|
|
test("keeps invalid Mermaid source visible and marks it as an error", async () => {
|
|
const value = fixture("not a diagram");
|
|
const mermaid = {
|
|
initialize() {},
|
|
async render() { throw new Error("Parse error"); }
|
|
};
|
|
await integration.hydrate(value.container, mermaid);
|
|
assert.equal(value.replacement(), null);
|
|
assert.equal(value.source.dataset.mermaidState, "error");
|
|
assert.deepEqual(value.source.classList.values, ["rw-mermaid-error"]);
|
|
assert.equal(value.source.title, "Parse error");
|
|
});
|