Fix issue #91: Make character column always visible
All checks were successful
Test / test (push) Successful in 2s
All checks were successful
Test / test (push) Successful in 2s
This commit is contained in:
111
tests/character-sheet-persistent-column.test.js
Normal file
111
tests/character-sheet-persistent-column.test.js
Normal file
@@ -0,0 +1,111 @@
|
||||
// Tests for Syndria98/dragonroll#91: the column that opens the character sheet — the portrait with
|
||||
// the armor class, the speed, the bonuses and both pools under it — stays on screen whoever's tab is
|
||||
// open, instead of being the ability scores tab's own content and disappearing when the sheet moves
|
||||
// to the inventory, the traits or the spells.
|
||||
//
|
||||
// The window is a `.vue` file and the repository root installs no dependencies for the frontend, so
|
||||
// the template and the styles are read out of the source the way tests/character-sheet-tabs.test.js
|
||||
// and tests/character-sheet-portrait-column.test.js read them.
|
||||
//
|
||||
// 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 read = (...parts) => fs.readFileSync(path.join(__dirname, "..", ...parts), "utf8");
|
||||
|
||||
const sheetSource = read("frontend", "app", "components", "windows", "CharacterSheetWindow.vue");
|
||||
const template = /<template>([\s\S]*)<\/template>/.exec(sheetSource)?.[1] ?? "";
|
||||
const TAB_KEYS = ["abilities", "inventory", "traits", "spells"];
|
||||
|
||||
const COLUMN = '<div class="character-column">';
|
||||
|
||||
function tabBody(key) {
|
||||
const block = new RegExp(`<main v-show="activeTab === '${key}'"[\\s\\S]*?</main>`).exec(template)?.[0];
|
||||
assert.ok(block, `expected a body for the ${key} tab`);
|
||||
return block;
|
||||
}
|
||||
|
||||
// The column, taken whole: it stands ahead of the tab bodies, which are its siblings.
|
||||
function characterColumn() {
|
||||
const start = template.indexOf(COLUMN);
|
||||
const end = template.indexOf('<main v-show="activeTab');
|
||||
assert.ok(start !== -1 && end > start, "expected the character column ahead of the tab bodies");
|
||||
return template.slice(start, end);
|
||||
}
|
||||
|
||||
// The classes wrapping a tag, worked out by walking the blocks of the template in order the way a
|
||||
// browser nests them, so a tab cannot hide the column by wrapping it.
|
||||
function ancestorsOf(needle) {
|
||||
const at = template.indexOf(needle);
|
||||
assert.ok(at !== -1, `expected ${needle} in the template`);
|
||||
const stack = [];
|
||||
for (const match of template.matchAll(/<(\/?)(div|main|section|header|nav)\b([^>]*)>/g)) {
|
||||
if (match.index > at) break;
|
||||
const [, closed, , attrs] = match;
|
||||
if (closed) stack.pop();
|
||||
else if (!attrs.trimEnd().endsWith("/")) {
|
||||
const classes = /\bclass="([^"]*)"/.exec(attrs);
|
||||
stack.push(classes ? classes[1].trim().split(/\s+/) : []);
|
||||
}
|
||||
}
|
||||
return stack.slice(0, -1).map((classes) => classes.join(" "));
|
||||
}
|
||||
|
||||
test("the character column belongs to no tab", () => {
|
||||
const column = characterColumn();
|
||||
assert.ok(column.includes('class="sheet-portrait"'), "the portrait is part of the column");
|
||||
assert.ok(column.includes('class="sheet-panel vitals-panel"'), "and so is the panel of readouts and bars");
|
||||
// Nothing toggles the column: no tab decides whether it shows.
|
||||
assert.doesNotMatch(column, /v-show=/, "the column never hides behind a tab");
|
||||
|
||||
for (const key of TAB_KEYS) {
|
||||
const body = tabBody(key);
|
||||
assert.doesNotMatch(body, /class="character-column"/, `the ${key} tab does not own the column`);
|
||||
assert.doesNotMatch(body, /sheet-portrait|vitals-panel/, `the ${key} tab does not hold the column's contents`);
|
||||
}
|
||||
});
|
||||
|
||||
test("the column and the tab that is open stand beside each other", () => {
|
||||
const columnWrappers = ancestorsOf(COLUMN);
|
||||
assert.equal(columnWrappers.at(-1), "sheet-layout", "the column stands in the sheet's own layout");
|
||||
for (const key of TAB_KEYS) {
|
||||
assert.deepEqual(
|
||||
ancestorsOf(`<main v-show="activeTab === '${key}'"`),
|
||||
columnWrappers,
|
||||
`the ${key} tab is a sibling of the column, so it is never wrapped around it`
|
||||
);
|
||||
}
|
||||
|
||||
const layout = /\.sheet-layout\s*\{[^}]*\}/.exec(sheetSource)?.[0] ?? "";
|
||||
assert.match(layout, /display:\s*grid/, "the layout puts the column and the open tab side by side");
|
||||
assert.match(
|
||||
layout,
|
||||
/grid-template-columns:\s*minmax\(200px,\s*34%\)\s+minmax\(0,\s*1fr\)/,
|
||||
"the character keeps its own column and the tab has all the room left"
|
||||
);
|
||||
const columnRule = /\.character-column\s*\{[^}]*\}/.exec(sheetSource)?.[0] ?? "";
|
||||
assert.match(columnRule, /grid-column:\s*1/, "the character stands in the first column");
|
||||
const bodyRule = /\.sheet-tab-body\s*\{[^}]*\}/.exec(sheetSource)?.[0] ?? "";
|
||||
assert.match(bodyRule, /grid-column:\s*2/, "the open tab takes the space beside it");
|
||||
assert.match(bodyRule, /grid-row:\s*1/, "and on the same row, so switching a tab leaves the column where it was");
|
||||
});
|
||||
|
||||
test("a narrow window stacks the column over the tab instead of hiding it", () => {
|
||||
const narrow = /@media \(max-width: 760px\)\s*\{([\s\S]*?)\n\}/.exec(sheetSource)?.[1] ?? "";
|
||||
assert.match(narrow, /\.sheet-layout\s*\{[^}]*grid-template-columns:\s*1fr/, "the two columns become one");
|
||||
assert.match(
|
||||
narrow,
|
||||
/\.character-column,\s*\.sheet-tab-body\s*\{[^}]*grid-column:\s*1 \/ -1;[^}]*grid-row:\s*auto/,
|
||||
"the column goes over the tab, it does not go away with it"
|
||||
);
|
||||
});
|
||||
|
||||
test("what stays on screen is what the sheet is read for", () => {
|
||||
const column = characterColumn();
|
||||
for (const key of ["armor-class", "speed", "proficiency-bonus", "initiative", "hit-points", "hit-dice"]) {
|
||||
assert.ok(column.includes(`campaigns.character-sheet.${key}`), `the ${key} stays visible from any tab`);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user