Files
make-space/app/js/ui.test.mjs
T

307 lines
10 KiB
JavaScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import vm from "node:vm";
const source = readFileSync(new URL("./ui.js", import.meta.url), "utf8");
// The fake environment models asynchronous close/popstate delivery deliberately:
// it catches lifetime mistakes hidden by a synchronous history.back() stub.
function setup() {
const tasks = [];
const microtasks = [];
const frames = [];
const elements = new Map();
const scrollCalls = [];
const document = {
activeElement: null,
documentElement: { style: { overflow: "auto" } },
body: { style: { overflow: "" } },
getElementById: id => elements.get(id),
querySelector: () => null,
};
class Element extends EventTarget {
isConnected = true;
focusCount = 0;
focus() { this.focusCount += 1; document.activeElement = this; }
blur() { if (document.activeElement === this) document.activeElement = null; }
}
class Dialog extends Element {
constructor(id, { inline = false } = {}) {
super();
this.id = id;
this.open = inline;
this.modal = false;
this.attributes = new Map(inline ? [["data-inline-dialog", "true"]] : []);
this.firstButton = new Element();
elements.set(id, this);
}
hasAttribute(name) { return this.attributes.has(name); }
setAttribute(name, value) { this.attributes.set(name, value); }
removeAttribute(name) { this.attributes.delete(name); }
querySelector() { return this.firstButton; }
show() { this.open = true; this.modal = false; }
showModal() {
if (this.open) throw new Error("A nonmodal dialog must be closed first");
this.open = true;
this.modal = true;
}
close() {
if (!this.open) return;
this.open = false;
this.modal = false;
tasks.push(() => this.dispatchEvent(new Event("close")));
}
getBoundingClientRect() { return { left: 10, top: 10, right: 300, bottom: 500 }; }
}
const window = new EventTarget();
window.scrollY = 340;
window.scrollTo = options => {
window.scrollY = options.top;
scrollCalls.push(options.top);
};
const location = new URL("http://localhost/place/kakao:123?from=%2Frecent");
const entries = [{ router: "base" }];
let historyIndex = 0;
const history = {
backCalls: 0,
get state() { return entries[historyIndex]; },
pushState(state) {
entries.splice(historyIndex + 1);
entries.push(state);
historyIndex += 1;
},
replaceState(state) { entries[historyIndex] = state; },
back() {
this.backCalls += 1;
tasks.push(() => {
if (historyIndex === 0) return;
historyIndex -= 1;
window.dispatchEvent(new Event("popstate"));
});
},
};
const context = {
window, document, location, history, URL, Event,
HTMLElement: Element, HTMLDialogElement: Dialog,
navigator: {},
queueMicrotask: callback => microtasks.push(callback),
requestAnimationFrame: callback => frames.push(callback),
matchMedia: () => Object.assign(new EventTarget(), { matches: false }),
};
const exports = ["saveScroll", "restoreScroll", "openDialog", "closeDialog", "releaseDialog"];
const api = vm.runInNewContext(
`${source.replace(/^export /gm, "")}\n({ ${exports.join(", ")} })`,
context,
{ filename: "ui.js" },
);
function flush() {
while (tasks.length || microtasks.length) {
while (microtasks.length) microtasks.shift()();
if (tasks.length) tasks.shift()();
}
}
function flushFrames() {
while (frames.length) frames.shift()();
}
function nextFrame() { frames.shift()?.(); }
return { api, Dialog, Element, document, window, location, history, entries, scrollCalls, flush, flushFrames, nextFrame };
}
test("nested modal close consumes only the inner history entry and retains the outer scroll lock", () => {
const env = setup();
const { api, document, history, flush } = env;
const opener = new env.Element();
opener.focus();
const scene = new env.Dialog("scene", { inline: true });
const photo = new env.Dialog("photo");
let sceneClosed = 0;
let photoClosed = 0;
api.openDialog("scene", () => sceneClosed++);
const innerOpener = new env.Element();
innerOpener.focus();
api.openDialog("photo", () => photoClosed++);
api.closeDialog("photo");
assert.equal(photo.open, true, "close is pending its history traversal");
flush();
assert.equal(photo.open, false);
assert.equal(photoClosed, 1);
assert.equal(sceneClosed, 0);
assert.equal(scene.open, true);
assert.equal(scene.modal, true);
assert.equal(document.body.style.overflow, "hidden");
assert.equal(document.activeElement, innerOpener);
assert.equal(history.state.spaceOverlay, env.entries[1].spaceOverlay);
api.closeDialog("scene");
flush();
assert.equal(sceneClosed, 1);
assert.equal(scene.open, true, "the inline canvas remains rendered");
assert.equal(scene.modal, false);
assert.equal(document.body.style.overflow, "");
assert.equal(document.documentElement.style.overflow, "auto");
assert.equal(document.activeElement, opener);
assert.equal(history.backCalls, 2);
});
test("the queued close event from promoting an inline dialog does not dismiss its modal", () => {
const env = setup();
const dialog = new env.Dialog("scene", { inline: true });
let closed = 0;
env.api.openDialog("scene", () => closed++);
env.flush();
assert.equal(dialog.modal, true);
assert.equal(dialog.open, true);
assert.equal(closed, 0);
assert.equal(env.history.backCalls, 0);
const cancel = new Event("cancel", { cancelable: true });
dialog.dispatchEvent(cancel);
assert.equal(cancel.defaultPrevented, true);
env.flush();
assert.equal(dialog.open, true);
assert.equal(dialog.modal, false);
assert.equal(closed, 1);
});
test("reopening an already open dialog replaces the callback without adding history", () => {
const env = setup();
new env.Dialog("photo");
let oldCalls = 0;
let newCalls = 0;
const first = env.api.openDialog("photo", () => oldCalls++);
const second = env.api.openDialog("photo", () => newCalls++);
assert.equal(second, first);
assert.equal(env.entries.length, 2);
env.api.closeDialog("photo");
env.flush();
assert.equal(oldCalls, 0, "the old Rust Closure can safely be dropped");
assert.equal(newCalls, 1);
});
test("component release restores inline content without navigating history or notifying disposed state", () => {
const env = setup();
const scene = new env.Dialog("scene", { inline: true });
let closed = 0;
const handle = env.api.openDialog("scene", () => closed++);
// The URL can still be the old route while a router disposes its component.
env.api.releaseDialog(handle);
env.flush();
assert.equal(env.history.backCalls, 0);
assert.equal(scene.open, true);
assert.equal(scene.modal, false);
assert.equal(env.document.body.style.overflow, "");
assert.equal(closed, 0);
});
test("close followed by immediate release cannot call a dropped callback after popstate", () => {
const env = setup();
const photo = new env.Dialog("photo");
const handle = env.api.openDialog("photo", () => assert.fail("disposed callback invoked"));
env.api.closeDialog("photo");
env.api.releaseDialog(handle);
env.flush();
assert.equal(env.history.backCalls, 1);
assert.equal(photo.open, false);
assert.equal(env.document.body.style.overflow, "");
});
test("browser back closes nested dialogs from top to bottom", () => {
const env = setup();
const scene = new env.Dialog("scene", { inline: true });
const photo = new env.Dialog("photo");
const notifications = [];
env.api.openDialog("scene", () => notifications.push("scene"));
env.api.openDialog("photo", () => notifications.push("photo"));
env.history.back();
env.flush();
assert.deepEqual(notifications, ["photo"]);
assert.equal(photo.open, false);
assert.equal(scene.modal, true);
env.history.back();
env.flush();
assert.deepEqual(notifications, ["photo", "scene"]);
assert.equal(scene.open, true);
assert.equal(scene.modal, false);
});
test("native dialog close still consumes overlay history once", () => {
const env = setup();
const photo = new env.Dialog("photo");
let closed = 0;
env.api.openDialog("photo", () => closed++);
photo.close();
env.flush();
assert.equal(env.history.backCalls, 1);
assert.equal(closed, 1);
assert.equal(env.document.body.style.overflow, "");
});
test("a deferred search scroll restore cannot affect a different query on the same route", () => {
const env = setup();
env.location.pathname = "/";
env.location.search = "?q=first";
env.window.scrollY = 620;
env.api.saveScroll("/");
env.window.scrollY = 0;
env.api.restoreScroll("/?q=first");
env.location.search = "?q=second";
env.flushFrames();
assert.deepEqual(env.scrollCalls, []);
env.location.search = "?q=first";
env.api.restoreScroll("/?q=first");
env.flushFrames();
assert.deepEqual(env.scrollCalls, [620]);
});
test("search restoration waits for the router's destination URL commit", () => {
const env = setup();
env.location.pathname = "/";
env.location.search = "?q=first";
env.window.scrollY = 89;
env.api.saveScroll("/");
env.location.pathname = "/recent";
env.location.search = "";
env.window.scrollY = 0;
// The new Home component exists while browser location still says /recent.
env.api.restoreScroll("/?q=first");
env.nextFrame();
assert.deepEqual(env.scrollCalls, []);
env.location.pathname = "/";
env.location.search = "?q=first";
env.flushFrames();
assert.deepEqual(env.scrollCalls, [89]);
});
test("a newer navigation cancels a pending restoration before its URL commits", () => {
const env = setup();
env.window.scrollY = 89;
env.api.saveScroll("/");
env.location.pathname = "/recent";
env.location.search = "";
env.api.restoreScroll("/?q=first");
env.nextFrame();
env.api.saveScroll("/recent");
env.location.pathname = "/";
env.location.search = "?q=first";
env.flushFrames();
assert.deepEqual(env.scrollCalls, []);
});
test("releasing a dialog after route change does not restore focus or scroll into the old page", () => {
const env = setup();
const opener = new env.Element();
opener.focus();
new env.Dialog("photo");
const handle = env.api.openDialog("photo", () => assert.fail("disposed callback invoked"));
env.location.pathname = "/recent";
env.location.search = "";
env.api.releaseDialog(handle);
env.flush();
assert.equal(opener.focusCount, 1);
assert.deepEqual(env.scrollCalls, []);
assert.equal(env.history.backCalls, 0);
});