Files
shrimp-server/test/crudRegistry.test.ts
T
wc24 ca283c6733 v0.5.0: 重新组织目录结构,按职责拆分
- Shrimp.ts (875 行) 按类拆分到 Shrimp/(7 文件: Shrimp, ShrimpModule, ShrimpTableModule, ShrimpPower, ShrimpCrud, UserRoleModule, OperationLogModule)
- forMongo/: MongoShrimpCrud + MongoShrimp(原 ShrimpServer 的 mongo 装配部分,无 express)
- forExpress/: buildShrimpRouter + jwtAuth + types
- main/: index(0 依赖核心) + expmon(express+mongo 整合,导出 ShrimpServer) + types(纯协议)
- 入口子路径 server → expmon(express+mongo 整合方案命名更准确)
- 删除冗余的 index.ts / core.ts(合并为 main/index.ts)
2026-05-23 19:44:02 +08:00

40 lines
1.8 KiB
TypeScript

import { describe, it, expect } from "vitest"
import { Shrimp, ShrimpTableModule, MemoryShrimpCrud, ShrimpFieldTypes } from "../src/main"
import type { IFieldConfig } from "../src/main"
describe("crudRegistry 自动登记", () => {
it("ShrimpTableModule 构造时自动把 dataSource 登记到 root.crudRegistry", () => {
const shrimp = new Shrimp()
const crud = new MemoryShrimpCrud()
new ShrimpTableModule(shrimp, "tag", crud, [], "标签")
expect(shrimp.crudRegistry.get("tag")).toBe(crud)
})
it("通过 ShrimpTableModule.list 走 relation populate,只需声明字段无需手动 wire", async () => {
const shrimp = new Shrimp()
const tagCrud = new MemoryShrimpCrud<{ _id: string; name: string }>()
new ShrimpTableModule(shrimp, "tag", tagCrud, [
{ key: "name", label: "名称", type: ShrimpFieldTypes.String },
], "标签").initCrud()
const t1 = await tagCrud.add({ name: "热门" } as any)
const t2 = await tagCrud.add({ name: "促销" } as any)
const productCrud = new MemoryShrimpCrud<{ _id: string; code: string; tagIds: string[] }>()
const productFields: IFieldConfig[] = [
{ key: "code", label: "编码", type: ShrimpFieldTypes.String },
{
key: "tagIds", label: "标签", type: ShrimpFieldTypes.Relation,
relation: { moduleKey: "tag", labelKey: "name" }, multiple: true,
},
]
const productModule = new ShrimpTableModule(shrimp, "product", productCrud, productFields, "商品")
productModule.initCrud()
await productCrud.add({ code: "P001", tagIds: [t1._id, t2._id] } as any)
const result = await productModule.list({})
expect((result.list[0] as any).tagIds_label).toEqual(["热门", "促销"])
})
})