95 lines
5.0 KiB
JavaScript
95 lines
5.0 KiB
JavaScript
// 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");
|
|
// #64 moved the rail out of the content: it hangs off the window border itself, so it is not
|
|
// swept along when the sheet scrolls (see tests/character-sheet-portrait-column.test.js).
|
|
assert.match(rail, /position:\s*absolute/, "the rail is mounted on the window frame");
|
|
assert.match(rail, /left:\s*100%/, "the rail stands outside the window's right border");
|
|
});
|
|
|
|
// 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-panel abilities-panel"/);
|
|
assert.match(abilities, /class="sheet-panel proficiencies-panel"/, "the proficiencies ride 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}`);
|
|
}
|
|
|
|
// #91 took the character column out of the tabs: it stands beside every one of them, so no tab
|
|
// body holds the portrait (see tests/character-sheet-persistent-column.test.js).
|
|
for (const key of ["abilities", "inventory", "traits", "spells"]) {
|
|
assert.doesNotMatch(tabBody(key), /class="sheet-portrait"/, `the portrait is not the ${key} tab's to hide`);
|
|
}
|
|
|
|
// 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}`);
|
|
}
|
|
}
|
|
});
|