126 lines
3.5 KiB
TypeScript
126 lines
3.5 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import {
|
|
fetchDesktopMenuTree,
|
|
fetchWorkbenchApps,
|
|
fetchAppCenterMenus,
|
|
} from "./api.js";
|
|
|
|
const mockMenuPayload = {
|
|
items: [
|
|
{
|
|
id: "newHomePage",
|
|
label: "首页",
|
|
appId: "100-home",
|
|
href: "/home",
|
|
children: [],
|
|
},
|
|
{
|
|
id: "dataLink",
|
|
label: "应用管理",
|
|
appId: "160-datalink-platform",
|
|
children: [
|
|
{
|
|
id: "SCGY:corp-test",
|
|
label: "用车补贴",
|
|
code: "dataLinkItem",
|
|
href: "/third-party-item/SCGY:corp-test",
|
|
appId: "160-datalink-platform",
|
|
type: "MENU",
|
|
fromWay: "SYSTEM",
|
|
},
|
|
{
|
|
id: "ID01PIqCA1Di0v",
|
|
label: "用车制度",
|
|
code: "dataLinkItem",
|
|
href: "/third-party-item/ID01PIqCA1Di0v",
|
|
appId: "160-datalink-platform",
|
|
type: "MENU",
|
|
fromWay: "SYSTEM",
|
|
},
|
|
{
|
|
id: "ID01RKbEvQsWbd",
|
|
label: "新转转集团",
|
|
code: "dataLinkItem",
|
|
href: "/third-party-item/ID01RKbEvQsWbd",
|
|
appId: "160-datalink-platform",
|
|
type: "MENU",
|
|
fromWay: "SYSTEM",
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: "appcenter",
|
|
label: "应用中心",
|
|
appId: "150-appcenter",
|
|
children: [
|
|
{
|
|
id: "selfBuiltApplication",
|
|
label: "自建应用",
|
|
code: "selfBuiltApplication",
|
|
href: "/expense-center-new/self_built_app",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
describe("Domains/Menu - API 单元测试", () => {
|
|
test("fetchDesktopMenuTree 成功解析全站桌面端菜单响应", async () => {
|
|
const mockClient = {
|
|
get: async (url: string, opts: any) => {
|
|
expect(url).toBe("api/menu/v2/menus/byDeviceTypeAndAppId/$MAIN_FRAME_DESKTOP");
|
|
expect(opts.searchParams.corpId).toBe("corp-test");
|
|
return {
|
|
json: async () => mockMenuPayload,
|
|
};
|
|
},
|
|
} as any;
|
|
|
|
const items = await fetchDesktopMenuTree(mockClient, "corp-test");
|
|
expect(items.length).toBe(3);
|
|
expect(items[0].label).toBe("首页");
|
|
expect(items[1].label).toBe("应用管理");
|
|
expect(items[2].label).toBe("应用中心");
|
|
});
|
|
|
|
test("fetchWorkbenchApps 成功提取应用管理下的 7/N 个业务应用", async () => {
|
|
const mockClient = {
|
|
get: async () => ({
|
|
json: async () => mockMenuPayload,
|
|
}),
|
|
} as any;
|
|
|
|
const apps = await fetchWorkbenchApps(mockClient, "corp-test");
|
|
expect(apps.length).toBe(3);
|
|
expect(apps[0].id).toBe("SCGY:corp-test");
|
|
expect(apps[0].name).toBe("用车补贴");
|
|
expect(apps[0].href).toBe("/third-party-item/SCGY:corp-test");
|
|
expect(apps[1].name).toBe("用车制度");
|
|
expect(apps[2].name).toBe("新转转集团");
|
|
});
|
|
|
|
test("fetchAppCenterMenus 成功提取应用中心子栏目", async () => {
|
|
const mockClient = {
|
|
get: async () => ({
|
|
json: async () => mockMenuPayload,
|
|
}),
|
|
} as any;
|
|
|
|
const menus = await fetchAppCenterMenus(mockClient, "corp-test");
|
|
expect(menus.length).toBe(1);
|
|
expect(menus[0].id).toBe("selfBuiltApplication");
|
|
expect(menus[0].label).toBe("自建应用");
|
|
});
|
|
|
|
test("当菜单树无 dataLink 节点时安全返回空数组", async () => {
|
|
const mockClient = {
|
|
get: async () => ({
|
|
json: async () => ({ items: [] }),
|
|
}),
|
|
} as any;
|
|
|
|
const apps = await fetchWorkbenchApps(mockClient, "corp-test");
|
|
expect(apps).toEqual([]);
|
|
});
|
|
});
|