fix(sidebar): restore node order after unpinning

This commit is contained in:
t8y2 committed 2026-07-14 18:39:44 +08:00
1 parent ee85033068
commit 39aa506e44
4 files changed
+163 -26

No files matched your search

+99 -1
View File
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { migrateLegacyPinnedTreeNodeIds, syncPinnedTreeNodeStateInPlace, treeNodePinKey, updatePinnedTreeNodeInPlace } from "@/lib/app/pinnedItems";
import { reactive } from "vue";
import { inheritNaturalTreeNodeOrder, migrateLegacyPinnedTreeNodeIds, syncPinnedTreeNodeStateInPlace, treeNodePinKey, updatePinnedTreeNodeInPlace } from "@/lib/app/pinnedItems";
import { buildTreeNodesFromLayout } from "@/lib/sidebar/sidebarLayout";
import type { ConnectionConfig, SidebarLayout, TreeNode } from "@/types/database";
@@ -23,6 +24,39 @@ describe("sidebar pinned tree nodes", () => {
expect(tree[0].children?.[0].pinned).toBe(true);
});
it("restores the original sibling order after unpinning", () => {
const children: TreeNode[] = [
{ id: "conn:db:a", label: "A", type: "database" },
{ id: "conn:db:b", label: "B", type: "database" },
{ id: "conn:db:c", label: "C", type: "database" },
];
const tree: TreeNode[] = [{ id: "conn", label: "Connection", type: "connection", children }];
updatePinnedTreeNodeInPlace(tree, children[1], true);
expect(tree[0].children?.map((node) => node.id)).toEqual(["conn:db:b", "conn:db:a", "conn:db:c"]);
updatePinnedTreeNodeInPlace(tree, children[1], false);
expect(tree[0].children?.map((node) => node.id)).toEqual(["conn:db:a", "conn:db:b", "conn:db:c"]);
});
it("restores unpinned nodes while keeping other pinned nodes first", () => {
const children: TreeNode[] = [
{ id: "conn:db:a", label: "A", type: "database" },
{ id: "conn:db:b", label: "B", type: "database" },
{ id: "conn:db:c", label: "C", type: "database" },
];
const tree: TreeNode[] = [{ id: "conn", label: "Connection", type: "connection", children }];
updatePinnedTreeNodeInPlace(tree, children[1], true);
updatePinnedTreeNodeInPlace(tree, children[2], true);
updatePinnedTreeNodeInPlace(tree, children[1], false);
expect(tree[0].children?.map((node) => node.id)).toEqual(["conn:db:c", "conn:db:a", "conn:db:b"]);
updatePinnedTreeNodeInPlace(tree, children[2], false);
expect(tree[0].children?.map((node) => node.id)).toEqual(["conn:db:a", "conn:db:b", "conn:db:c"]);
});
it("reorders pinned root nodes in place", () => {
const tree: TreeNode[] = [
{ id: "group-a", label: "A", type: "connection-group" },
@@ -35,6 +69,48 @@ describe("sidebar pinned tree nodes", () => {
expect(tree[0].pinned).toBe(true);
});
it("restores the original root order after unpinning", () => {
const tree: TreeNode[] = [
{ id: "group-a", label: "A", type: "connection-group" },
{ id: "group-b", label: "B", type: "connection-group" },
{ id: "group-c", label: "C", type: "connection-group" },
];
const groupB = tree[1];
updatePinnedTreeNodeInPlace(tree, groupB, true);
updatePinnedTreeNodeInPlace(tree, groupB, false);
expect(tree.map((node) => node.id)).toEqual(["group-a", "group-b", "group-c"]);
});
it("restores order for Vue reactive tree nodes", () => {
const tree = reactive<TreeNode[]>([
{ id: "group-a", label: "A", type: "connection-group" },
{ id: "group-b", label: "B", type: "connection-group" },
{ id: "group-c", label: "C", type: "connection-group" },
]);
const groupB = tree[1];
updatePinnedTreeNodeInPlace(tree, groupB, true);
updatePinnedTreeNodeInPlace(tree, groupB, false);
expect(tree.map((node) => node.id)).toEqual(["group-a", "group-b", "group-c"]);
});
it("restores hidden children and keeps appended children in load order", () => {
const tableA: TreeNode = { id: "table-a", label: "A", type: "table", connectionId: "conn", database: "db" };
const tableB: TreeNode = { id: "table-b", label: "B", type: "table", connectionId: "conn", database: "db" };
const tableC: TreeNode = { id: "table-c", label: "C", type: "table", connectionId: "conn", database: "db" };
const database: TreeNode = { id: "db", label: "Database", type: "database", hiddenChildren: [tableA, tableB] };
const tree = [database];
syncPinnedTreeNodeStateInPlace(tree, new Set([treeNodePinKey(tableB)]));
database.hiddenChildren!.push(tableC);
syncPinnedTreeNodeStateInPlace(tree, new Set());
expect(database.hiddenChildren?.map((node) => node.id)).toEqual(["table-a", "table-b", "table-c"]);
});
it("scopes duplicate node ids by database when pinning", () => {
const databaseA: TreeNode = {
id: "conn:a",
@@ -97,5 +173,27 @@ describe("sidebar pinned tree nodes", () => {
expect(nodes.map((node) => node.id)).toEqual(["group-b", "group-a"]);
expect(nodes[0].pinned).toBe(true);
syncPinnedTreeNodeStateInPlace(nodes, new Set());
expect(nodes.map((node) => node.id)).toEqual(["group-a", "group-b"]);
});
it("retains the latest layout order through rebuilt node clones", () => {
const layout: SidebarLayout = {
groups: [
{ id: "group-a", name: "A", collapsed: false },
{ id: "group-b", name: "B", collapsed: false },
],
order: [
{ type: "group", id: "group-a", children: [] },
{ type: "group", id: "group-b", children: [] },
],
};
const built = buildTreeNodesFromLayout(layout, [], new Set(["group-b"]));
const rebuilt = built.map((node) => inheritNaturalTreeNodeOrder(node, { ...node }));
syncPinnedTreeNodeStateInPlace(rebuilt, new Set());
expect(rebuilt.map((node) => node.id)).toEqual(["group-a", "group-b"]);
});
});