115 lines
4.9 KiB
JavaScript
115 lines
4.9 KiB
JavaScript
// Tests for the sidebar section buttons of Syndria98/dragonroll#41: clicking the actors or the items
|
|
// button must not change what the note container shows — an anchored map stays on the screen and the
|
|
// notes that were open stay open — while the notes and maps buttons keep switching the note container
|
|
// between those two, the way they have since #15.
|
|
//
|
|
// The repository root installs no dependencies, so `selectSection` is lifted out of the sidebar's
|
|
// `<script setup>` block and run in a `node:vm` context with its collaborators stubbed, the way
|
|
// tests/map-viewer.test.js loads Content.js. `ActivePane` is modelled as a plain value written by the
|
|
// `SetActivePane` stub, so a test asks the same question the user does: what is the note container
|
|
// showing after the click.
|
|
//
|
|
// Run with `npm test` from the repository root, or `node --test tests/`.
|
|
|
|
const { test } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
|
|
const source = fs.readFileSync(
|
|
path.join(__dirname, "..", "frontend", "app", "components", "partials", "ContentSidebar.vue"),
|
|
"utf8"
|
|
);
|
|
|
|
const script = /<script setup>([\s\S]*?)<\/script>/.exec(source)?.[1];
|
|
|
|
// Top-level functions of the script block: their bodies are indented, so the first `}` in column 0
|
|
// closes the function.
|
|
function functionSource(name) {
|
|
assert.ok(script, "expected the sidebar to have a <script setup> block");
|
|
const fn = new RegExp(`function ${name}\\s*\\([^)]*\\)\\s*\\{[\\s\\S]*?\\n\\}`).exec(script)?.[0];
|
|
assert.ok(fn, `expected a top-level ${name}() function in the sidebar script`);
|
|
return fn;
|
|
}
|
|
|
|
// The sidebar's section buttons with the pane they act on: `pane` is what the note container shows
|
|
// when the button is clicked and `anchoredMap` the map anchored on it.
|
|
function sidebar({ pane = "notes", anchoredMap = null } = {}) {
|
|
const body = [
|
|
functionSource("entryEntityType"),
|
|
functionSource("isEntrySection"),
|
|
functionSource("selectSection"),
|
|
"return selectSection;",
|
|
].join("\n\n");
|
|
|
|
const state = { activeSection: { value: "notes" }, pane, entriesFetched: [], anchoredMap: { value: anchoredMap } };
|
|
|
|
state.selectSection = vm.compileFunction(body, ["activeSection", "fetchCampaignEntries", "AnchoredMap", "SetActivePane"], {
|
|
parsingContext: vm.createContext({}),
|
|
})(
|
|
state.activeSection,
|
|
(section) => state.entriesFetched.push(section),
|
|
state.anchoredMap,
|
|
(value) => {
|
|
state.pane = value;
|
|
}
|
|
);
|
|
|
|
return state;
|
|
}
|
|
|
|
test("the actors button keeps an anchored map on the note container", () => {
|
|
const bar = sidebar({ pane: "map", anchoredMap: { _id: "map-1" } });
|
|
bar.selectSection("actors");
|
|
assert.equal(bar.pane, "map", "the anchored map must not be swapped for the notes");
|
|
assert.equal(bar.anchoredMap.value?._id, "map-1", "the map must stay anchored");
|
|
});
|
|
|
|
test("the items button keeps an anchored map on the note container", () => {
|
|
const bar = sidebar({ pane: "map", anchoredMap: { _id: "map-1" } });
|
|
bar.selectSection("items");
|
|
assert.equal(bar.pane, "map", "the anchored map must not be swapped for the notes");
|
|
assert.equal(bar.anchoredMap.value?._id, "map-1", "the map must stay anchored");
|
|
});
|
|
|
|
test("the actors button keeps the notes that are open showing", () => {
|
|
const bar = sidebar({ pane: "notes", anchoredMap: { _id: "map-1" } });
|
|
bar.selectSection("actors");
|
|
assert.equal(bar.pane, "notes", "the open notes must not be swapped for the anchored map");
|
|
});
|
|
|
|
test("the buttons still select the section they were clicked for", () => {
|
|
for (const section of ["notes", "actors", "items", "maps"]) {
|
|
const bar = sidebar({ pane: "map", anchoredMap: { _id: "map-1" } });
|
|
bar.selectSection(section);
|
|
assert.equal(bar.activeSection.value, section, `${section} must be the selected section`);
|
|
}
|
|
});
|
|
|
|
test("the actor and item buttons still load their own list", () => {
|
|
const bar = sidebar({ pane: "map", anchoredMap: { _id: "map-1" } });
|
|
bar.selectSection("actors");
|
|
bar.selectSection("items");
|
|
assert.deepEqual(bar.entriesFetched, ["actors", "items"]);
|
|
});
|
|
|
|
test("the notes button switches the note container back to the open notes", () => {
|
|
const bar = sidebar({ pane: "map", anchoredMap: { _id: "map-1" } });
|
|
bar.selectSection("notes");
|
|
assert.equal(bar.pane, "notes");
|
|
assert.equal(bar.anchoredMap.value?._id, "map-1", "switching to the notes keeps the map anchored");
|
|
});
|
|
|
|
test("the maps button shows the anchored map again", () => {
|
|
const bar = sidebar({ pane: "notes", anchoredMap: { _id: "map-1" } });
|
|
bar.selectSection("maps");
|
|
assert.equal(bar.pane, "map");
|
|
});
|
|
|
|
test("the maps button has nothing to show without an anchored map", () => {
|
|
const bar = sidebar({ pane: "notes" });
|
|
bar.selectSection("maps");
|
|
assert.equal(bar.pane, "notes");
|
|
});
|