Fix issue #55: Split character sheet in tabs
Some checks failed
Test / test (push) Failing after 2s
Some checks failed
Test / test (push) Failing after 2s
This commit is contained in:
89
tests/character-sheet-tabs.test.js
Normal file
89
tests/character-sheet-tabs.test.js
Normal file
@@ -0,0 +1,89 @@
|
||||
// Tests for Syndria98/dragonroll#55: the character sheet splits its contents across bookmarks down
|
||||
// the right edge (ability scores, inventory, traits, spells) instead of stacking every panel on one
|
||||
// long page.
|
||||
//
|
||||
// The repository root installs no dependencies, so the window wiring is read from the sources the way
|
||||
// tests/item-sheet-window.test.js and tests/settings-window.test.js do it.
|
||||
//
|
||||
// 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 script = /<script setup>([\s\S]*?)<\/script>/.exec(sheetSource)?.[1] ?? "";
|
||||
const template = /<template>([\s\S]*)<\/template>/.exec(sheetSource)?.[1] ?? "";
|
||||
|
||||
// The four bookmarks the issue asks for, in order.
|
||||
test("the sheet declares the four tabs the issue splits the contents into", () => {
|
||||
const array = /const tabs = \[([\s\S]*?)\];/.exec(script)?.[1] ?? "";
|
||||
const keys = [...array.matchAll(/key:\s*['"](\w+)['"]/g)].map((m) => m[1]);
|
||||
assert.deepEqual(keys, ["abilities", "inventory", "traits", "spells"]);
|
||||
for (const key of keys) {
|
||||
assert.ok(
|
||||
new RegExp(`label:\\s*['"]campaigns\\.character-sheet\\.tabs\\.${key}['"]`).test(array),
|
||||
`the ${key} tab must point at its own i18n key`
|
||||
);
|
||||
}
|
||||
assert.match(script, /const activeTab = ref\(['"]abilities['"]\)/, "the sheet opens on the ability scores tab");
|
||||
});
|
||||
|
||||
test("the tabs sit on the right as a vertical bookmark rail", () => {
|
||||
assert.match(template, /<nav class="sheet-tabs"[^>]*role="tablist"[^>]*aria-orientation="vertical"/);
|
||||
assert.match(template, /v-for="tab in tabs"[\s\S]*?:aria-selected="activeTab === tab\.key"/);
|
||||
const rail = /\.sheet-tabs\s*\{[^}]*\}/.exec(sheetSource)?.[0] ?? "";
|
||||
assert.match(rail, /flex-direction:\s*column/, "the bookmarks stack down the right edge");
|
||||
// content column and rail sit side by side, the rail hugging the right
|
||||
assert.match(sheetSource, /\.sheet-layout\s*\{[^}]*display:\s*flex[^}]*\}/, "content and rail share a row");
|
||||
assert.match(sheetSource, /\.sheet-content\s*\{[^}]*flex:\s*1[^}]*\}/, "the content takes the room, the rail stays slim");
|
||||
});
|
||||
|
||||
// Each tab shows only its own panels; the rest are hidden with v-show so an in-progress edit is
|
||||
// not thrown away when hopping between tabs.
|
||||
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;
|
||||
}
|
||||
|
||||
test("every tab shows only its own content and hides the others", () => {
|
||||
const abilities = tabBody("abilities");
|
||||
assert.match(abilities, /class="sheet-portrait"/, "the portrait stays with ability scores");
|
||||
assert.match(abilities, /class="sheet-panel abilities-panel"/);
|
||||
assert.match(abilities, /class="sheet-panel combat-panel"/, "combat rides with the ability scores");
|
||||
for (const other of ["inventory-panel", "traits-panel", "spells-panel"]) {
|
||||
assert.doesNotMatch(abilities, new RegExp(other));
|
||||
}
|
||||
|
||||
for (const [key, panel] of [["inventory", "inventory-panel"], ["traits", "traits-panel"], ["spells", "spells-panel"]]) {
|
||||
const body = tabBody(key);
|
||||
assert.ok(body.includes(panel), `the ${key} tab shows the ${panel}`);
|
||||
assert.doesNotMatch(body, /class="sheet-portrait"/);
|
||||
}
|
||||
|
||||
// the panels are toggled, not unmounted, so editing on one tab and flipping away keeps the draft
|
||||
assert.match(template, /v-show="activeTab === 'inventory'"/);
|
||||
assert.match(template, /v-show="activeTab === 'traits'"/);
|
||||
assert.match(template, /v-show="activeTab === 'spells'"/);
|
||||
});
|
||||
|
||||
test("the tabs still edit and save through the existing sheet form", () => {
|
||||
assert.match(tabBody("inventory"), /@click="addInventoryItem"/);
|
||||
assert.match(tabBody("inventory"), /removeInventoryItem\(index\)/);
|
||||
assert.match(tabBody("traits"), /v-model="form\.traits"/);
|
||||
assert.match(tabBody("spells"), /v-model="form\.spells"/);
|
||||
assert.match(tabBody("abilities"), /v-model\.number="form\.abilityScores\[stat\.key\]"/);
|
||||
});
|
||||
|
||||
test("every locale names the four tabs", () => {
|
||||
for (const locale of ["en", "es", "ca"]) {
|
||||
const sheet = JSON.parse(read("frontend", "i18n", "locales", locale, "campaigns.json"))["character-sheet"];
|
||||
for (const key of ["abilities", "inventory", "traits", "spells"]) {
|
||||
assert.ok(sheet.tabs?.[key], `${locale} is missing campaigns.character-sheet.tabs.${key}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user