Files
racket-wiki/test/cmap-submap-navigation.test.js
T
2026-09-02 08:38:03 +02:00

75 lines
2.2 KiB
JavaScript

"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
function fakeNode(attributes) {
const state = { ...attributes };
return {
attr(value) {
if (typeof value === "string") return state[value];
Object.assign(state, value);
return this;
},
onRendered() {}, onMove() {}, onMoveEnd() {}, visible() {}, redraw() {},
toFront() {}, remove() {}, element: () => null
};
}
async function loadEditorFactory() {
global.Element = class Element {};
global.document = {
currentScript: null,
styleSheets: [],
body: { append() {} },
createElement: () => ({ style: {}, remove() {} })
};
global.window = {
Cmap: null,
setTimeout,
clearTimeout,
requestAnimationFrame: (callback) => callback()
};
await import("../static/cmap/cmap-racket-wiki.js");
return global.window.RacketWikiCmap;
}
test("back from a second nested submap returns exactly one map level", async () => {
const factory = await loadEditorFactory();
const map = {
onSelection() {},
onActivation() {},
node: (attributes) => fakeNode(attributes)
};
const canvas = {
addEventListener() {}, querySelector: () => null,
clientWidth: 1200, clientHeight: 800, scrollLeft: 0, scrollTop: 0,
isConnected: true
};
const editor = factory.createEditor(canvas, { Cmap: () => map });
const first = editor.addItem({
id: 1, kind: "submap", label: "First level", separateMap: true,
mapReference: { id: "first", title: "First level" },
x: 100, y: 100, width: 220, height: 70
});
first.submapInitialized = true;
const second = editor.addItem({
id: 2, kind: "submap", label: "Second level", separateMap: true,
mapReference: { id: "second", title: "Second level" },
parentSubmap: first, submapDepth: 1,
x: 400, y: 200, width: 220, height: 70
});
second.submapInitialized = true;
editor.openSubmapMap(first);
editor.openSubmapMap(second);
assert.equal(editor.activeMapRoot, second);
assert.equal(editor.canStepBackWithinMap(), true,
"the host must use local map history before navigating to a source CMap route");
editor.openParentMap();
assert.equal(editor.activeMapRoot, first);
assert.equal(editor.canStepBackWithinMap(), false);
});