88 lines
4.1 KiB
JavaScript
88 lines
4.1 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const test = require("node:test");
|
|
const vm = require("node:vm");
|
|
|
|
const source = fs.readFileSync(path.join(__dirname, "../static/app.js"), "utf8");
|
|
|
|
test("category columns sort excluded choices as lowest and pending submissions last", () => {
|
|
const context = vm.createContext({
|
|
document: { querySelector: () => ({}) },
|
|
fetch: () => new Promise(() => {}),
|
|
});
|
|
vm.runInContext(source, context);
|
|
context.people = [
|
|
{ userId: 1, loginUsername: "Third", preferences: { class: { ranking: ["Mage", "Warrior", "Paladin"] } } },
|
|
{ userId: 2, loginUsername: "First", preferences: { class: { ranking: ["Paladin", "Mage", "Warrior"] } } },
|
|
{ userId: 3, loginUsername: "Pending", preferences: {} },
|
|
{ userId: 4, loginUsername: "Second", preferences: { class: { ranking: ["Mage", "Paladin", "Warrior"] } } },
|
|
{ userId: 5, loginUsername: "Excluded", preferences: { class: { ranking: ["Mage", "Warrior"] } } },
|
|
];
|
|
const names = vm.runInContext(
|
|
"state.sort = { key: 'category:Paladin', direction: 1 }; sortPeople(people, 'class').map(person => person.loginUsername)",
|
|
context,
|
|
);
|
|
assert.deepEqual(Array.from(names), ["First", "Second", "Third", "Excluded", "Pending"]);
|
|
const reversed = vm.runInContext(
|
|
"state.sort = { key: 'category:Paladin', direction: -1 }; sortPeople(people, 'class').map(person => person.loginUsername)",
|
|
context,
|
|
);
|
|
assert.deepEqual(Array.from(reversed), ["Excluded", "Third", "Second", "First", "Pending"]);
|
|
const rankColumn = vm.runInContext(
|
|
"state.sort = { key: 'rank2', direction: 1 }; sortPeople(people, 'class').map(person => person.loginUsername)",
|
|
context,
|
|
);
|
|
assert.deepEqual(Array.from(rankColumn), ["Third", "First", "Second", "Excluded", "Pending"]);
|
|
assert.equal(vm.runInContext("ordinal(1)", context), "1st");
|
|
assert.equal(vm.runInContext("ordinal(12)", context), "12th");
|
|
});
|
|
|
|
test("group tables leave excluded categories and empty rank positions blank", () => {
|
|
const app = {
|
|
innerHTML: "",
|
|
querySelector: () => ({ addEventListener() {} }),
|
|
querySelectorAll: () => [],
|
|
};
|
|
const context = vm.createContext({
|
|
document: { querySelector: () => app },
|
|
fetch: () => new Promise(() => {}),
|
|
});
|
|
vm.runInContext(source, context);
|
|
vm.runInContext(`
|
|
state.data = {
|
|
user: { userId: 1, loginUsername: "Aly" },
|
|
properties: [{ id: "class", name: "Class", categories: ["Paladin", "Mage", "Warrior"] }],
|
|
preferences: { class: { ranking: ["Mage"] } },
|
|
people: [{ userId: 1, loginUsername: "Aly", preferences: { class: { ranking: ["Mage"], updatedAt: "2026-10-01T00:00:00Z" } } }],
|
|
};
|
|
state.groupMode = "category";
|
|
renderGroup();
|
|
`, context);
|
|
assert.equal((app.innerHTML.match(/aria-label="No preference"><\/td>/g) || []).length, 2);
|
|
assert.doesNotMatch(app.innerHTML, />0th</);
|
|
assert.match(app.innerHTML, /class="person-name">Aly<small>YOU<\/small>/);
|
|
assert.doesNotMatch(app.innerHTML, /person-login|undefined/);
|
|
vm.runInContext('state.groupMode = "rank"; renderGroup();', context);
|
|
assert.equal((app.innerHTML.match(/aria-label="No ranked choice"><\/td>/g) || []).length, 2);
|
|
});
|
|
|
|
test("content columns sort Yes, Maybe, No and leave unanswered people last", () => {
|
|
const context = vm.createContext({
|
|
document: { querySelector: () => ({}) },
|
|
fetch: () => new Promise(() => {}),
|
|
});
|
|
vm.runInContext(source, context);
|
|
context.people = [
|
|
{ userId: 1, loginUsername: "Noah", preferences: { content: { choices: { "Group Questing": "No" } } } },
|
|
{ userId: 2, loginUsername: "Yara", preferences: { content: { choices: { "Group Questing": "Yes" } } } },
|
|
{ userId: 3, loginUsername: "Missing", preferences: {} },
|
|
{ userId: 4, loginUsername: "Mira", preferences: { content: { choices: { "Group Questing": "Maybe" } } } },
|
|
];
|
|
const names = vm.runInContext(
|
|
"state.sort = { key: 'category:Group Questing', direction: 1 }; sortPeople(people, 'content').map(person => person.loginUsername)",
|
|
context,
|
|
);
|
|
assert.deepEqual(Array.from(names), ["Yara", "Mira", "Noah", "Missing"]);
|
|
});
|