Files
yaak-mountain-loop/apps/yaak-proxy/lib/sidebarCollapse.test.ts
T
Gregory SchierandClaude Opus 5.5 96ab0a8a60 Give empty sidebar nodes a collapsed flag too
The shared tree treats every node with a children list as a folder and
collapses an empty one before the left arrow moves to its parent. Left
out of the collapsed map, a leaf path never read as collapsed, so the
left arrow kept trying to collapse it and never moved up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 10:44:22 -07:00

76 lines
3.0 KiB
TypeScript

import type { TreeNode } from "@yaakapp-internal/ui";
import { describe, expect, test } from "vite-plus/test";
import { applyCollapsed, collapsedMap, folderIds } from "./sidebarCollapse";
/** A node with a children list, empty or not, as the sidebar builds every node */
function node(id: string, children: TreeNode<{ id: string }>[] = []): TreeNode<{ id: string }> {
return { item: { id }, parent: null, depth: 0, children };
}
const tree = node("root", [
node("domains", [
node("domain:api.example.com", [
node("path:api.example.com/v1", [node("path:api.example.com/v1/users")]),
]),
node("domain:cdn.example.com", [node("path:cdn.example.com/app.js")]),
node("domain:empty.example.com"),
]),
]);
describe("collapsedMap", () => {
test("collapses every domain and path, and leaves the root and Domains open", () => {
const map = collapsedMap(folderIds(tree), {}, {});
// Empty ones too: the tree collapses one before the left arrow moves to its parent.
expect(map).toEqual({
"domain:api.example.com": true,
"path:api.example.com/v1": true,
"path:api.example.com/v1/users": true,
"domain:cdn.example.com": true,
"path:cdn.example.com/app.js": true,
"domain:empty.example.com": true,
});
expect(map.root).toBeUndefined();
expect(map.domains).toBeUndefined();
});
test("opens what the user expanded, and a host seen later starts collapsed", () => {
const expanded = { "domain:api.example.com": true };
expect(collapsedMap(folderIds(tree), expanded, {})).toMatchObject({
"domain:api.example.com": false,
"path:api.example.com/v1": true,
"domain:cdn.example.com": true,
});
const later = node("root", [
node("domains", [node("domain:new.example.com", [node("path:new.example.com/x")])]),
]);
expect(collapsedMap(folderIds(later), expanded, {})["domain:new.example.com"]).toBe(true);
});
});
describe("applyCollapsed", () => {
test("keeps an expanded domain, and forgets it when collapsed again", () => {
const prev = collapsedMap(folderIds(tree), {}, {});
const opened = applyCollapsed(prev, { ...prev, "domain:api.example.com": false }, {}, {});
expect(opened.expanded).toEqual({ "domain:api.example.com": true });
// Persisted, so the next launch builds the map from it.
const relaunched = collapsedMap(folderIds(tree), opened.expanded, {});
expect(relaunched["domain:api.example.com"]).toBe(false);
const closed = applyCollapsed(
relaunched,
{ ...relaunched, "domain:api.example.com": true },
opened.expanded,
{},
);
expect(closed.expanded).toEqual({});
});
test("keeps the other nodes' flags apart from the persisted ones", () => {
const prev = collapsedMap(folderIds(tree), {}, {});
const next = applyCollapsed(prev, { ...prev, domains: true }, {}, {});
expect(next).toEqual({ expanded: {}, others: { domains: true } });
expect(collapsedMap(folderIds(tree), next.expanded, next.others).domains).toBe(true);
});
});