fix(theme): nav-state.js tracks wired sections in a WeakSet so enhanced navigation cannot stack toggle listeners; bump 0.5.2 (scadaproj#48)
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ZB.MOM.WW.Theme.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Theme-008: nav-state.js must not stack toggle listeners when Blazor's enhanced-navigation DOM
|
||||
/// sync strips its marker attribute. Runs <c>nav-state.test.js</c> under Node (a DOM stub, no
|
||||
/// jsdom); silently passes only when Node is not installed.
|
||||
/// </summary>
|
||||
public class NavStateScriptTests
|
||||
{
|
||||
private static string Here([CallerFilePath] string path = "") => Path.GetDirectoryName(path)!;
|
||||
|
||||
[Fact]
|
||||
public void NavState_does_not_stack_listeners_or_persist_the_transient_reveal()
|
||||
{
|
||||
var script = Path.GetFullPath(Path.Combine(Here(), "..", "..", "src", "ZB.MOM.WW.Theme", "wwwroot", "js", "nav-state.js"));
|
||||
var test = Path.Combine(Here(), "nav-state.test.js");
|
||||
|
||||
Process? node;
|
||||
try
|
||||
{
|
||||
node = Process.Start(new ProcessStartInfo("node", $"\"{test}\" \"{script}\"")
|
||||
{
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
});
|
||||
}
|
||||
catch (System.ComponentModel.Win32Exception)
|
||||
{
|
||||
return; // Node not available on this machine.
|
||||
}
|
||||
|
||||
using (node!)
|
||||
{
|
||||
var output = node.StandardOutput.ReadToEnd() + node.StandardError.ReadToEnd();
|
||||
node.WaitForExit();
|
||||
Assert.True(node.ExitCode == 0, output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// Behavioural test for nav-state.js (Theme-008), run by NavStateScriptTests under Node with a
|
||||
// minimal DOM stub - no jsdom dependency. Exit code 0 = pass; failures print and exit 1.
|
||||
"use strict";
|
||||
const fs = require("fs");
|
||||
const vm = require("vm");
|
||||
|
||||
function makeEl(attrs) {
|
||||
const listeners = [];
|
||||
const a = Object.assign({}, attrs);
|
||||
let open = false;
|
||||
const el = {
|
||||
classList: { contains: () => true },
|
||||
getAttribute: (n) => (n in a ? a[n] : null),
|
||||
setAttribute: (n, v) => { a[n] = String(v); },
|
||||
removeAttribute: (n) => { delete a[n]; },
|
||||
hasAttribute: (n) => n in a,
|
||||
addEventListener: (t, f) => { if (t === "toggle") listeners.push(f); },
|
||||
querySelector: () => null,
|
||||
closest: () => el,
|
||||
get open() { return open; },
|
||||
set open(v) { if (v !== open) { open = v; listeners.slice().forEach((f) => f()); } },
|
||||
listenerCount: () => listeners.length,
|
||||
attrs: a,
|
||||
};
|
||||
return el;
|
||||
}
|
||||
|
||||
function run() {
|
||||
const section = makeEl({ "data-nav-key": "admin" });
|
||||
const link = { closest: () => section, active: false };
|
||||
const store = { "zbnav:admin": "0" };
|
||||
const writes = [];
|
||||
let enhancedLoad = null;
|
||||
|
||||
const document = {
|
||||
readyState: "complete",
|
||||
documentElement: {},
|
||||
addEventListener() {},
|
||||
querySelectorAll(sel) {
|
||||
if (sel === "details.rail-section[data-nav-key]") return [section];
|
||||
if (sel === "details.rail-section a.rail-link.active") return link.active ? [link] : [];
|
||||
return [];
|
||||
},
|
||||
};
|
||||
const window = {
|
||||
document,
|
||||
localStorage: {
|
||||
getItem: (k) => (k in store ? store[k] : null),
|
||||
setItem: (k, v) => { store[k] = v; writes.push([k, v]); },
|
||||
},
|
||||
Blazor: { addEventListener: (t, f) => { if (t === "enhancedload") enhancedLoad = f; } },
|
||||
};
|
||||
const ctx = { window, document, WeakSet, console };
|
||||
vm.createContext(ctx);
|
||||
vm.runInContext(fs.readFileSync(process.argv[2], "utf8"), ctx);
|
||||
|
||||
const fail = (m) => { console.error("FAIL: " + m); process.exit(1); };
|
||||
if (section.open) fail("saved '0' should leave the section collapsed");
|
||||
|
||||
// Three enhanced navigations; DOM sync strips attributes the server did not render.
|
||||
for (let i = 0; i < 3; i++) {
|
||||
Object.keys(section.attrs).forEach((k) => { if (k !== "data-nav-key") delete section.attrs[k]; });
|
||||
enhancedLoad();
|
||||
}
|
||||
if (section.listenerCount() !== 1) fail("expected 1 toggle listener after re-apply, got " + section.listenerCount());
|
||||
|
||||
// The user expands, then collapses: exactly one write each.
|
||||
section.open = true;
|
||||
section.open = false;
|
||||
if (writes.length !== 2) fail("expected 2 writes for expand+collapse, got " + writes.length);
|
||||
store["zbnav:admin"] = "0";
|
||||
writes.length = 0;
|
||||
|
||||
// Active-link reveal is transient and must not overwrite the saved preference.
|
||||
link.active = true;
|
||||
Object.keys(section.attrs).forEach((k) => { if (k !== "data-nav-key") delete section.attrs[k]; });
|
||||
enhancedLoad();
|
||||
if (!section.open) fail("active link should reveal its section");
|
||||
if (writes.length !== 0) fail("transient reveal persisted " + JSON.stringify(writes));
|
||||
if (store["zbnav:admin"] !== "0") fail("saved preference was overwritten");
|
||||
console.log("ok");
|
||||
}
|
||||
run();
|
||||
Reference in New Issue
Block a user