fix: getMenuTree 默认用 module.label,而非凭空假定 i18n key

Shrimp.getMenuTree() 之前默认输出 { t: '<moduleKey>.menuManage' },
强加一个用户未必写过的 i18n key,不写翻译就显示 raw key。退化为直接
用 module.label —— 模块注册时已声明,不另起 key。

UserModule / RoleModule 同步补 label: 之前 super(parent, "user", ...)
没传 label,会落到 ShrimpTableModule 默认的 { t: moduleKey };现在显
式传 { t: "user.menuManage" } / { t: "role.menuManage" },沿用 hand-
admin 已有的翻译键,迁移零成本。

demo 里临时塞的 DemoShrimp 子类清掉,直接 new Shrimp()。
This commit is contained in:
2026-05-23 15:52:27 +08:00
parent fea7c16d65
commit 21a56a7b2d
3 changed files with 32 additions and 16 deletions
+27
View File
@@ -0,0 +1,27 @@
import { describe, it, expect } from "vitest"
import { Shrimp, ShrimpTableModule, MemoryShrimpCrud, UserModule, RoleModule } from "../src"
describe("Shrimp.getMenuTree", () => {
it("返回的 label 等于模块注册时声明的 label", () => {
const shrimp = new Shrimp()
new ShrimpTableModule(shrimp, "tag", new MemoryShrimpCrud(), [], "标签管理")
new ShrimpTableModule(shrimp, "product", new MemoryShrimpCrud(), [], { t: "product.menuManage" })
const tree = shrimp.getMenuTree()
expect(tree).toEqual([
{ name: "tag", label: "标签管理", moduleKey: "tag" },
{ name: "product", label: { t: "product.menuManage" }, moduleKey: "product" },
])
})
it("内置 UserModule / RoleModule 的菜单 label 用 user.menuManage / role.menuManage", () => {
const shrimp = new Shrimp()
new UserModule(shrimp, new MemoryShrimpCrud() as any)
new RoleModule(shrimp, new MemoryShrimpCrud() as any)
const tree = shrimp.getMenuTree()
expect(tree.find(m => m.moduleKey === "user")?.label).toEqual({ t: "user.menuManage" })
expect(tree.find(m => m.moduleKey === "role")?.label).toEqual({ t: "role.menuManage" })
})
})