Files
dbx/packages/app-tests/sidebarTreeAffordances.test.ts
T
fa6540b739 fix(sidebar): 修复搜索筛选树分组折叠在 scope-only 过滤下空操作
* 🐛 fix(sidebar): 修复搜索筛选树的分组折叠与重复点击切换

- 在保留搜索子树时应用临时折叠状态,避免空分组重新展开
- 仅在搜索筛选期间记录节点折叠操作
- 允许分组标签双击后的连续点击正常切换展开状态
- 补充侧边栏搜索及节点点击行为测试

* fix(sidebar): collapseAllTreeNodes 按 scope-only 过滤填充 searchCollapsedIds

---------

Co-authored-by: t8y2 <t8y2@users.noreply.github.com>
Co-authored-by: skyler <1156263951@qq.com>
2026-08-06 00:55:24 +08:00

93 lines
4.7 KiB
TypeScript
Vendored

import { strict as assert } from "node:assert";
import { readFileSync } from "node:fs";
import { test } from "vitest";
import { syncSidebarTreeNodeExpansion } from "../../apps/desktop/src/lib/sidebar/sidebarTreeExpansion.ts";
import type { TreeNode } from "../../apps/desktop/src/types/database.ts";
const treeItem = readFileSync("apps/desktop/src/components/sidebar/TreeItem.vue", "utf8");
const runtimeHost = readFileSync("apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue", "utf8");
const connectionTree = readFileSync("apps/desktop/src/components/sidebar/ConnectionTree.vue", "utf8");
const connectionStore = readFileSync("apps/desktop/src/stores/connectionStore.ts", "utf8");
test("sidebar rows retain database-specific node affordances", () => {
for (const nodeType of ["connection", "database", "schema", "table", "column", "mongo-db", "mongo-collection", "redis-db", "nacos-namespace", "mq-tenant"]) {
const sources = `${treeItem}\n${runtimeHost}`;
assert.ok(sources.includes(`node.type === "${nodeType}"`) || sources.includes(`node.type === '${nodeType}'`), nodeType);
}
assert.match(treeItem, /@dblclick="onDoubleClick"/);
assert.match(treeItem, /@keydown="onKeydown"/);
assert.match(treeItem, /@mousedown="onRowMouseDown"/);
assert.match(treeItem, /@contextmenu="onTreeItemContextMenu"/);
});
test("complex tree changes retain the full rebuild fallback", () => {
assert.match(connectionTree, /const filteredNodes = computed/);
assert.match(connectionTree, /filterSidebarTree\(/);
assert.match(connectionTree, /const flatNodes = computed<FlatTreeNode\[]>/);
assert.match(connectionTree, /flattenTree\(filteredNodes\.value\)/);
assert.match(connectionTree, /watch\(flatNodes,/);
assert.doesNotMatch(connectionTree, /treeScrollerRef\.value\?\.(?:forceUpdate|updateVisibleItems)/);
assert.match(connectionTree, /@node-toggled="onNodeToggled"/);
});
test("tree toggles synchronize filtered node clones with the live sidebar tree", () => {
const expandedConnection: TreeNode = {
id: "connection-1",
label: "Connection 1",
type: "connection",
connectionId: "connection-1",
isExpanded: true,
};
const collapsedClone: TreeNode = { ...expandedConnection, isExpanded: false };
const collapsedConnection: TreeNode = {
id: "connection-2",
label: "Connection 2",
type: "connection",
connectionId: "connection-2",
isExpanded: false,
};
const expandedClone: TreeNode = { ...collapsedConnection, isExpanded: true };
assert.equal(syncSidebarTreeNodeExpansion([expandedConnection], collapsedClone, false), true);
assert.equal(expandedConnection.isExpanded, false);
assert.equal(syncSidebarTreeNodeExpansion([collapsedConnection], expandedClone, true), true);
assert.equal(collapsedConnection.isExpanded, true);
assert.equal(syncSidebarTreeNodeExpansion([expandedConnection], expandedConnection, true), false);
});
test("async tree expansion does not restore a stale rendered clone state", () => {
const liveDatabase: TreeNode = {
id: "connection-1:database-1",
label: "Database 1",
type: "database",
connectionId: "connection-1",
database: "database-1",
isExpanded: false,
children: [],
};
const staleRenderedClone: TreeNode = { ...liveDatabase, children: [] };
liveDatabase.isExpanded = true;
assert.equal(syncSidebarTreeNodeExpansion([liveDatabase], staleRenderedClone, true), false);
assert.equal(liveDatabase.isExpanded, true);
});
test("tree filters retain a temporary expansion state", () => {
assert.match(connectionTree, /return \{ \.\.\.node, children: matchingChildren \};/);
assert.doesNotMatch(connectionTree, /children: matchingChildren,\s*isExpanded:\s*true/);
assert.match(connectionTree, /function onSearchToggle\(node: TreeNode\) \{\s*if \(!isTreeSearchFiltering\.value \|\| !node\.children\) return;/);
assert.match(connectionTree, /function onNodeToggled\(node: TreeNode, wasExpanded: boolean\) \{\s*if \(isTreeSearchFiltering\.value\) return;\s*syncSidebarTreeNodeExpansion\(store\.treeNodes, node, !wasExpanded\)/);
assert.match(runtimeHost, /shouldRunTreeNodeRowAction\(action, clickDetail, isGroupLabel\(node\)\)/);
});
test("tree rebuilds keep a context menu only while its target row remains visible", () => {
assert.match(connectionTree, /nodes\.find\(\(\{ node \}\) => matchesSidebarActionTarget\(node, contextMenuTarget\)\)\?\.node/);
assert.match(connectionTree, /if \(!visibleContextMenuTarget \|\| visibleContextMenuTarget\.valid === false\)/);
});
test("programmable object groups use the shared metadata loader", () => {
assert.match(runtimeHost, /const databaseObjectGroup = !!objectTypesForGroupNode\(node\.type\)/);
assert.match(connectionStore, /else if \(objectTypesForGroupNode\(node\.type\)\) \{\s*await loadObjectGroupChildren\(node, options\);/);
});