feat: sort and paginate usage history
This commit is contained in:
@@ -34,7 +34,7 @@ function placeholderNames(value) {
|
||||
test("all three locale dictionaries have identical keys", () => {
|
||||
assert.deepEqual(locales, ["en", "zh", "th"]);
|
||||
const expected = Object.keys(dictionaries.en).sort();
|
||||
assert.equal(expected.length, 215);
|
||||
assert.equal(expected.length, 217);
|
||||
for (const locale of locales) {
|
||||
assert.deepEqual(Object.keys(dictionaries[locale]).sort(), expected, `${locale} key parity`);
|
||||
}
|
||||
|
||||
41
tests/usage-history.test.mjs
Normal file
41
tests/usage-history.test.mjs
Normal file
@@ -0,0 +1,41 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import test from "node:test";
|
||||
import vm from "node:vm";
|
||||
|
||||
const appSource = await readFile(new URL("../app.js", import.meta.url), "utf8");
|
||||
const helperStart = appSource.indexOf("function dateOnlyMs");
|
||||
const helperEnd = appSource.indexOf("\n\nconst I18N = ", helperStart);
|
||||
assert.notEqual(helperStart, -1, "usage date helpers should exist");
|
||||
assert.notEqual(helperEnd, -1, "usage date helpers should end before translations");
|
||||
|
||||
const helperContext = {};
|
||||
vm.runInNewContext(`${appSource.slice(helperStart, helperEnd)}\nglobalThis.usageRecordsInDisplayOrder = usageRecordsInDisplayOrder;`, helperContext);
|
||||
|
||||
function localIsoOffset(offsetDays) {
|
||||
const date = new Date();
|
||||
date.setHours(12, 0, 0, 0);
|
||||
date.setDate(date.getDate() + offsetDays);
|
||||
return [date.getFullYear(), String(date.getMonth() + 1).padStart(2, "0"), String(date.getDate()).padStart(2, "0")].join("-");
|
||||
}
|
||||
|
||||
test("usage history orders records by distance from today's stay interval", () => {
|
||||
const records = [
|
||||
{ id: "missing", checkin: "not-a-date", checkout: "not-a-date" },
|
||||
{ id: "future-far", checkin: localIsoOffset(10), checkout: localIsoOffset(12) },
|
||||
{ id: "past-near", checkin: localIsoOffset(-5), checkout: localIsoOffset(-2) },
|
||||
{ id: "active", checkin: localIsoOffset(-1), checkout: localIsoOffset(1) },
|
||||
{ id: "future-near", checkin: localIsoOffset(1), checkout: localIsoOffset(3) }
|
||||
];
|
||||
|
||||
assert.deepEqual(
|
||||
Array.from(helperContext.usageRecordsInDisplayOrder(records), record => record.id),
|
||||
["active", "future-near", "past-near", "future-far", "missing"]
|
||||
);
|
||||
});
|
||||
|
||||
test("usage history keeps the requested page size and direct pagination surface", () => {
|
||||
assert.match(appSource, /const USAGE_PAGE_SIZE = 30;/);
|
||||
assert.match(appSource, /#stayPageNumbers/);
|
||||
assert.match(appSource, /data-usage-page/);
|
||||
});
|
||||
Reference in New Issue
Block a user