Protect editor sessions from unsaved link navigation and unload
Quality / quality (ubuntu-latest) (push) Failing after 10m2s
Quality / quality (windows-latest) (push) Canceled after 0s

This commit is contained in:
Clintchiz committed 2026-09-05 16:15:01 +05:30
1 parent 88f2e456ef
commit a1704877e8
4 files changed
+70 -1

No files matched your search

@@ -0,0 +1,28 @@
import { expect, test } from "bun:test";
import { Window } from "happy-dom";
import { mountUnsavedChangesGuard } from "../src/unsaved.ts";
test("unsaved guard protects unload and ordinary navigation, permits safe links, and cleans up", () => {
const window = new Window({ url: "https://email.example.com/builder/one" });
const root = window.document.createElement("main");
window.document.body.append(root);
let dirty = true, allow = false, confirmations = 0;
const guard = mountUnsavedChangesGuard(root as unknown as HTMLElement, { hasUnsavedChanges: () => dirty, confirmLeave: () => { confirmations++; return allow; } });
const unload = () => { const event = new window.Event("beforeunload", { cancelable: true }); window.dispatchEvent(event); return event.defaultPrevented; };
const click = (href: string, target = "") => {
const link = window.document.createElement("a"); link.href = href; link.target = target; root.append(link);
const event = new window.MouseEvent("click", { bubbles: true, cancelable: true });
link.dispatchEvent(event); return event.defaultPrevented;
};
expect(unload()).toBe(true);
expect(click("/builder/two")).toBe(true);
expect(click("#content")).toBe(false);
expect(click("/builder/two", "_blank")).toBe(false);
expect(confirmations).toBe(1);
allow = true;
expect(click("/builder/two")).toBe(false);
dirty = false;
expect(unload()).toBe(false);
guard.destroy(); guard.destroy(); dirty = true;
expect(unload()).toBe(false);
});