import { describe, expect, it } from "vitest"; import { buildMenuParentTree, getMenuEditorFields, normalizeMenuEditorInput } from "./system-menu-form"; import type { SystemMenuItem } from "@/types"; describe("system menu editor", () => { it("shows only fields supported by each menu type", () => { expect(getMenuEditorFields("directory")).toEqual({ path: true, component: false, permission: false, icon: true }); expect(getMenuEditorFields("page")).toEqual({ path: true, component: true, permission: true, icon: true }); expect(getMenuEditorFields("button")).toEqual({ path: false, component: false, permission: true, icon: false }); }); it("clears route-only values when saving a button menu", () => { expect(normalizeMenuEditorInput({ type: "button", path: "/users", componentKey: "SystemUsers", permissionCode: "system:user:update", icon: "User", })).toMatchObject({ path: null, componentKey: null, permissionCode: "system:user:update", icon: null, }); }); it("does not offer buttons or the edited branch as a parent", () => { const menus = [ { id: "root", name: "系统管理", type: "directory", parentId: null, sortOrder: 0 }, { id: "users", name: "用户管理", type: "page", parentId: "root", sortOrder: 1 }, { id: "user-edit", name: "编辑用户", type: "button", parentId: "users", sortOrder: 1 }, { id: "roles", name: "角色管理", type: "page", parentId: "root", sortOrder: 2 }, ] as SystemMenuItem[]; const parentTree = buildMenuParentTree(menus, "root"); expect(parentTree.map((item) => item.id)).toEqual([]); const editParentTree = buildMenuParentTree(menus, "users"); expect(editParentTree[0].id).toBe("root"); expect(editParentTree[0].children?.map((item) => item.id)).toEqual(["roles"]); }); });