-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): evaluate menu title expressions
- Loading branch information
1 parent
5780327
commit 596a06e
Showing
6 changed files
with
426 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
bricks/nav/src/data-providers/get-menu-config-options.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { describe, test, expect } from "@jest/globals"; | ||
import { initializeI18n } from "@next-core/i18n"; | ||
import type { MicroApp } from "@next-core/types"; | ||
import { getMenuConfigOptions } from "./get-menu-config-options.js"; | ||
import type { MenuRawData } from "./get-menu-config-tree.js"; | ||
|
||
initializeI18n(); | ||
|
||
const mockError = jest.spyOn(console, "error"); | ||
|
||
describe("getMenuConfigOptions", () => { | ||
test("should work", async () => { | ||
const menuList: MenuRawData[] = [ | ||
{ | ||
menuId: "menu-a", | ||
title: "<% I18N('MENU_A') %>", | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
i18n: { | ||
en: { | ||
MENU_A: "Menu A", | ||
}, | ||
}, | ||
}, | ||
{ | ||
menuId: "menu-b", | ||
title: "<% I18N('MENU_B', 'Default Menu B') %>", | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
}, | ||
{ | ||
menuId: "menu-c", | ||
title: "<% I18N('MENU_C') %>", | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
}, | ||
{ | ||
menuId: "menu-d", | ||
title: undefined!, | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
}, | ||
{ | ||
menuId: "menu-e", | ||
title: "<% APP.localeName %>", | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
overrideApp: { | ||
name: "App A", | ||
} as MicroApp, | ||
}, | ||
{ | ||
menuId: "menu-f", | ||
title: "<% `${CTX.name} - ${I18n('')}` %>", | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
}, | ||
]; | ||
expect(await getMenuConfigOptions(menuList)).toEqual([ | ||
{ | ||
label: "Menu A (menu-a)", | ||
value: "menu-a", | ||
}, | ||
{ | ||
label: "Default Menu B (menu-b)", | ||
value: "menu-b", | ||
}, | ||
{ | ||
label: "MENU_C (menu-c)", | ||
value: "menu-c", | ||
}, | ||
{ | ||
label: " (menu-d)", | ||
value: "menu-d", | ||
}, | ||
{ | ||
label: "App A (menu-e)", | ||
value: "menu-e", | ||
}, | ||
{ | ||
label: "<% `${CTX.name} - ${I18n('')}` %> (menu-f)", | ||
value: "menu-f", | ||
}, | ||
]); | ||
}); | ||
|
||
test("syntax error in expression", async () => { | ||
mockError.mockReturnValueOnce(); | ||
expect( | ||
await getMenuConfigOptions([ | ||
{ | ||
menuId: "menu-b", | ||
title: "<% I18N('MENU_B' %>", | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
}, | ||
]) | ||
).toEqual([ | ||
{ | ||
label: "<% I18N('MENU_B' %> (menu-b)", | ||
value: "menu-b", | ||
}, | ||
]); | ||
expect(mockError).toBeCalledTimes(1); | ||
expect(mockError).toBeCalledWith( | ||
"Parse menu title expression \"<% I18N('MENU_B' %>\" failed:", | ||
expect.any(SyntaxError) | ||
); | ||
}); | ||
|
||
test("expression evaluates error", async () => { | ||
mockError.mockReturnValueOnce(); | ||
expect( | ||
await getMenuConfigOptions([ | ||
{ | ||
menuId: "menu-b", | ||
title: "<% I18N[0][0] %>", | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
}, | ||
]) | ||
).toEqual([ | ||
{ | ||
label: "<% I18N[0][0] %> (menu-b)", | ||
value: "menu-b", | ||
}, | ||
]); | ||
expect(mockError).toBeCalledTimes(1); | ||
expect(mockError).toBeCalledWith( | ||
'Evaluate menu title expression "<% I18N[0][0] %>" failed:', | ||
expect.any(TypeError) | ||
); | ||
}); | ||
|
||
test("expression to non-string", async () => { | ||
mockError.mockReturnValueOnce(); | ||
expect( | ||
await getMenuConfigOptions([ | ||
{ | ||
menuId: "menu-b", | ||
title: "<% {} %>", | ||
type: "main", | ||
app: [{ appId: "app-a" }], | ||
instanceId: "i-a", | ||
}, | ||
]) | ||
).toEqual([ | ||
{ | ||
label: "<% {} %> (menu-b)", | ||
value: "menu-b", | ||
}, | ||
]); | ||
expect(mockError).toBeCalledTimes(1); | ||
expect(mockError).toBeCalledWith( | ||
'The result of menu title expression "<% {} %>" is not a string:', | ||
{} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { createProviderClass } from "@next-core/utils/general"; | ||
import { i18n } from "@next-core/i18n"; | ||
import type { MenuRawData } from "./get-menu-config-tree"; | ||
import { smartDisplayForMenuTitle } from "./shared/smartDisplayForMenuTitle"; | ||
|
||
export interface MenuOption { | ||
label: string; | ||
value: string; | ||
} | ||
|
||
/** | ||
* 构造用于菜单自定义的下拉选项数据。 | ||
* | ||
* 将对菜单标题进行表达式解析,支持 I8N 和 APP。 | ||
*/ | ||
export async function getMenuConfigOptions( | ||
menuList: MenuRawData[] | ||
): Promise<MenuOption[]> { | ||
const options: MenuOption[] = []; | ||
|
||
for (const menu of menuList) { | ||
if (menu.type === "main") { | ||
const menuI18nNamespace = `customize-menu/${menu.menuId}~${menu.app[0].appId}+${ | ||
menu.instanceId | ||
}`; | ||
// Support any language in `menu.i18n`. | ||
Object.entries(menu.i18n ?? {}).forEach(([lang, resources]) => { | ||
i18n.addResourceBundle(lang, menuI18nNamespace, resources); | ||
}); | ||
options.push({ | ||
label: `${smartDisplayForMenuTitle(menu.title, menuI18nNamespace, menu.overrideApp) ?? ""} (${menu.menuId})`, | ||
value: menu.menuId, | ||
}); | ||
} | ||
} | ||
|
||
return options; | ||
} | ||
|
||
customElements.define( | ||
"nav.get-menu-config-options", | ||
createProviderClass(getMenuConfigOptions) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.